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

How do I use CompositeFilterDescriptor to set the initial datasource

2 Answers 381 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ray
Top achievements
Rank 1
Ray asked on 20 Jul 2015, 01:28 AM

I am looking to filter the datasource for Grid with a CompositeFilterDescriptor instead of just a FilterDescriptor so I can set the or condition. I can't quite figure out a way to do this using the example on your site as well as playing around with the different constructors for the add method. The add methods only take a FilterDescriptor.  Below is an example from your site, is this possible using the Filter() Method on the grid to asdd

 

@(Html.Kendo().Grid<Product>()
    .Name("grid")
    .DataSource(dataSource => dataSource
        .Ajax()
        .Read(read => read.Action("Products_Read", "Home"))
        .Filter(filters =>
        {
            // Show products whose ProductName property contains "C"
            filters.Add(product => product.ProductName).Contains("C");
            // and UnitsInStock is greater than 10
            filters.Add(product => product.UnitsInStock).IsGreaterThan(10);
        })
    )
)     

2 Answers, 1 is accepted

Sort by
1
Accepted
Alexander Popov
Telerik team
answered on 22 Jul 2015, 07:20 AM
Hello,

I would suggest using the Kendo.Mvc.CompositeFilterDescriptor and the AddRange method. For example: 
    ICollection<Kendo.Mvc.CompositeFilterDescriptor> filters = new List<Kendo.Mvc.CompositeFilterDescriptor>();
    filters.Add(new Kendo.Mvc.CompositeFilterDescriptor()
    {
        LogicalOperator = Kendo.Mvc.FilterCompositionLogicalOperator.And,
        FilterDescriptors = new Kendo.Mvc.Infrastructure.Implementation.FilterDescriptorCollection()
        {
            new Kendo.Mvc.FilterDescriptor() { Member = "ProductName", Operator = Kendo.Mvc.FilterOperator.IsEqualTo, Value = "Chai"},
            new Kendo.Mvc.FilterDescriptor() { Member = "Discontinued", Operator = Kendo.Mvc.FilterOperator.IsEqualTo, Value = true},
        }
    });
}
@(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.ProductViewModel>()
    .Name("grid")
    ...
    .DataSource(dataSource => dataSource
        .Ajax()
        .Filter(f=>f.AddRange(filters))


Regards,
Alexander Popov
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
John
Top achievements
Rank 1
Veteran
commented on 17 Aug 2021, 08:10 PM

This helped me very much.  Although i could bind a column with a string field name and even add sorts to one, none of the filter builders accept column names as strings.  This prevented me from pulling them into an extension.  I was able to use your code above to use filterbuilder instead since i can assign the filter by string that way.  Thank you!

 

0
Ray
Top achievements
Rank 1
answered on 23 Jul 2015, 12:38 PM
That seemed to did the trick. Thank you very much!
Tags
Grid
Asked by
Ray
Top achievements
Rank 1
Answers by
Alexander Popov
Telerik team
Ray
Top achievements
Rank 1
Share this question
or