Can "or" be used to filter a datasource?

13 posts, 0 answers
  1. T
    T avatar
    15 posts
    Member since:
    Feb 2012

    Posted 02 Apr 2012 Link to this post

    I need to check a single field for several possible values in my datasource filter.

    Imagine you want a filter to show only rows with "name" containing "Jennie" or "name" containing "Genie".

    Trying this yields no matches:

    myPeopleDataSource.filter([{field:"name",operator:"contains",value:"Jennie"}, {field:"name",operator:"contains",value:"Genie"}])

    I'm guessing this is because the filter is limited to one criteria per field per filter action.  

    Is that true?

    Any way around this?
  2. Rosen
    Admin
    Rosen avatar
    3247 posts

    Posted 04 Apr 2012 Link to this post

    Hi,

    Indeed, the or filtering is possible, however the format of the filter descriptor should be similar to the following:

    {
       logic:"or",
       filters: [
           {field:"name",operator:"contains",value:"Jennie"},
           {field:"name",operator:"contains",value:"Genie"}
       ]
    }

    All the best,
    Rosen
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  3. T
    T avatar
    15 posts
    Member since:
    Feb 2012

    Posted 04 Apr 2012 Link to this post

    Cool, thanks!  Is that documented somewhere?

    Sadly in my case I need to mix AND and OR.

    What if I need all records where ("name" contains "Jennie" OR "Genie") AND ("grade" equals "7")?

    Is there a way to do that?  Can I filter twice cumulatively?  Build a hierarchical filter? Or is there a custom filter function I can provide?  Or something else?

    Help me, Obi Wan!
  4. Rosen
    Admin
    Rosen avatar
    3247 posts

    Posted 04 Apr 2012 Link to this post

    Hello,

    You should nest the expressions. For example:

    {
       logic: "and",
       filters: [{
          logic:"or",
          filters: [
              {field:"name",operator:"contains",value:"Jennie"},
              {field:"name",operator:"contains",value:"Genie"}
          ]
      },
      { field:"grade",operator:"eq",value: 7 } ]
    }

    All the best,
    Rosen
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  5. T
    T avatar
    15 posts
    Member since:
    Feb 2012

    Posted 04 Apr 2012 Link to this post

    Wow!  Awesome!
  6. Erlend
    Erlend avatar
    11 posts
    Member since:
    Apr 2011

    Posted 06 Jun 2012 Link to this post

    Would be great if this filter documentation was part of the datasource documentation. 
  7. T
    T avatar
    15 posts
    Member since:
    Feb 2012

    Posted 20 Jun 2012 Link to this post

    Documentation would be nice, but they don't want you to know that the filter language is Turing complete.

    I've actually coded a surprisingly playable Nintendo 64 emulator using only the Kendo data filter language.

    Which brings me to my question:

    does anybody have a Super Mario World ROM image in json format?
  8. Trent Jones
    Trent Jones avatar
    49 posts
    Member since:
    Aug 2009

    Posted 31 Jan 2013 Link to this post

    I need to filter by three or fields.  Every example only shows two.  WHen i send in three how can i do the or.  Using MVC the IFilterDescriptor looses one of the filters.  It's like it only understands two.  Here is what i want but i want "OR" not "AND"

    var filter = {
                            logic: "or",
                            filters: [
                                {
                                    field: "LastName",
                                    operator: "startswith",
                                    value: text.toLowerCase()
                                },
                                {
                                    field: "FirstName",
                                    operator: "startswith",
                                    value: text.toLowerCase()
                                },
                                {
                                    field: "OrganizationName",
                                    operator: "startswith",
                                    value: text.toLowerCase()
                                }
                            ]
                        }
  9. Rosen
    Admin
    Rosen avatar
    3247 posts

    Posted 01 Feb 2013 Link to this post

    Hello Trent,

    I'm afraid that I'm unable to observe such behavior locally. I have attached a screenshot which shows the created structure which maps to the expression you have provided. 

    Greetings,
    Rosen
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  10. Trent Jones
    Trent Jones avatar
    49 posts
    Member since:
    Aug 2009

    Posted 01 Feb 2013 Link to this post

    Ya, not seeing that.  Here is the network post data from Firebug.  With the given example which is what i desire to get... I see this in firebug POST request on NETWORK panel:
    LastName~startswith~'a'~or~FirstName~startswith~'a'~or~OrganizationName~startswith~'a'

    Looks good so far.... However, when MVC gets ahold of it i see picture "ONE" as attached.... it drops OrganizationName altogether..

    If i change the filter to read this way:

    var filter = [{
                            logic: "or",
                            filters: [
                                {
                                    field: "LastName",
                                    operator: "startswith",
                                    value: text.toLowerCase()
                                },
                                {
                                    field: "FirstName",
                                    operator: "startswith",
                                    value: text.toLowerCase()
                                },
                                 
                            ]
                        },
                        {
                            field: "OrganizationName",
                            operator: "startswith",
                            value: text.toLowerCase()
                        }]
    Then i see this in the traffic in NETWORK panel:
    (LastName~startswith~'a'~or~FirstName~startswith~'a')~and~OrganizationName~startswith~'a'

    And attached picture TWO is the result in the DataSourceRequest object.

    Notice the "AND" in the last half of the equation.

    I am on v2012.3.1114 if that makes a difference.  No matter which way i send data, i can't seem to get the DataSourceRequest to read you have in your screenshot.  Please help.  Thanks.
  11. Rosen
    Admin
    Rosen avatar
    3247 posts

    Posted 04 Feb 2013 Link to this post

    Hi Trent,

    Could you please provide a small sample in which the behavior can be observed locally.

    Regarding the second construct, indeed this is expected. The default value of the logic field is and, thus as with your example, when it is omitted this value is used. 

    Greetings,
    Rosen
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  12. Shashank
    Shashank avatar
    4 posts
    Member since:
    Dec 2016

    Posted 10 Jul in reply to Rosen Link to this post

    Hi Rosen.

    I am stuck how to get this LOGICfiltering concept in c#?

    I want all (single, composite KENDO filter) to be converted into a string formatted statement, so that I can pass it as Dynamic query statement in SQL to stored Proc?

     

    can it be done?

    I just want colname, colvalue, operators value I will loop it and make SQL statement//

    EX: colname 'startswith' colvalue and/or colname 'endswith' colvalue   and so on...

  13. Boyan Dimitrov
    Admin
    Boyan Dimitrov avatar
    1966 posts

    Posted 13 Jul Link to this post

    Hello,

    My suggestion is to take a look at the Ajax Binding article and specifically the sections where DataSourceRequest object and ToDataSourceResult method are explained. The DataSourceRequest object contains the information about the page, sort and etc expressions applied. The ToDataSourceResult method is building expression trees so Entity Framework to create and execute sql queries against the data base.

    Regards,
    Boyan Dimitrov
    Progress Telerik
    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