Creating MDataGrid date filter

In this post I will describe how to create custom filter for MDataGrid (my extension of Flex 3 DataGrid described in one of the previous posts and hosted on http://code.google.com/p/reusable-fx/). I will guide you through creation of date filter as an example.

I consider extensibility one of the most important feature of well designed reusable component and that’s why I am trying to make process of creating new MDataGrid filters as straightforward as possible.

Following “Favor object composition over class inheritance.” rule I decided to split filtering functionality among a number of classes rather than write another thousand lines of DataGrid code. At the first glance the solution may look complicated but after understanding responsibilities of “filters” and “filter editors” it is really straightforward.

Filter editors are the components displayed below column header after clicking filter button. Filter editor know nothing about business logic of filtering, their only responsibility is to present current state of the filter to the user and modify filter according to users actions. Filter editor may be any component implementing IColumnFilterEditor interface. The default implementation of this interface is FilterEditorBase class extending Box which is used as a base class for all standard MDataGrid filter editors. In the presented example we will create filter editor in MXML extending FilterEditorBase class.

Filter classes are where actual filtering takes place, they implement filterFunction which is used to eliminate items from MDataGrid data provider. Filters are also responsible for examining MDataGrid and presenting information about MDataGrid content to filter editors. In our example filter will present minimum and maximum dates found in given column. All filters extends ColumnFilterBase class.

To implement date filtering mechanism two classes have to be implemented: filter class (DateRangeFilter) and filter editor class (DateChooserFilterEditor).

DateRangeFilter

DateRangeFilter extends ColumnFilterBase class and defines four fields: dataMinimum, dataMaximum, minimum and maximum.

dataMinimum and dataMaximum represents a earliest and latest dates found in MDataGrid. These fields are updated by updateOriginalDateRange() function.

minimum and maximum defines the range of dates which will be displayed in MDataGrid. By default minimum is set to dataMinimum and maximum is set to dataMaximum which means that all data are displayed (filter is inactive). It is important to call commitFilterChange() function inside minimum/maximum setter to inform MDataGrid that filter value have changed.

DateRangeFilter also overrides two functions defined in ColumnFilterBase:

isActive getter checks if filter is active i.e. if it may eliminate any items form MDataGrid. In our case isActive getter simply checks if minimum and maximum differs from dataMinimum and dataMaximum.

override public function get isActive():Boolean
{
    return (minimum != dataMinimum || maximum != dataMaximum);
}

filterFunction checks if item passed as an argument should be eliminated. First date displayed in column associated with this filter is extracted from given item and then it is compared against minimum and maximum. If the value is within the range true is returned (item is not eliminated) other ways false is returned (item is filtered out). If no date is fond for the given item true is returned. filterFunction is called many times for every MDataGrid item so it should not be computationally expensive.

override public function filterFunction(obj:Object):Boolean
{
    var value:Date = itemToDate(obj);
    
    if (value)
    {
        if (minimum && value < minimum)
        {
            return false;
        }
        if (maximum && value > maximum)
        {
            return false;
        }
    }
    return true;
}

You can find two additional functions in DateRangeFilter code: itemToDate() is responsible for extracting date from data item and originalCollectionChandeHandler() refresh dataMinimum and dataMaximum when MDataGrid original collection (copy of data provider) changes.

DateChooserFilterEditor

Good news: creating filter editors in MXML is much simpler than coding filter and the effect is instantly visible!

To make curly brackets binding simpler it is good idea to create strongly typed, bindable reference to filter instance edited by filter editor. The code below is the only code placed in <script> tag of our filter editor:

[Bindable]
protected var filter:DateRangeFilter;

override public function startEdit(column:MDataGridColumn):void
{
    super.startEdit(column);
    if (!column.filter || !column.filter is DateRangeFilter)
    {
        column.filter = new DateRangeFilter(column);
    }
    filter = column.filter as DateRangeFilter;
}

All you have to do now to create cool filter editor is placing MXML components with appropriate bindings and inline event handlers. Below you can see my proposals how date filter editor may look like.

The simple DateChooserFilterEditor consists of two DateChoosers and reset button.
View source is enabled, you can download zipped sources from here.

For better experience open example in separate window.

To save some space I have replaced DataChoosers with DateField and created DateFieldFilterEditor.
View source is enabled, you can download zipped sources from here.

For better experience open example in separate window.

Of course dates may be filtered in many different ways. For example date filter editor may consist of two NumericSteppers to select years. I hope now you can create such filter editor by yourself.

If you have any questions or suggestions don’t hesitate to add a comment!

Share on Google+Share on FacebookShare on LinkedInPin on PinterestTweet about this on Twitter

53 thoughts on “Creating MDataGrid date filter”

  1. Is there any way to use a checkbox control on two columns in two datagrid to filter out the rows where a specified columns = 0 in each datagrid?

  2. @Norbert

    Norbert :
    Hi Iwo,
    First of all, I would like to say this a great component, though, I had a little problem using the MultipleChoiceFilterEditor, the supposedly list of CheckBoxes became Toggle Buttons. This happens when I used SDK 4 and loaded the MDataGrid inside a Module. Can you give me some pointers to fix it?
    Thank you in advance.
    Norbert

    @Ricardo

    Hi Iwo, this is a great component! I use it everywhere! but I have the same annoying issue. How can I solve it?
    Thank you very much
    Jenry

  3. Hi Iwo great control. I have an issue with the style of filter button can’t change(filter.png and filter_active.png) I run my project on Flex 4 and also on Apache Fles but the color of filter button doesn,t change when the filter is applied. Could you help me with that. Thanks in advance.

Leave a Reply

Your email address will not be published. Required fields are marked *