【发布时间】:2021-06-23 20:41:08
【问题描述】:
我正在根据文档中的类别示例实现 jQuery UI 自动完成 我需要在 Jquery 中通过自动完成对结果进行排序(按字母顺序),首先在类别(酒店和城市)中,然后在 Itens(名称)中。示例:
城市
- 迈阿密
- 奥兰多
酒店
- A酒店
- 酒店B
任何帮助我走上正确的道路将不胜感激。
$( 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">
【问题讨论】:
标签: javascript jquery jquery-ui-autocomplete