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

Applying filter to a grid from server-side

1 Answer 18 Views
Grid
This is a migrated thread and some comments may be shown as answers.
subith
Top achievements
Rank 1
subith asked on 08 Jan 2021, 02:15 AM

Kendo.Mvc, Version=2020.3.1118.0

I am trying to apply filter to a grid from server-side. Below is the implementation. 

public static AjaxDataSourceBuilder<T> Configure<T>(this AjaxDataSourceBuilder<T> source, UserGridPreferenceVM userPreferences) where T : class
{
    if (userPreferences.Filter?.Filters != null && userPreferences.Filter.Filters.Length > 0)
    {
        List<FilterDescriptor> filters = new List<FilterDescriptor>();
 
        foreach (FilterElement filter in userPreferences.Filter.Filters)
        {
            filters.Add(new FilterDescriptor(filter.Field, GetFilterOperator(filter.Operator), filter.Value));
        }
 
        source.Filter(s =>
        {
            s.AddRange(filters);
        });
    }
 
    return source;
}

 

I have 2 questions.

1. When the grid loads the filter condition is set but value of the filter is not set.

2. How we can apply Logic operator "And"/"Or" to the filter?

 

Really appreciated if you can help with this issue.

1 Answer, 1 is accepted

Sort by
0
Georgi
Telerik team
answered on 12 Jan 2021, 01:31 PM

Hello Subith,

I would suggest you to add the filters through the API of the grid.

e.g.

@(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.OrderViewModel>()
    ...
    .DataSource(dataSource => dataSource
        .Ajax()
        .Filter(x=> x.Add(y=> y.Freight).IsGreaterThan(12).And().IsLessThan(25))
      ...
     )
)

This way, not only the data will be filtered, but the filter UI will reflect the applied filters. Additionally, the above configuration demonstrates how to chain filter expressions with AND/OR.

I hope this helps.

Regards,
Georgi
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
Grid
Asked by
subith
Top achievements
Rank 1
Answers by
Georgi
Telerik team
Share this question
or