I am using the jQuery-Autocomplete library for an input field but instead of targeting local data for the suggestions I want to use serviceUrl
, which provides to the script a JSON file with the list of files. In the demo (lines 30-47) provided by the library author I then replace lookup: countriesArray,
with serviceUrl: 'retrieve_foreign_countries.php',
.
<?php // retrieve_foreign_countries.php
include 'init_db.php';
include 'functions.php';
prepareForeignCountriesJson($db);
?>
with prepareForeignCountriesJson()
defined as
function prepareForeignCountriesJson($db) {
$sql = 'SELECT country_code AS code, country_name AS value FROM foreign_country ORDER BY value;';
$stmt = $db->prepare($sql);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo "{\"query\": \"Unit\", \"suggestions\": ".json_encode($result)."}";
}
The lookup when entering some text works just fine BUT doesn't filter the result according to the input text and instead provides the whole list of countries. In other words the function (in the demo at lines 34-37)
lookupFilter: function(suggestion, originalQuery, queryLowerCase) {
var re = new RegExp('\\b' + $.Autocomplete.utils.escapeRegExChars(queryLowerCase), 'gi');
return re.test(suggestion.value);
},
doesn't kick in since, as explained by the library author (here)
When using serviceUrl option, it is server responsibility to filter results.
Then, where do I start to obtain with my serviceUrl
the same results of when loading local data? Do I need to filter the lookup data in my prepareForeignCountriesJson()
? But how many times prepareForeignCountriesJson()
is called? Every time the user enters some character in the field?
Aucun commentaire:
Enregistrer un commentaire