How to define a Kendo grid Column filter between two dates?

18 posts, 1 answers
  1. Mahlon
    Mahlon avatar
    8 posts
    Member since:
    Dec 2013

    Posted 01 Aug 2014 Link to this post

    In our application we want the filter on a date column to prompt the user for a start date and an end date, with the filter returning rows where the field in question falls between (or on) those two dates.

    Initial Approach

    Our initial approach was to restrict date types to use gte and lte operators, and add the "extra : true" filterable option on the column. This came close, but presented the following problems: A) Each date input could use either the gte (Start) or lte (End) operator, providing undesired flexibility and the option for the user to create a filter that would never return results, and B) Presented a logical comparison (And / Or) that we don't want.

    Better Approach

    A better approach was found on this StackOverflow question. This question has an answer by Matthew Erwin that gets us very close: it allows us to completely re-style the filter entirely, so we can present simply a Start Date input and an End date input. However, what I can't get working is associating the right filter operation with the right input (gte for the Start date, lte for the End date). My custom filter is as follows:

    $scope.dateFilter = {
        extra: true,
        operators: {},
        ui: function (element) {
            var parent = element.parent();
            while (parent.children().length > 1)
                $(parent.children()[0]).remove();
     
            parent.prepend(
                "Start Date:<br/><span class=\"k-widget k-datepicker k-header\">" +
                "<span class=\"k-picker-wrap k-state-default\">" +
                "<input data-bind=\"value: filters[0].value\" class=\"k-input\" type=\"text\" data-role=\"datepicker\"" +
                " style=\"width: 100%\" role=\"textbox\" aria-haspopup=\"true\" aria-expanded=\"false\" aria-disabled=\"false\" " +
                " aria-readonly=\"false\" aria-label=\"Choose a date\">" +
                "<span unselectable=\"on\" class=\"k-select\" role=\"button\">" +
                "<span unselectable=\"on\" class=\"k-icon k-i-calendar\">select</span></span></span></span>" +
     
                "<br/>End Date:<br/>" +
                "<span class=\"k-widget k-datepicker k-header\"><span class=\"k-picker-wrap k-state-default\">" +
                "<input data-bind=\"value: filters[1].value\" class=\"k-input\" type=\"text\" data-role=\"datepicker\"" +
                " style=\"width: 100%\" role=\"textbox\" aria-haspopup=\"true\" aria-expanded=\"false\" " +
                " aria-disabled=\"false\" aria-readonly=\"false\" aria-label=\"Choose a date\">" +
                "<span unselectable=\"on\" class=\"k-select\" role=\"button\">" +
                "<span unselectable=\"on\" class=\"k-icon k-i-calendar\">select</span></span></span></span>"
            );
        }
    };

    With this approach, the Odata filter option is generated for each of the dates, however it uses the eq Equal To operator, so no values are ever returned. We aren't building filters specifically on the data source.Is there a simple way I can associate each of those date inputs with a specific filter operator? Is there a better way to approach this subject? It seems like filtering dates based on a Start - End range would be commonly desired.

    Other Details

    We are using AngularJS, and WebAPI with Odata.
  2. Mahlon
    Mahlon avatar
    8 posts
    Member since:
    Dec 2013

    Posted 04 Aug 2014 in reply to Mahlon Link to this post

    Update: Since my last post I've tried to add a filter directly to my kendo data source. My reasoning goes that if I add the desired filters myself, I can specify what operator they use and use them in the grid filter UI.

    So my Angular controller has code that basically works as follows:

    $scope.dataSource = service.getDataSource();
     
    var filter = {
        logic: "and",
        filters: [
            { field: "FinalDate", operator: "ge" },
            { field: "FinalDate", operator: "le" }
        ]
    };
     
    $scope.dataSource.filter(filter);

    $scope.dataSource is what the grid is bound to with k-data-source. "FinalDate" is the field I want to filter on. 

    However, when I try this I get the following error message: 

    "Property not found!: Parameter 'FinalDate_0' not found in context"

    It complains that it can't find the property FinalDate_0, but I didn't specify that - I specified 'FinalDate'. So what is going on?

    Again, it seems like it should be much simpler to filter between two dates, so I can't help but feel that i am missing something and over-complicating matters.
  3. Petur Subev
    Admin
    Petur Subev avatar
    1882 posts

    Posted 05 Aug 2014 Link to this post

    Hello Mahlon,

    Here is an example that shows how you can use the filterMenuInit event to customize the filter menu and set default operators. 

    filterMenuInit: function(e){
                              if(e.field=="OrderDate"){
                                var operator = e.container
                                    .find("[data-role=dropdownlist]:eq(2)").data("kendoDropDownList");
                                operator.value("lt");
                                operator.trigger("change");
                                e.container
                                    .find("[data-role=dropdownlist]:eq(1)").data("kendoDropDownList").wrapper.hide();
                              }
                            },


    Please notice that passing filters without values such as:

    var filter = {
        logic: "and",
        filters: [
            { field: "FinalDate", operator: "ge" },
            { field: "FinalDate", operator: "le" }
        ]
    };

    Is not supported - you cannot set default operator in such way.

    If this is not what you are searching for please elaborate.

    Regards,
    Petur Subev
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  4. Mahlon
    Mahlon avatar
    8 posts
    Member since:
    Dec 2013

    Posted 05 Aug 2014 in reply to Petur Subev Link to this post

    Thanks for the reply, Petur.

    I created a method to use in the filterMenuinit method, but it doesn't seem to be firing. Here is the event markup for my grid:

    <div id="grid"
      // Other properties
      k-on-filterMenuInit = "filterInit(kendoEvent)"

    And here is the method in my Angular controller:

    $scope.filterInit = function (e) {
        alert("Filter initialized");
            if (e.field == "FinalDate") {
                var beginOperator = e.container.find("[data-role=dropdownlist]:eq(0)").data("kendoDropDownList");
                beginOperator.value("gte");
                beginOperator.trigger("change");
                 
                var endOperator = e.container.find("[data-role=dropdownlist]:eq(1)").data("kendoDropDownList");
                endOperator.value("lte");
                endOperator.trigger("change");
            }
        };

    The alert statement is there just to verify that the function was called, but it never runs. Do you know what I might be doing wrong?
  5. Petur Subev
    Admin
    Petur Subev avatar
    1882 posts

    Posted 06 Aug 2014 Link to this post

    Hello again,

    You should use dashes instead of camel case letters when the events and other options are more than one word. In this particular case you should use:

    k-on-filter-menu-init = "filterInit(kendoEvent)"

    Here is an example that I created:

    http://dojo.telerik.com/@pesho/UMIw/2


    Kind Regards,
    Petur Subev
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  6. Mahlon
    Mahlon avatar
    8 posts
    Member since:
    Dec 2013

    Posted 06 Aug 2014 in reply to Petur Subev Link to this post

    Thanks Petur! That was exactly what I needed. With that, I've been able to get my date filter to look like the screenshot I included, which is very close to what I want. My only remaining questions are:

    1.) Is there any way to change the text at the top of the filter? I'd like it to say "Show items with dates between:" instead of "Show items with value that:"

    2.) Is there any way to prevent the user from changing the Logic operator, without hiding it? Since we're presenting them a date range "And" is the only sensible option.

    Thanks again for all your help!
     







  7. Mahlon
    Mahlon avatar
    8 posts
    Member since:
    Dec 2013

    Posted 07 Aug 2014 in reply to Mahlon Link to this post

    I solved question number 1 above by using the "messages" configuration object in the column's filterable option, and that's working perfectly. Question 2, how to prevent the user changing the logic operator, still stands. 

    And I have one (final) question: Is there any way to apply this filter menu initialization to any date field, instead of having to specify each individual field it applies to? So instead of:

    if(e.field=="FieldName")

    Having:
    if(//Field type is Date)

    That would be preferable so that all of our dates could be configured with one object, instead of having to build the filterMenuInit event handler for each grid for each field individually.
  8. Answer
    Petur Subev
    Admin
    Petur Subev avatar
    1882 posts

    Posted 07 Aug 2014 Link to this post

    Hello again,

    Straight to the questions:

    1) You can use the readonly method  of the Kendo DropDownList - you already know how to access it - the same way as accessing the other DropDownLists.

    2) Inside the view event you have only access to the name of the field - you do not know the type. You can however get this information from the schema.model options.

    Here is what I mean:


    function onMenuInit(e) {
        alert('the type is -> ' + e.sender.dataSource.options.schema.model.fields[e.field].type);
    }


    e.g.

    http://dojo.telerik.com/@pesho/UMIw/3

    All the best,
    Petur Subev
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  9. Mahlon
    Mahlon avatar
    8 posts
    Member since:
    Dec 2013

    Posted 07 Aug 2014 in reply to Petur Subev Link to this post

    Thanks Petur! That takes care of everything. :)

    Thanks for the help!
  10. Prasert
    Prasert avatar
    3 posts
    Member since:
    May 2015

    Posted 26 Jul 2015 Link to this post

    Please tell me about how to make a filter two dates in the grid via razor (MVC).
  11. Boyan Dimitrov
    Admin
    Boyan Dimitrov avatar
    1958 posts

    Posted 29 Jul 2015 Link to this post

    Hello Prasert,

    In order to modify the solution from my college Peter to work with Grid MVC please use the following configuration: 

        1. Configure the "Operators" option in the grid filterable option to set what operators can be used for dates (gte, lte) and what text is displayed for each (Begin Date, End Date); Use the "Extra" option of the column "filterable" option to get the extra Date selector in the filter menu;Use the "Messages" option of the column "filterable" option to customize the filter display message. 

    .Filterable(ftb => ftb.Operators(oper => oper.ForDate(dat => dat.IsGreaterThanOrEqualTo("Begin Date").IsLessThanOrEqualTo("End Date"))).Extra(true).Messages(mes => mes.Info("Show items between dates")))

      2. Bind to the filter menu event and execute the same logic as the solution for Kendo UI Grid

    .Events(e => e.FilterMenuInit("filterMenuInit"))

    Regards,
    Boyan Dimitrov
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  12. Prasert
    Prasert avatar
    3 posts
    Member since:
    May 2015

    Posted 30 Jul 2015 in reply to Boyan Dimitrov Link to this post

    Thanks a lot Boyan Dimitrov.

    You 're very cool!

  13. Mark
    Mark avatar
    2 posts
    Member since:
    Jul 2013

    Posted 25 Apr in reply to Petur Subev Link to this post

    Looking at your dojo, this no longer works for order date, if you move to a new page of data, how to fix?

     

  14. Boyan Dimitrov
    Admin
    Boyan Dimitrov avatar
    1958 posts

    Posted 27 Apr Link to this post

    Hello Mark,

    The communication in this thread has several replies with dojo examples. Could you please specify which dojo you are testing with and what are the expected behavior and the actual results? 

    Regards,
    Boyan Dimitrov
    Telerik by Progress
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  15. Mark
    Mark avatar
    2 posts
    Member since:
    Jul 2013

    Posted 27 Apr in reply to Boyan Dimitrov Link to this post

    Hi Boyan,

    Using Dojo: http://dojo.telerik.com/@pesho/UMIw/2

    Initially I can click the "Order Date" filter menu and the options are set as desired (giving a between dates selector) , this is the behaviour I was expecting

    Click on a different data page (i.e. go directly to page 5) and now click the "Order Date" filter menu and the options have reverted to both being "Is equal to" - this behaviour is undesirable because if you hide the operator selectors, they will only ever work on the first page after a reload.

     

    Regards

    Mark

     

  16. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    2055 posts

    Posted 01 May Link to this post

    Hi Mark,

    You could attach a handler for the "activate" event of the popup, which will fire each time the filter menu is opened:
    Hope this helps.


    Regards,
    Konstantin Dikov
    Telerik by Progress
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  17. J
    J avatar
    3 posts
    Member since:
    Jul 2012

    Posted 15 May in reply to Konstantin Dikov Link to this post

    Hello,

    Is there any directive for open event k-on-filter-menu-open? It does not work for me. 

  18. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    2055 posts

    Posted 17 May Link to this post

    Hi Jaba,

    You could define the event handler through the options of the Grid:
    As for the directive, you could log this in our public repository, so we could expose this in the next releases:

    Regards,
    Konstantin Dikov
    Telerik by Progress
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Back to Top