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

Multiple Filters on datasource

10 Answers 1515 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Yousuf Mohammed
Top achievements
Rank 1
Yousuf Mohammed asked on 15 May 2012, 09:12 PM

Hello,

I am trying to filter datasource on multiple fields. 

Below is my Filter 1 - 
  var _flt = { logic: "or", filters: [] };
        $("#mySelection").find(':checkbox').each(function () {
            if (this.checked) {
                _flt.filters.push({ field: "TypeID", operator: "eq", value: parseInt(this.value) });
            }
        });       
        dataSource1.query({ filter: _flt});

The above javascript code works great when filtering on 1 field. 

I am trying to add one more filter, the below filter should do a logical and to the results from first filter. 

var _fltStatus = { logic: "or", filters: [] };
        $("#myStatusSelection").find(':checkbox').each(function () {
            if (this.checked) {
                _fltStatus.filters.push({ field: "StatusID", operator: "eq", value: parseInt(this.value) });
            }
        });
       
dataSource1.query({ filter: _fltStatus });


Basically what I want is 

(TypeID  = 1 OR TypeID  = 2 OR TypeID  = 3) AND  (StatusID = 1 OR StatusID = 2)

I am trying to do local filtering. Please let me know if this is doable.

Thanks,



10 Answers, 1 is accepted

Sort by
0
Accepted
Alexander Valchev
Telerik team
answered on 16 May 2012, 01:47 PM
Hello Yousuf,

It is possible to pass an array of more complex filter objects to the filter method of the dataSource. In order to illustrate this functionality I created a small example.
Alternatively in this forum topic you can find information about how to create a custom filter operator. You could use this functionality to build your own filtering logic.

Regards,
Alexander Valchev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Yousuf Mohammed
Top achievements
Rank 1
answered on 16 May 2012, 06:51 PM
Thank you very much for your reply. I will give it a try.

0
Greg Lavelle
Top achievements
Rank 1
answered on 28 May 2012, 10:42 AM
Hi

This 'AND' and 'OR' filtering is exactly what I am looking for, although I would like to build the filter up dynamically via javascript in response to button clicks. Is there any way to do this?


0
Yousuf Mohammed
Top achievements
Rank 1
answered on 28 May 2012, 09:26 PM
Hi Greq,

I created dynamic filters based on the checkbox selected . Below is my code
  var _fltMain = [];
      

var _flt = { logic: "or", filters: [] };
        $("#mySelection").find(':checkbox').each(function () {
            if (this.checked) {
                _flt.filters.push({ field: "TypeID", operator: "eq", value: parseInt(this.value) });
            }
        });       

var _fltStatus = { logic: "or", filters: [] };
        $("#myStatusSelection").find(':checkbox').each(function () {
            if (this.checked) {
                _fltStatus.filters.push({ field: "StatusID", operator: "eq", value: parseInt(this.value) });
            }
        });
       
        
_fltMain .push(_flt);        
_fltMain .push(_fltStatus);
dataSource1.query({ filter: _fltMain });
0
Greg Lavelle
Top achievements
Rank 1
answered on 29 May 2012, 12:05 PM
Thanks Yousuf, that got it working
0
Fazlur
Top achievements
Rank 1
answered on 12 Sep 2012, 08:11 AM
Can i get the same AND/OR expression in C# code. I have the following code
DataSourceRequest dsRequest = new DataSourceRequest();
FilterDescriptor newDesc = new FilterDescriptor("FirstName", FilterOperator.Contains, 'fazlur');
dsRequest.Filters.Add(newDesc);
newDesc = new FilterDescriptor("LastName", FilterOperator.Contains, 'fazlur');
dsRequest.Filters.Add(newDesc);

This gives me AND expression. But i want OR. For example

FirstName LIKE '%fazlur%' OR LastName LIKE '%fazlur%'.

Can you please help.
0
Daniel
Telerik team
answered on 17 Sep 2012, 08:38 AM
Hello,

This is possible by using a composite descriptor e.g.

CompositeFilterDescriptor compositeDescriptor = new CompositeFilterDescriptor();
compositeDescriptor.LogicalOperator = FilterCompositionLogicalOperator.Or;
FilterDescriptor newDesc = new FilterDescriptor("FirstName", FilterOperator.Contains, 'fazlur');
compositeDescriptor.FilterDescriptors.Add(newDesc);
newDesc = new FilterDescriptor("LastName", FilterOperator.Contains, 'fazlur');
compositeDescriptor.FilterDescriptors.Add(newDesc);
DataSourceRequest dsRequest = new DataSourceRequest();
dsRequest.Filters.Add(compositeDescriptor);
Regards,
Daniel
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Benjamin
Top achievements
Rank 2
answered on 19 Dec 2012, 06:20 AM
after running the sample javascript to push a filter to array of filters, an error occurred 

Line: 356
Error: Function expected

on 
_flt.filters.push


0
Alexander Valchev
Telerik team
answered on 21 Dec 2012, 04:45 PM
Hello Benjamin,

I am afraid that the provided information is not sufficient enough in order to determine what is going wrong. Is it possible for you to reproduce the problem in jsBin/jsFiddle and send me back a link? In this way I would be able to examine your current implementation in details and assist you further.

Alternatively you may open a support ticket with a small but runnable example attached.

Kind regards,
Alexander Valchev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Christos
Top achievements
Rank 1
answered on 26 Mar 2019, 03:53 AM
Thank you!
Tags
Data Source
Asked by
Yousuf Mohammed
Top achievements
Rank 1
Answers by
Alexander Valchev
Telerik team
Yousuf Mohammed
Top achievements
Rank 1
Greg Lavelle
Top achievements
Rank 1
Fazlur
Top achievements
Rank 1
Daniel
Telerik team
Benjamin
Top achievements
Rank 2
Christos
Top achievements
Rank 1
Share this question
or