when using autocomplete without a json file in the url but calling a script that generate the json in return the results items are duplicating itself and showing instead of the searched word it shows all matches of all single letters of the searched word…
using this
url: “assets/request_cities.php”,
instead of
url: “assets/cities.json”,
I have big cities database to look for matches and I cannot use a json file for this but I need to consult the db to return the matching records… this is why I need to use .php script…
any solution for this behaviour ?
https://github.com/framework7io/framework7/blob/master/kitchen-sink/pages/autocomplete.html#L310
here the code
// Dropdown with ajax data
self.autocompleteDropdownAjax = app.autocomplete.create({
openerEl: "#destination-ajax", //link that opens autocomplete
multiple: false, //allow multiple values
valueProperty: 'id', //object's "value" property name
textProperty: 'name', //object's "text" property name
openIn: "popup", //open in page
preloader: true, //enable preloader
/* If we set valueProperty to "id" then input value on select will be set according to this property */
valueProperty: 'name', //object's "value" property name
textProperty: 'name', //object's "text" property name
limit: 20, //limit to 20 results
dropdownPlaceholderText: 'Try "JavaScript"',
source: function (query, render) {
var autocomplete = this;
var results = [];
if (query.length === 0) {
render(results);
return;
}
// Show Preloader
autocomplete.preloaderShow();
//alert('');
// Do Ajax request to Autocomplete data
app.request({
url: "assets/request_cities.php",
method: 'POST',
dataType: 'json',
//send "query" to server. Useful in case you generate response dynamically
data: {
query: query,
},
success: function (data) {
// Find matched items
console.log(data);
for (var i = 0; i < data.length; i++) {
if (data[i].name.toLowerCase().indexOf(query.toLowerCase()) >= 0) results.push(data[i]);
}
// Hide Preoloader
autocomplete.preloaderHide();
// Render items by passing array with result items
render(results);
}
});
}
});
in the php:
$query = $_POST[‘query’];
$results = $db->rawQuery("SELECT * from t_cities where Name like ‘%$query%’ ");
foreach ($results as $row)
{
$data []= array(
"id" => $row['cityID'],
"name" => $row['Name']
);
}
echo $json = json_encode($data,JSON_PRETTY_PRINT);