如何在 Jquery UI 自动完成中搜索多个属性?
How to search across more than one attribute in Jquery UI Autocomplete?
我有一个对象数组,其结构类似于:
$scope.usersList = [{
"name": "John",
"email": "john@xyz.com",
"id": "abcd"
},{
"name": "Jane",
"email": "jane@xyz.com",
"id": "efgh"
}];
我在对服务器发出 ajax 调用后得到了数组。我目前在这样的 angular 应用程序中使用 jQuery UI 自动完成功能
$(input).autocomplete({
source: $scope.usersList,
appendTo: container,
position: {
at: 'left bottom',
of: container
},
select: function(e, v) {
e.preventDefault();
//Do something
}
});
如果数组只包含名称,上面的代码可以工作,但我也想搜索 "email" 和 "id" 字段。例如如果我在搜索框中键入 "abcd",我想在结果列表中看到 John 的名字。我无法弄清楚如何进行此操作。
能否请您尝试如下修改源代码:
$("#suggest").autocomplete({
delay: 100,
minLength:2,
**source: function (request, response) {
var temp = [];
usersList.filter(buildResponse);
function buildResponse(p){
for (var key in p) {
if (p.hasOwnProperty(key)) {
if(p[key].indexOf(request.term) > -1){
temp.push({
"label" : p[key],
"value" : p
});
}
}
}
}
console.log(temp);
response(temp);
}**,
select: function (event, ui) {
// Prevent value from being put in the input:
this.value = ui.item.label;
// Set the next input's value to the "value" of the item.
console.log(ui.item);
event.preventDefault();
}
});
//usersList = $scope.usersList(如您的问题所述)
jsfiddle: http://jsfiddle.net/32Bck/501/
经过一段时间的磕磕绊绊,我设法让搜索词成为任意 属性。这应该适用于您的情况(只需将 var usersList
替换为您的 collection):
<input id="in" type="input" class="ui-autocomplete-input" />
var usersList = [{
"name": "John",
"email": "john@xyz.com",
"id": "abcd"
}, {
"name": "Jane",
"email": "jane@xyz.com",
"id": "efgh"
}];
$("#in").autocomplete({
source: function(request, response) {
function hasMatch(s) {
return s.toLowerCase().indexOf(request.term.toLowerCase()) !== -1;
}
var i, l, obj, matches = [];
if (request.term === "") {
response([]);
return;
}
for (i = 0, l = usersList.length; i < l; i++) {
obj = usersList[i];
if (hasMatch(obj.name) || hasMatch(obj.email) || hasMatch(obj.id)) {
if ($.inArray(obj, matches) < 1) { // remove duplicates
matches.push({
"label": obj.name
//add here other properties you might need
})
}
}
response(matches);
}
}
});
在这里 fiddle 工作:https://jsfiddle.net/qg50zwv3/
我有一个对象数组,其结构类似于:
$scope.usersList = [{
"name": "John",
"email": "john@xyz.com",
"id": "abcd"
},{
"name": "Jane",
"email": "jane@xyz.com",
"id": "efgh"
}];
我在对服务器发出 ajax 调用后得到了数组。我目前在这样的 angular 应用程序中使用 jQuery UI 自动完成功能
$(input).autocomplete({
source: $scope.usersList,
appendTo: container,
position: {
at: 'left bottom',
of: container
},
select: function(e, v) {
e.preventDefault();
//Do something
}
});
如果数组只包含名称,上面的代码可以工作,但我也想搜索 "email" 和 "id" 字段。例如如果我在搜索框中键入 "abcd",我想在结果列表中看到 John 的名字。我无法弄清楚如何进行此操作。
能否请您尝试如下修改源代码:
$("#suggest").autocomplete({
delay: 100,
minLength:2,
**source: function (request, response) {
var temp = [];
usersList.filter(buildResponse);
function buildResponse(p){
for (var key in p) {
if (p.hasOwnProperty(key)) {
if(p[key].indexOf(request.term) > -1){
temp.push({
"label" : p[key],
"value" : p
});
}
}
}
}
console.log(temp);
response(temp);
}**,
select: function (event, ui) {
// Prevent value from being put in the input:
this.value = ui.item.label;
// Set the next input's value to the "value" of the item.
console.log(ui.item);
event.preventDefault();
}
});
//usersList = $scope.usersList(如您的问题所述)
jsfiddle: http://jsfiddle.net/32Bck/501/
经过一段时间的磕磕绊绊,我设法让搜索词成为任意 属性。这应该适用于您的情况(只需将 var usersList
替换为您的 collection):
<input id="in" type="input" class="ui-autocomplete-input" />
var usersList = [{
"name": "John",
"email": "john@xyz.com",
"id": "abcd"
}, {
"name": "Jane",
"email": "jane@xyz.com",
"id": "efgh"
}];
$("#in").autocomplete({
source: function(request, response) {
function hasMatch(s) {
return s.toLowerCase().indexOf(request.term.toLowerCase()) !== -1;
}
var i, l, obj, matches = [];
if (request.term === "") {
response([]);
return;
}
for (i = 0, l = usersList.length; i < l; i++) {
obj = usersList[i];
if (hasMatch(obj.name) || hasMatch(obj.email) || hasMatch(obj.id)) {
if ($.inArray(obj, matches) < 1) { // remove duplicates
matches.push({
"label": obj.name
//add here other properties you might need
})
}
}
response(matches);
}
}
});
在这里 fiddle 工作:https://jsfiddle.net/qg50zwv3/