Search code examples
kendo-uimulti-selectkendo-multiselect

Kendo MultiSelect: Search on multiple fields


I'm working with kendo Multiselect and I would like to find a way to search in multiple fields of my data source. Here is my actual code. but it works only for one field:

`
$scope.dataList = new kendo.data.DataSource({
    data:[{id: "1",name: "Doe, John",email: "John.Doe@example.com"}],
});
$scope.customOption = {
                dataSource: $scope.dataList,
                dataTextField: "name",
                dataValueField: "id",
                filter: "contains", 
                itemTemplate: '<span>#=id#</span>#=name#<i> #=email#</i>',
}
`

As you see I'm also using AngularJS, I try to search for names and emails.


Solution

  • $("#id").kendoMultiSelect({
            placeholder: "Select products...",
            dataTextField: "name",
            dataValueField: "id",
            autoBind: false,
            filtering: function (e) {
                if (e.filter) {
                    var value = e.filter.value
                    var newFilter = {
                        filters: [
                            { field: "id", operator: "contains", value: value },
                            { field: "name", operator: "contains", value: value },
                            { field: "email", operator: "contains", value: value }
                        ],
                        logic: "or"
                    }
                    e.sender.dataSource.filter(newFilter)
                    e.preventDefault()
                }
                e.preventDefault()
            },
            dataSource: {
                data:[
                {id: "1",name: "Doe, John",email: "John.Doe@example.com"},
                {id: "2",name: "sss, John",email: "sss.Doe@example.com"},
                {id: "3",name: "fff, John",email: "fff.Doe@example.com"},
                {id: "4",name: "ccc, John",email: "ccc.Doe@example.com"}
                ],
            },
            value: [
                {id: "1",name: "Doe, John"},
            ]
        });
    

    Refer to API Documentation of Kendo DataSource and MultiSelect. hope it helps.