This is a migrated thread and some comments may be shown as answers.

Can "or" be used to filter a datasource?

12 Answers 2176 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
T
Top achievements
Rank 1
T asked on 02 Apr 2012, 05:53 PM
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?

12 Answers, 1 is accepted

Sort by
0
Rosen
Telerik team
answered on 04 Apr 2012, 07:07 AM
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!
0
T
Top achievements
Rank 1
answered on 04 Apr 2012, 12:04 PM
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!
0
Rosen
Telerik team
answered on 04 Apr 2012, 03:58 PM
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!
0
T
Top achievements
Rank 1
answered on 04 Apr 2012, 04:27 PM
Wow!  Awesome!
0
Erlend
Top achievements
Rank 2
answered on 06 Jun 2012, 06:15 AM
Would be great if this filter documentation was part of the datasource documentation. 
0
T
Top achievements
Rank 1
answered on 20 Jun 2012, 08:17 PM
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?
0
Trent Jones
Top achievements
Rank 1
answered on 31 Jan 2013, 08:14 PM
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()
                            }
                        ]
                    }
0
Rosen
Telerik team
answered on 01 Feb 2013, 11:40 AM
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!
0
Trent Jones
Top achievements
Rank 1
answered on 01 Feb 2013, 01:51 PM
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.
0
Rosen
Telerik team
answered on 04 Feb 2013, 07:25 AM
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!
0
Shashank
Top achievements
Rank 1
answered on 10 Jul 2017, 10:00 AM

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...

0
Boyan Dimitrov
Telerik team
answered on 13 Jul 2017, 07:19 AM

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.
Tags
Data Source
Asked by
T
Top achievements
Rank 1
Answers by
Rosen
Telerik team
T
Top achievements
Rank 1
Erlend
Top achievements
Rank 2
Trent Jones
Top achievements
Rank 1
Shashank
Top achievements
Rank 1
Boyan Dimitrov
Telerik team
Share this question
or