我正在尝试使用jQuery UI自动完成功能。在完成一些搜索之后,在简单编写我自己的代码之前,可能已经有了解决方案。jQuery UI自动完成多项搜索
我以前使用jQuery插件http://bassistance.de/jquery-plugins/jquery-plugin-autocomplete/,但出于各种原因想要迁移到集成在UI中的一个。
目前有两件东西让我质疑。
所选术语突出显示和多项术语搜索/选择以及如何实现这些术语。
下面是一个小提琴页面:http://jsfiddle.net/vapc2/来展示这个例子。 键入“蚂蚁”,它会照亮蚂蚁。但是如果我输入“蚂蚁吉尔希尔”,它应该选择并突出显示所有带有“蚂蚁”的条目,全部用“吉尔”和全部用“山”来代替,而不是像现在这样。基本上,如果一个术语出现在任何地方,在任何答案中,我希望返回该值并突出显示该术语,即使它不是完全匹配,也只是它包含在答案的某处。所以如果我输入“蚂蚁虫”,我想用这两个单词中的任何一个返回所有条目。
说我有下面的示例JSON对象:
var sampleAnswers = [
{
"id": "00450",
"label": "Ants, Ants on a hill near Jill",
"Category": "Ants",
"value": "hill ant"},
{
"id": "00450",
"label": "Ants, Ant not on a hill",
"Category": "Ants",
"value": "regular ant"},
{
"id": "00452",
"label": "Some ants on a hill",
"Category": "Ants",
"value": "hill ants"},
{
"id": "00454",
"label": "Red ants on a hill near Jill",
"Category": "Ants",
"value": "hill ant red"},
{
"id": "00470",
"label": "Bugs on a rug",
"Category": "bugs",
"value": "rug bug"},
{
"id": "00472",
"label": "rug bugs under the rug",
"Category": "bugs",
"value": "rug bug"},
{
"id": "69000",
"label": "bed bugs",
"Category": "bed",
"value": "bed bugs"},
{
"id": "69005",
"label": "Large complicated bed bugs",
"Category": "bugs",
"value": "bed bug"},
{
"id": "69020",
"label": "red bed bugs",
"Category": "bugs",
"value": "red bugs"}
];
function replaceWords(wordsy, text) {
//var re = '(' + words.join('|') + ')(?![^<]*(?:<\/script|>))',
var re = '(' + wordsy + ')(?![^<]*(?:<\/script|>))',
regExp = new RegExp(re, 'ig'),
sTag = "<span class='autoCompleteWord'>",
eTag = "</span>";
return text.replace(regExp, sTag + '$&' + eTag);
};
$("#tags").autocomplete({
minLength: 1,
source: sampleAnswers,
delay: 1000,
focus: function(event, ui) {
$("#cpt").val(ui.item.label);
return false;
},
open: function(event, ui) {
var myValue = $(this).val(); //get typed
$("ul.ui-autocomplete li a").each(function() {
var autoCompleteRow = $(this);
var htmlString = autoCompleteRow.html();
var words = myValue.split(" ");
$('#mesText2').val(myValue + ":" + words.length);
var i = words.length;
while (i--) {
htmlString = replaceWords(words[i], htmlString);
};
autoCompleteRow.html(htmlString);
});
},
select: function(event, ui) {
var hasValue = (ui.item.value != undefined && ui.item.value != "" && ui.item.value != null);
$("#bugid").val(ui.item.id);
$("#bugcategory").val(ui.item.Category);
$("#bug").val(hasValue ? ui.item.value : ui.item.label);
$("#bugLabel").val(ui.item.label);
return false;
}
}).data("autocomplete")._renderItem = function(ul, item) {
return $("<li></li>").data("item.autocomplete", item).append("<a>" + item.id + " <span class='autoCompCat'>" + item.Category + "</span> (Category)<br/>" + item.label + " <br/><span class='autoCompCpt'>" + item.value + "</span>" + "</a>").appendTo(ul);
};
标记:
<p><label class='mesFieldLabel'>
ya typed:</label><input id='mesText2' type='text' class='mesText' /></p><div class="ui-widget cptarea">
<label class='mesFieldLabel' for="tags">Tags:</label>
<input id="tags" /><span class='testCSS'>type 'ant' for example</span>
</div>
<div class='bugarea'>
<div>
<label class='mesFieldLabel'>
Code:</label><input id='bugid' maxlength="100" />
</div>
<div>
<label class='mesFieldLabel'>
Category:</label><input id='bugcategory' maxlength="100" />
</div>
<div>
<label class='mesFieldLabel'>
Bug:</label><input id='bug' maxlength="100" />
</div><div>
<label class='mesFieldLabel'>
Bug Label:</label><input id='bugLabel' maxlength="100" />
</div>
</div>
jQuery UI的最新版本,jQuery的1.6是首选项。
编辑: 我尝试使用下面的“源”:
source: function(request, response) {
var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
response($.each(sampleAnswers, function() {
var text = $(this).label;
if (this.value && (!request.term || matcher.test(text)))
return {
label: text.replace(
new RegExp(
"(?![^&;]+;)(?!<[^<>]*)(" +
$.ui.autocomplete.escapeRegex(request.term) +
")(?![^<>]*>)(?![^&;]+;)", "gi"
), "<strong>$1</strong>"),
value: text,
option: this
};
}));
},
但这返回结果不包括我的条件我列出类型的(如“红色臭虫”返回项目00450,00450,00452)我不想显示。 对于更新看到这个小提琴:http://jsfiddle.net/vapc2/1/
我相信这个问题是每个返回相同长度的阵列 - 我会尝试修复小提琴。 – Hogan 2011-06-13 20:22:23