I have jQuery UI autocomplete input with AJAX source where I want to show the label in suggestions and textbox when selected, but I want the id for further search. How can I get the id behind the scenes so I can use it for further search, but keep showing label on front?
Also suggestions (results) are too many and shows a big scroll on the page, is there any way to restrict or show only few records at a time based on what user types? Here is my code so far: (so far suggestions/selection is working but its passing label/value for further search)
HTML:
<input type="text" name="CountyID" id="CountyID">
JSON:
[{"countyid":1302010,"name":"Fort Bend"},{"countyid":1011416,"name":"Houston"},{"countyid":1166827,"name":"Harris"}
jQuery/AJAX:
$("#ShowID").autocomplete({
source: function (request, response) {
var matcher = new RegExp( $.ui.autocomplete.escapeRegex(request.term), "i" );
$.ajax({
url: 'path/listCounty',
type: 'GET',
data: {State:StateID},
dataType: 'json',
success: function (data) {
response($.map(data.slice(0,10), function(v,i){
var text = v.name;
if ( text && ( !request.term || matcher.test(text) ) ){
return {
label: v.name,
value: v.name,
id: v.countyID
};
}
}));
}
});
},
minLength:2,
delay: 100,
select: function(event, ui) {
var e = ui.v;
var result = "value= " + e.id;
$("#ShowID").append(result);
}
});