Search code examples
asp.netteleriktelerik-gridtelerik-mvc

Can't loop throught filterDescriptors properly TelerikUi


So i have a TelerikUigrid and im trying to use serverside filtering and i have a very weird issue.

So when i filter 1 single column of the grid everything works as intended but when i filter 2 or more columns at the same time i encounter it doesn't work.

The problem happens because the my 2 filtered columns both get saved in a single object that is not loopable so in this example below if i filter 2 columns filter will have a count of 2 but is not loopable so i can't "split" the objects.

So when there is a single object in filter it works fine because there is only 1 to choose from but when there is 2 or more Visual Studio doesn't know which one it should choose so my variable remails a null.

if (request.filter != null && request.filter.Any())
{
    foreach(var filter in request.filter)
    {
        var filterDescriptor = filter as FilterDescriptor;
        if (filterDescriptor.Value != null)
        {
        //Code

        }
}

Solution

  • If you have two filters then the filter comes in as an object called CompositeFilterDescriptor.

    You'll need something like this:

    for (var i = 0; i < filters.Count; i++)
    {
        if (filters[i] is CompositeFilterDescriptor)
        {
            var outerCompositeFilter = (CompositeFilterDescriptor)filters[i];
    
            for (var j = 0; j < outerCompositeFilter.FilterDescriptors.Count; j++)
            {
                if (outerCompositeFilter.FilterDescriptors[j] is FilterDescriptor)
                {
                    // Do something with this filter
                }
            }
        }
    
        if (filters[i] is FilterDescriptor)
        {
            // Only 1 filter - do something with it
        }
    }