jquery - Prioritize JavaScript array according to key value -
i have javascript array
var airports = [ { iata: "cpt", city: "cape town", airport: "cape town international", country: "south africa", priority: 9 }, { iata: "hla", city: "johannesburg", airport: "lanseria", country: "south africa", priority: 1 }, { iata: "jnb", city: "johannesburg", airport: "or tambo international", country: "south africa", priority: 9 }, ];
take note: final .js file has on 3 000 airports listed.
i'm trying autocomplete return prioritized results.
in example above, if user starts typing "johannesburg" must prioritize according "priority" value e.g. jnb, or tambo international should show above hla, lanseria.
currently autocomplete display results according listed in array.
a fiddle can found here: https://jsfiddle.net/cgaybba/17p7uyvf/
try code maybe useful you
var airportarray = [ { iata: "cpt", city: "cape town", airport: "cape town international", country: "south africa", priority: 9 }, { iata: "hla", city: "johannesburg", airport: "lanseria", country: "south africa", priority: 1 }, { iata: "jnb", city: "johannesburg", airport: "or tambo international", country: "south africa", priority: 9 }, { iata: "dur", city: "durban", airport: "king shaka international", country: "south africa", priority: 9 }, { iata: "bfn", city: "bloemfontein", airport: "bram fischer international", country: "south africa", priority: 9 }, { iata: "plz", city: "port elizabeth", airport: "port elizabeth airport", country: "south africa", priority: 8 }, { iata: "agz", city: "aggeneys", airport: "aggeneys airport", country: "south africa", priority: 6 }, { iata: "grj", city: "george", airport: "george airport", country: "south africa", priority: 7 }, { iata: "els", city: "east london", airport: "ben schoeman airport", country: "south africa", priority: 7 } ]; var airportarray = airportarray.sort(function(a, b) { return (b.priority || 0) - (a.priority || 0); }); $(function() { function custom_source(request, response) { var matcher = new regexp($.ui.autocomplete.escaperegex(request.term), "i"); response($.grep(airportarray, function(value) { return matcher.test(value.label) || matcher.test(value.value) || matcher.test(value.airport) || matcher.test(value.country); })); } $("#input_1_1").autocomplete({ minlength: 3, source: custom_source, ookuplimit: 3, clearcache: true, sortbeforelimit: function(suggestions) { var sorted = suggestions.sort(function(a, b) { alert(a.priority); return (a.priority || 0) - (b.priority || 0); }); return sorted; }, focus: function(event, ui) { // text inside input while selecting airport $("#input_1_1").val(ui.item.city + ', ' + ui.item.airport + ' (' + ui.item.iata + '), ' + ui.item.country); return false; }, select: function(event, ui) { // text inside input after selecting $("#input_1_1").val(ui.item.city + ' (' + ui.item.iata + ')'); $("#input_1_2").val(ui.item.iata); return false; } }) .autocomplete("instance")._renderitem = function(ul, item) { return $('<li>') .append('<div class="result-row">' + item.city + ', ' + item.airport + ' (' + item.iata + '), ' + item.country + '</div>') .appendto(ul); }; });
Comments
Post a Comment