I am implementing the jQuery UI Autocomplete based on the categories example in the documentation I need to sort (alphabetical order) the result by Autocomplete in Jquery, first in Categories (Hotel and Cities), then in the Itens (Names). Exemplo:
City
- Miami
- Orlando
Hotel
- hotel A
- hotel B
Any help starting me down the right path would be greatly appreciated.
$( function() {
$.widget( "custom.catcomplete", $.ui.autocomplete, {
_create: function() {
this._super();
this.widget().menu( "option", "items", "> :not(.ui-autocomplete-category)" );
},
_renderMenu: function( ul, items ) {
var that = this,
currentCategory = "";
$.each(items, function( index, item ) {
var li;
if (item.category != currentCategory) {
ul.append("<li class='ui-autocomplete-category'>" + item.category + "</li>");
currentCategory = item.category;
}
li = that._renderItemData( ul, item );
if ( item.category ) {
li.attr( "aria-label", item.category + " : " + item.label );
}
});
}
});
var data = function (request, response) {
$.ajax({
type : 'GET',
url: "http://commons.t4w.com.br/new/api/v1/mapping/autocomplete/all/"+ request.term +"/managerid/223/token/0b0f00da-30ec-4210-aca4-92aa6ea9470a",
success: function (data) {
response($.map(data, function (item) {
if (item.type == "City" || item.type == "Hotel") {
var label = item.name["pt-BR"];
return {
label: label,
destinationId: item.destinationId,
category: item.type
}
}
}));
}
})
};
$( "#search" ).catcomplete({
delay: 0,
source: data,
minLength: 3
});
} );
<link href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
City:
<input id="search">