New to Telerik UI for ASP.NET CoreStart a free 30-day trial

Implementing AutoComplete with Server Filtering in the Filter Component

Environment

ProductTelerik UI for ASP.NET Core Filter
Progress Telerik UI for ASP.NET Core versionCreated with the 2023.3.1114 version

Description

How can I define the AutoComplete component with server filtering as a custom editor of a specified Telerik UI for ASP.NET Core Filter field?

Solution

  1. Define a field within the Fields() configuration and use the EditorTemplateHandler() option to specify the JavaScript function that will return the AutoComplete editor.

    Razor
        @(Html.Kendo().Filter<Kendo.Mvc.Examples.Models.ProductViewModel>()
            .Name("filter")
            .ApplyButton(true)
            .DataSource("dataSource1")
            .Fields(f =>
            {
                f.Add(p=>p.ProductName).Label("Product Name").EditorTemplateHandler("productNameAutoCompleteEditor");
            })
        )
  2. Initialize the AutoComplete editor within the productNameAutoCompleteEditor handler.

  3. Handle the filtering event of the AutoComplete, extract the search entry value from the event data, and store it into a global variable searchParameter.

  4. Use the data option of the DataSource to pass the global variable as an additional parameter through the Read request of the AutoComplete. This way, you will ensure that the correct search entry will be sent to the server when the Filter component has multiple fields that use the same AutoComplete editor.

    Html
        function productNameAutoCompleteEditor(container, options) {
            var searchParameter = "";
            $('<input data-bind="value: value" name="' + options.field + '"/>')
            .appendTo(container)
            .kendoAutoComplete({
                minLength: 3,
                placeholder: "Please type at least 3 characters.",
                filter: "contains",
                filtering: function(e) {
                    searchParameter = e.filter.value; // Get the search entry from the event data.
                },
                dataTextField: "ProductName",
                dataSource: {
                    serverFiltering: true,
                    transport: {
                        read: {
                            url: '@Url.Action("GetItems","Home")',
                            data: function () {
                                return {
                                    search: searchParameter
                                };
                            }
                        }
                    }
                }
            });
        }

For a runnable example based on the code above, refer to the following REPL samples:

More ASP.NET Core Filter Resources

See Also