Reputation: 305
I'm using textext plugin for autocomplete, auto-suggestion etc.
I have a Destination Field i.e textbox whenever user search for a Country or city, It should populate or suggest matched cities or countries in the drop-down.
For example, if users search as 'Ameri'
Current output
from the textext plugin is retrieving all the strings which contain the searched text 'Americ'
Expected output want the list of matched strings, which begins with the searched text 'Americ' result should come first, later searched text contains the list.
String begins with
Contains list
$('#textarea').textext({
plugins: 'tags autocomplete filter focus',
minLength: 3,
})
.bind('getSuggestions', function(e, data) {
if (data.query.length) {
var list = [
'United states of America',
'South America',
'India',
'Delhi',
'Pune',
'North America',
'American River ',
'American canyon'
],
textext = $(e.target).textext()[0],
query = (data ? data.query : '') || '';
$(this).trigger(
'setSuggestions', {
result: textext.itemManager().filter(list, query)
}
);
}
});
<textarea id="textarea" class="example" rows="1"></textarea>
Upvotes: 0
Views: 115
Reputation: 305
I've resolved the issue myself by adding some part of code over textext plugin, please correct me if I'm not in the correct way. Here is the demo for it - http://holidays.coxandkings.com/
$('#textarea')
.textext({
plugins : 'autocomplete filter',
useSuggestionsToFilter : true
})
.bind('getSuggestions', function(e, data)
{
var list = [
'United states of America',
'South America',
'India',
'Delhi',
'Pune',
'North America',
'American River ',
'American canyon'
],
textext = $(e.target).textext()[0],
query = (data ? data.query : '') || ''
;
// Implementing search functionality from the begining of the string, later dispalying the contains list.
var filterItems = textext.itemManager().filter(list, query);
var filteredResultCount = filterItems.length;
var beginingMatches = [];
var containsMatches = [];
for (var i=0; i < filteredResultCount; i++) {
if(filterItems[i].toLowerCase().indexOf(query.toLowerCase()) == 0 ) {
beginingMatches.push(filterItems[i]);
}else{
containsMatches.push(filterItems[i]);
}
}
var finalResult = beginingMatches.concat(containsMatches);
$(this).trigger(
'setSuggestions', { result : finalResult}
);
});
Upvotes: 0