I am trying to use Twitter Bootstrap typeahead (v2.3.2) with jQuery v1.8.3 to search one subscriber user from big DB table with 20k users, and using many join and union with two or one more DBs.
I know sugests work fine but i want to limit ajax requests to server only by pressing submit button or enter.
My questions is how to start suggestions and search requests only when submit button or Enter is pressed.
My code:
$('input[name="searchSubs"]').typeahead({
minLength: 2,
highlight: true,
hint: true,
source: function (query, process) {
var postData = {'query': query};
$.extend( postData, {'search_field': '2'} );
return $.post('search/ajax_search/', postData,
function (response) {
var data = new Array();
$.each(response, function(i, item) {
data.push(item.subs_id +'_'+ item.firstname + ' ' + item.lastname);
});
return process(data);
},
'JSON'
);
}
, highlighter: function(item) {
item = item.replace(new RegExp('(' + this.query + ')', 'ig'), function ($1, match) {
return '<strong>' + match + '</strong>'
})
var parts = item.split('_');
var subs_id = parts.shift();
var itm = ''
+ "<div class='typeahead_wrapper'>"
+ "<div class='typeahead_primary'>" + parts.join('_') + "</div>"
+ "<div class='typeahead_secondary'>ID: " + subs_id + "</div>"
+ "</div>";
return itm;
}
, updater: function(item) {
var parts = item.split('_');
var subs_id = parts.shift();
window.location.href = 'home/?subs_id='+subs_id;
return parts.join('_');
}
, matcher: function (item) {
return ~item.toLowerCase().indexOf(this.query.toLowerCase())
}
}
);