I'm using the twitter typeahead javascript library to pre-populate a search term to help the users searching for specific names. I'm using their example substringMatcher function which can be found here.
I'm populating my data using an Ajax call which returns the data I expect. This array is then passed to that example substringMatcher function however when searching it returns the entire array instead of each individual item (see image).
It should just return the individual name, not the array!
Here he is my typeahead function & source;
$('input#practition-typeahead').typeahead({
hint: true,
highlight: true,
minLength: 3,
},{
name: 'output',
source: substringMatcher(
jQuery.ajax({
type: "POST",
url: "/wp-admin/admin-ajax.php",
dataType: "json",
data: {
action: 'get_all_practitioners'
},
success:function(output){
return output;
}
})
)
});
My substring matcher function
var substringMatcher = function(strs) {
return function findMatches(q, cb) {
var matches, substringRegex;
// an array that will be populated with substring matches
matches = [];
// regex used to determine if a string contains the substring `q`
substrRegex = new RegExp(q, 'i');
// iterate through the pool of strings and for any string that
// contains the substring `q`, add it to the `matches` array
$.each(strs, function(i, str) {
if (substrRegex.test(str)) {
matches.push(str);
}
});
cb(matches);
};
};
Edit - When I console.log my output from the success of my ajax I get the following;
success:function(output){
console.log(output);
return output;
}
from Typeahead result population returns the entire array object


No comments:
Post a Comment