Premium forums

Multiple Filters on datasource

  • Yousuf Mohammed
    Yousuf Mohammed avatar

    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,



  • Alexander Valchev
    Alexander Valchev avatar

    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!
  • Yousuf Mohammed
    Yousuf Mohammed avatar

    16 May 2012 (Link to this post)

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

  • Greg Lavelle
    Greg Lavelle avatar

    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?


  • Yousuf Mohammed
    Yousuf Mohammed avatar

    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 });
  • Greg Lavelle
    Greg Lavelle avatar

    29 May 2012 (Link to this post)

    Thanks Yousuf, that got it working
  • Fazlur
    Fazlur avatar

    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.
  • Daniel
    Daniel avatar

    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!
  • Benjamin
    Benjamin avatar

    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


  • Alexander Valchev
    Alexander Valchev avatar

    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!

Read FAQ or see Kendo UI in action!

Launch Demos