Using Or condition while filtering kendo grid

2019-08-31 17:14发布

问题:

I am using a kendo grid in my MVC application. Grid has two columns FirstName and LastName. Grid has a textbox where user can enter FirstName or LastName to search. I want to filter grid based on this criteria. Following is the code that i am currently using :

<script>
$(document).ready(function () {
    $("#FirstNameFilter").keyup(function () {

        var value = $("#FirstNameFilter").val();
        grid = $("#grid").data("kendoGrid");

        if (value) {
            grid.dataSource.filter({ field: "FirstName", operator: "contains", value: value });
        } else {
            grid.dataSource.filter({});
        }
    });
});
</script>

Where FirstNameFilter is the name of textbox where user can enter FirstName or LastName. This code is currently working if user enters FirstName. I want to have "OR" condition in the filter so that it will also search for LastName.

回答1:

$("#FirstNameFilter").keyup(function () {

        var value = $("#FirstNameFilter").val();
        grid = $("#grid").data("kendoGrid");
        var orfilter = { logic: "or", filters: [] };
        if (value) {
            //grid.dataSource.filter({ field: "FirstName", operator: "contains", value: value }); if searching FirstName only
            orfilter.filters.push({ field: "FirstName", operator: "contains", value: value }, { field: "LastName", operator: "contains", value: value });
            grid.dataSource.filter(orfilter);
        } else {
            grid.dataSource.filter({});
        }
    });