我正在尝试使用jQuery UI自动完成功能。经过一番搜索,在简单地编写自己的解决方案之前,也许已经有了可用的解决方案。
我以前使用过jQuery插件http://bassistance.de/jquery-plugins/jquery-plugin-autocomplete/但是由于各种原因想要迁移到集成在UI中的那个。
目前有两项挑战我。
所选术语高亮显示和多个术语搜索/选择以及如何实现这些功能
这是一个小提琴页:http://jsfiddle.net/vapc2/以显示示例。输入"ants",它会高亮显示蚂蚁。但如果我键入"ants-jill-hill",它应该选择并突出显示所有带有"ant"、所有带有"jill"和所有带有"hill"的条目,而不是像现在这样什么都没有。基本上,如果一个术语出现在任何地方,在任何答案中,我都想返回该值并突出显示该术语,即使它不是完全匹配的,只要它包含在某个地方的答案中。因此,如果我键入"ant bug",我希望返回所有带有这两个单词中任一个的条目。
假设我有以下示例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
};
}));
},
但这返回的结果不包括我键入的术语列表(如"红色bug"返回的项目00450300452),我不希望显示这些术语。请参阅此小提琴了解更新:http://jsfiddle.net/vapc2/1/
好的,我解决了它返回所有结果而不过滤的问题
http://jsfiddle.net/vapc2/4/
以下是我所做的代码更改——需要注意的事项:
- 我把$(这个)改成了这个
- 我添加了一个结果数组,只有在测试通过时才添加(根据我最初的评论)
-
您的正则表达式不起作用,但我认为这超出了这个问题的范围。
source: function(request, response) { var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i"); var resultset = []; $.each(sampleAnswers, function() { var t = this.label; if (this.value && (!request.term || matcher.test(t))) { resultset.push( { label: t.replace( new RegExp( "(?![^&;]+;)(?!<[^<>]*)(" + $.ui.autocomplete.escapeRegex(request.term) + ")(?![^<>]*>)(?![^&;]+;)", "gi" ), "<strong>$1</strong>"), value: t, option: this }); } return; }); response(resultset); },