Search code examples
javascriptkendo-uikendo-asp.net-mvckendo-autocomplete

Custom filtering/sorting logic in Kendo Autocomplete


I can set a "startswith", "contains" or "endswith" filters for Kendo Autocomplete control, according to the docs:

The filtering method used to determine the suggestions for the current value. The default filter is "startswith" - all data items which begin with the current widget value are displayed in the suggestion popup. The supported filter values are startswith, endswith and contains.

Is there any way to setup my own filtering logic? Or at least way to sort the results? What do I need is a "contains" filter with results sorted according to relevancy to the query using my own logic.


Solution

  • You have to use sort.compare property to achieve that, e.g.:

    sort: {
        field: "FieldName",
        dir: "asc",
        compare: function(a, b) {
            return a.Relevancy > b.Relevancy;
        }
    }
    

    Demo.

    Note: It seems that it ignores the properties field and dir when you define compare, but you need to define it, otherwise it will ignore compare property as well.