Multiple Filters on datasource

10 posts, 1 answers
  1. Yousuf Mohammed
    Yousuf Mohammed avatar
    15 posts
    Member since:
    Dec 2007

    Posted 15 May 2012 Link to this post

    
    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,



  2. Answer
    Alexander Valchev
    Admin
    Alexander Valchev avatar
    2877 posts

    Posted 16 May 2012 Link to this post

    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!
  3. Kendo UI is VS 2017 Ready
  4. Yousuf Mohammed
    Yousuf Mohammed avatar
    15 posts
    Member since:
    Dec 2007

    Posted 16 May 2012 Link to this post

    Thank you very much for your reply. I will give it a try.

  5. Greg Lavelle
    Greg Lavelle avatar
    3 posts
    Member since:
    Oct 2006

    Posted 28 May 2012 Link to this post

    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?


  6. Yousuf Mohammed
    Yousuf Mohammed avatar
    15 posts
    Member since:
    Dec 2007

    Posted 28 May 2012 Link to this post

    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 });
  7. Greg Lavelle
    Greg Lavelle avatar
    3 posts
    Member since:
    Oct 2006

    Posted 29 May 2012 Link to this post

    Thanks Yousuf, that got it working
  8. Fazlur
    Fazlur avatar
    1 posts
    Member since:
    Sep 2012

    Posted 12 Sep 2012 Link to this post

    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.
  9. Daniel
    Admin
    Daniel avatar
    2118 posts

    Posted 17 Sep 2012 Link to this post

    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!
  10. Benjamin
    Benjamin avatar
    1 posts
    Member since:
    Apr 2011

    Posted 19 Dec 2012 Link to this post

    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


  11. Alexander Valchev
    Admin
    Alexander Valchev avatar
    2877 posts

    Posted 21 Dec 2012 Link to this post

    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!
Back to Top
Kendo UI is VS 2017 Ready