0

Datasource is defined as:

var KendoDataSource_EmployeeAutoCompleteByFirstName = {
    serverFiltering: true,
    serverPaging: true,
    serverSorting: true,
    pageSize: 10,
    transport: {
        read: {
            url: '@Url.Action("GetEmployeesByFirstName", "Employee")',
            dataType: "json"
        }
    }
};

AutoComplete is defined as:

function KendoGridFilterAutoComplete(element, kendoDataSource, textField) {
    element.kendoAutoComplete({
        minLength: 3,
        filter: "startswith",
        dataSource: kendoDataSource,
        dataTextField: textField
    });
}

When using a kendoAutoComplete widget, the filter which is send by the datasource is like:

filter[logic]=and&
filter[filters][0][value]=smith&
filter[filters][0][operator]=startswith&
filter[filters][0][field]=LastName&
filter[filters][0][ignoreCase]=true

The JSON response from the server looks like:

[
    {"First":"Bill","LastName":"Smith"},
    {"First":"Jack","LastName":"Smith"},
    {"First":"ABC","LastName":"Smithy"}
]

This works fine, however as you can see I return multiple entries, so the kendoAutoComplete shows two the same entries (Smith) because the first-name differs.

So what I actually want is do distinct on the server, and return only the possible LastName, as an array of strings like this:

[
    "Smith",
    "Smithy"
]

However the kendoAutoComplete cannot handle this. It shows "undefined" or an error.

How to solve this ?

Stef Heyenrath
  • 9,335
  • 12
  • 66
  • 121

1 Answers1

0

I've create the following code:

#region AutoComplete
public virtual IQueryable GetAutoComplete(KendoGridRequest request)
{
    // Get filter from KendoGridRequest (in case of kendoAutoComplete there is only 1 filter)
    var filter = request.FilterObjectWrapper.FilterObjects.First();

    // Change the field-name in the filter from ViewModel to Entity
    string fieldOriginal = filter.Field1;
    filter.Field1 = MapFieldfromViewModeltoEntity(filter.Field1);

    // Query the database with the filter
    var query = Service.AsQueryable().Where(filter.GetExpression1<TEntity>());

    // Apply paging if needed
    if (request.PageSize != null)
    {
        query = query.Take(request.PageSize.Value);
    }

    // Do a linq dynamic query GroupBy to get only unique results
    var groupingQuery = query.GroupBy(string.Format("it.{0}", filter.Field1), string.Format("new (it.{0} as Key)", filter.Field1));

    // Make sure to return new objects which are defined as { "FieldName" : "Value" }, { "FieldName" : "Value" } else the kendoAutoComplete will not display search results.
    return groupingQuery.Select(string.Format("new (Key as {0})", fieldOriginal));
}

public virtual JsonResult GetAutoCompleteAsJson(KendoGridRequest request)
{
    var results = GetAutoComplete(request);
    return Json(results, JsonRequestBehavior.AllowGet);
}
#endregion

Which returns a unique list of anonymous objects which look like { "LastName" : "a" }.

Stef Heyenrath
  • 9,335
  • 12
  • 66
  • 121