Skip to content

Filter transformation #12

Description

@nullpainter

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.

Activity

  1. tom-englert commented on Sep 27, 2018

    @tom-englert
    Collaborator

    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?

  2. nullpainter commented on Sep 27, 2018

    @nullpainter
    Author

    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.

  3. tom-englert commented on Sep 28, 2018

    @tom-englert
    Collaborator

    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?

  4. nullpainter commented on Sep 28, 2018

    @nullpainter
    Author

    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.

  5. tom-englert commented on Sep 29, 2018

    @tom-englert
    Collaborator

    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?

  6. nullpainter commented on Sep 30, 2018

    @nullpainter
    Author

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions