How to filter Grid on a column that is a list?
2 Answers 24 Views
Jason
Top achievements
Rank 1
Jason asked on 07 Jun 2021, 06:26 PM | edited on 10 Jun 2021, 04:11 PM

I populate the Grid with ~5000 results from a database. The Grid does the paging, sorting, and filtering in memory, except it won't filter for this one column that is a List<string> CustomerNamesList. I'm attempting to use the FilterMenuTemplate -- I just need a textbox and a dropdown like "Contains" and "Does Not Contain" but I can't figure out how to filter the Grid.

<GridColumn Field="@(nameof(CustomerNamesList))" Title="Customers" Sortable="false" Filterable="true">
    <FilterMenuTemplate>
        <label for="NameMenuFilter">Customer Name:</label>
        <TelerikTextBox Id="NameMenuFilter" ValueChanged="@((value) => UpdateCustomerNameFilter(value))"> 
        </TelerikTextBox>
    </FilterMenuTemplate>
    <Template>
        @* display - loop thru List of customer names *@ 
    </Template>
</GridColumn>

 

This code gets hit, but it's not doing anything...

public void UpdateCustomerNameFilter(string itemValue)
{
        var filter1 = partyNamesFilterContext.FilterDescriptor.FilterDescriptors[0] as FilterDescriptor;
        filter1.Value = itemValue;
        filter1.Operator = FilterOperator.Contains;
}


When I click Filter, it gives me a blazor.server.js error
Error: System.ArgumentException: Provided expression should have string type (Parameter 'stringExpression')

 

Update

Here's what I'm using as the datasource to make things clearer: IEnumerable<Companies>
So what it appears to me is that the Grid doesn't know how to filter on a List of objects like this List<string> 

public class Companies {
public int Id {get; set;} public List<string> CustomerNamesList {get; set;} = new List<string>();
// Other fields }


2 Answers, 1 is accepted

Sort by
0
Hristian Stefanov
Telerik team
answered on 10 Jun 2021, 03:18 PM

Hi Jason,

I tried to reproduce the described scenario on my end. It seems I cannot get the same result.

The problem might come from those little parts missing (like the Template content) from the provided example. I have also changed a little bit the Filed of the column from "@(nameof(CustomerNamesList))" -> "@(nameof(Sample.CustomerNamesList))", but this might be just a typo.

In order for me to be on the same page as you, could you please try to send me a little reproduction or a little more code snippets? This will be really helpful for us, and I will be able to investigate further.

By the way, you can see an example of an approach using FilterMenuTemplate in our demo here: https://demos.telerik.com/blazor-ui/grid/custom-filter-menu. You may find really useful additional information about your case.

Thank you.

Regards,
Hristian Stefanov
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

I've updated the question to make source clearer. I'm trying to filter on a list of strings, so I'm wondering if that is supported or not. The examples all use predefine types like String, Int, Date, and Bool.
- by
Jason
Top achievements
Rank 1
on 10 Jun 2021, 04:10 PM | edited
I'd also like to know if there's a recommended way to filter a column with a list<string> in it.
- by
John
Top achievements
Rank 1
Iron
on 11 Jun 2021, 03:22 PM | edited
0
Hristian Stefanov
Telerik team
answered on 14 Jun 2021, 03:34 PM

Hello Jason,

I apologize for the misunderstanding. You are correct that in a Grid column field we support only the predefined types like the ones we use in our examples (string, int, date, bool..). This is the reason that causes a System.ArgumentException error.

The desired result can be achieved with the following approaches:

  • Use the OnRead event to perform the filtering inside of it. This will let you customize your filter.

I hope this helps. If you have any other questions, please let me know.

Regards,
Hristian Stefanov
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Thanks for that confirmation. But you are saying it IS possible to have Telerik Grid filter a List<string> in either approach? I understand OnRead allows me to go retrieve a newly filtered result from the database or even in memory, but I was hoping we could get Telerik to do its own filtering.

I would be intrigued if I could do it using FilterMenuTemplate to filter the List<string>, but your demo only shows using string, int, date, bool. The example shows the use of FilterDescriptors and I'm wondering if I'm only a couple lines of code away from filtering on a List<string> ??

My only viable option right now is to get rid of the List<string> which reduces the usefulness of the Grid.
- by
Jason
Top achievements
Rank 1
on 14 Jun 2021, 04:12 PM
Tags
Grid
Asked by
Jason
Top achievements
Rank 1
Answers by
Hristian Stefanov
Telerik team
Share this question
or