Kendo Grid: Filter "1234 5678" by contains "1234" and contains "5678"

1 Answer 52 Views
Filter Grid
Cristian-Leonard
Top achievements
Rank 1
Iron
Cristian-Leonard asked on 19 Jul 2024, 08:50 AM

Hello,

I am currently using Kendo Grid MVC in .NET Core. My customer needs a way to search one column for multiple words.

For example: If I type in "123 789" it should return an entry with "123456789".

Is there a way to achieve this? Thanks,

Florence
Top achievements
Rank 1
commented on 22 Jul 2024, 10:31 AM

Hello, 

I've encountered a similar issue with Kendo Grid MVC in .NET Core where I needed to search a column for multiple words. If you got solution please  share with me.  choiceADVANTAGE com

Best regards,
florence023 

1 Answer, 1 is accepted

Sort by
1
Accepted
Alexander
Telerik team
answered on 24 Jul 2024, 07:26 AM

Hi,

Achieving the desired outcome would require a little more muscle. When it comes to filtering a value in a more dichotomous manner.

Based on the provided information alone, I am not fully certain whether server-side or client-side operations have been enabled on your side.

Regardless, a possible way to outmaneuver this behavior would be to intercept the change event of the search box. Considering the filtering happens from there.

Here is an exemplary configuration:

<script>
    $(".k-searchbox").on("change", function(e){
        var values = $(this).find("input").val().split(" "); // Split the values of the input.
        if(values.length > 0){
            var customFilter = { // Create a Composite filter object.
                logic: "or",
                filters: []
            };

            values.forEach(function(value){ // Traverse through each of the values.
                customFilter.filters.push({ // Push a raw filter.
                    field: "SerialNumber",
                    operator: "contains",
                    value: value
                })    
            })

            var grid = $("#grid").getKendoGrid();

            grid.dataSource // Clear the previous filter.
                .filter({});

            grid.dataSource
                .filter(customFilter); // Re-filter the Grid with the newly composed filter object.
        }
    })
</script> 

Which would produce the following result:

Here is a Telerik REPL that you can use as an anchor point during your endeavors:

I hope this helps.

Kind Regards,
Alexander
Progress Telerik

Do you have a stake in the designеr-developer collaboration process? If so, take our survey to share your perspective and become part of this global research. You’ll be among the first to know once the results are out.
-> Start The State of Designer-Developer Collaboration Survey 2024

Cristian-Leonard
Top achievements
Rank 1
Iron
commented on 16 Aug 2024, 08:16 AM

Thanks Alex for the code. It pointed me in the right direction.

I still have a strange issue. If I have 2 contains conditions, nothing is returned. I actually have a field that contains both 'T1' and 'RS' but nothing is shown. If I have only one contains condition, the filtering works as expected.

Alexander
Telerik team
commented on 20 Aug 2024, 02:13 PM

Hi,

It appears that the behavior is due to a peculiarity that is caused by the additionally employed custom JavaScript intervention. Which seems to be in the way of the built-in filtering mechanism. Leading to the predicament in which you have been placed in.

Generally, this is considered a more custom approach. And it is not cover all edge cases one can have when using the Grid. My personal recommendation here would be to consider decoupling the search input. As demonstrated in the following article. That way you will have full control over the filtering behavior:

Tags
Filter Grid
Asked by
Cristian-Leonard
Top achievements
Rank 1
Iron
Answers by
Alexander
Telerik team
Share this question
or