Filter transformation
Is there an out-of-the-box way to perform mapping of DataGrid values to filter values? I have a year column which I'm wishing to aggregate in the associated multi-choice filter as decades. I've taken a look at your sample MultipleChoiceFilter and DataGridFilterColumnControl but I can't see anything obvious.
It's easy for me to build this in a non-reusable fashion - populating the filters values explicitly - but it would be elegant if I could instead provide some sort of two-way mapping class.
Aggregating dates by decades sounds very specific - what would be the reusable part?
If you build an AggregateDatesByDecadesFilter, what could you use it for other than aggregating dates?
My notion of reusability is two-fold. In order to perform custom aggregation for the filter, my filter has knowledge of the specific model of the data source for the DataGrid. What I am suggesting/asking is if there's a way to feed this data to the filter in a reusable fashion, so there is no explicit glue?
In a more general sense, I'm asking about implementation of an optional filter transformation class which can be provided to the filter for display in MultipleChoiceFilter. This would be a truly reusable filter, allowing a variety of aggregations (e.g., first letter, morning/afternoon/evening and any other use-case specific numeric transformations).
In my specific use case, the filter would be something like:
public class DecadeFilterTransform : IFilterTransform<int>
{
/// <summary>
/// Converts a year to a decade; e.g., 1973 -> "1970s".
/// </summary>
public string Transform(int value) => $"{Math.Floor(value / 10d) * 10}s";
}
public interface IFilterTransform<in T>
{
/// <summary>
/// Performs transformation of data into a filter value.
/// </summary>
/// <param name="value">source value to transform</param>
/// <returns>value displayed in filter list</returns>
string Transform(T value);
}
The value from Transform(T) is then used to populate the filter list. The default filter would just return the value unchanged, which represents the current behaviour.
Currently DGX provides only a list of strings: https://github.com/tom-englert/DataGridExtensions/blob/d43942ddf476927725e616fe11934975409f8016/DataGridExtensions/DataGridFilterColumnControl.cs#L350 To make your approach work, that would need to be the original objects, right?
Yes, that was rather optimistic sample code 😄
Original objects would naturally be ideal and most flexible, but - at least for my specific use-case - the string representations provided by the grid would also work, as the filtering will be on the column which is rendering the year.
Currently a multiple choice filter exists only as example, so I did not put too much effort in making it super-reusable. However if your solution fits more aspects, maybe you can just provide a PR?
Sure thing; I appreciated that it was an example. I'll see how I go - I may just work around it for now, but am happy to provide a PR a bit later.