When initially create the AutoComplete, it invokes a transport.read
but there is no StartsWith
argument so it exits without returning anything.
You should either return an empty array:
if (!$q) {
echo "{\"data\":" .json_encode($arr). "}";
return;
}
or define the autocomplete to do not send a request until there is a minimum number of characters typed using minLenght:
$("#autocomplete").kendoAutoComplete({
dataSource : new kendo.data.DataSource({
transport: {
read: "autocomplet.php"
},
schema : {
data: "data"
}
}),
minLength : 1,
dataTextField: "nombre",
placeholder : "Please Select A State"
});
By default, AutoComplete does not use serverFiltering that means that it expects that you return everything from your PHP code and then the client filters it. So you should do:
$("#autocomplete").kendoAutoComplete({
dataSource : new kendo.data.DataSource({
serverFiltering: true,
transport : {
read: "autocomplet.php"
},
schema : {
data: "data"
}
}),
minLength : 1,
dataTextField: "nombre",
placeholder : "Please Select A State"
});
Next, the typed string is not sent as StartsWith
parameter but a little more complex string, something like filter: { logic: "and", filters: [ { field: "nombre", operator: "startswith", value: "typed" } ] }
. If you don't want to parse it, you can use parameterMap and then your AutoComplete definition would be something like:
$("#autocomplete").kendoAutoComplete({
dataSource : new kendo.data.DataSource({
serverFiltering: true,
transport : {
read : "autocomplet.php",
parameterMap: function (op) {
return { StartsWith: op.filter.filters[0].value };
}
},
schema : {
data: "data"
}
}),
minLength : 1,
dataTextField: "nombre",
placeholder : "Please Select A State"
});