gpt4 book ai didi

jquery - 通过对象数组的多个值过滤 Jquery AutoComplete

转载 作者:行者123 更新时间:2023-12-01 00:48:24 24 4
gpt4 key购买 nike

我想设置自动完成插件以仅选择列表中的有效员工。我有一个 Employee 对象数组,其中包含 EmployeeID 和 EmployeeName。目前,我已经通过将所有 Employee 的 EmployeeName 复制到数组中并将其提供给设置自动完成插件来加载 EmployeeName 的自动完成功能。

var employeeNames = new Array();
for (var i = 0 ; i < employees.length ; i++)
{
employeeNames[a] = employees.Employee[a].Name;
}
$("#txtEmployeeName").autocomplete(employeeNames, {
multiple: true,
mustMatch: true,
autoFill: true
});

它会完成这项工作,但我想要的是,如果用户想在此文本框中输入 EmployeeID,它也会加载对 EmployeeID 的建议过滤,尽管它会在建议中显示 EmployeeNames。有什么方法可以实现这一点,我记得我在某个地方看到过它,但不记得那个网站了。

最佳答案

如果您使用对象,jQueryUI 需要一个值和/或标签字段:

Multiple types supported:

Array: An array can be used for local data. There are two supported formats: An array of strings: [ "Choice1", "Choice2" ]

An array of objects with label and value properties: [ { label: "Choice1", value: "value1" }, ... ] The label property is displayed in the suggestion menu. The value will be inserted into the input element when a user selects an item. If just one property is specified, it will be used for both, e.g., if you provide only value properties, the value will also be used as the label.

来源:http://api.jqueryui.com/autocomplete/#option-source

考虑到这一点,您必须采用您的数据,以包含您刚刚分配给名称的 value 属性(例如 $.each(employees, function(){ this.value = this.name } ); ...)

现在您必须定义的另一件事是您想要如何过滤。这可以通过定义来完成。

示例:

    $("#txtEmployeeName").autocomplete({
source: function (request, response) {
var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");

var matching = $.grep(employees, function (value) {
var name = value.value;
var id = value.id;

return matcher.test(name) || matcher.test(id);
});
response(matching);
}
});

Fiddler 示例:http://fiddle.jshell.net/YJkTr/

完整代码:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Demo</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" />
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>


<script>
$(function () {
var employees = [
{ "value": "Tom", "id": "1" },
{ "value": "Stefan", "id": "2" },
{ "value": "Martin", "id": "3" },
{ "value": "Sara", "id": "4" },
{ "value": "Valarie", "id": "5" },
]

$("#txtEmployeeName").autocomplete({
source: function (request, response) {
var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");

var matching = $.grep(employees, function (value) {
var name = value.value;
var id = value.id;

return matcher.test(name) || matcher.test(id);
});
response(matching);
}
});
});
</script>
</head>
<body style="font-size: 62.5%;">
<div class="ui-widget">
<form>
<label for="txtEmployeeName">Developer:</label>
<input id="txtEmployeeName" />
</form>
</div>
</body>
</html>

关于jquery - 通过对象数组的多个值过滤 Jquery AutoComplete,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15267912/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com