[英]jQuery UI Autocomplete highlight multiple inputs: doesn't work for uppercase?
I came across an assignment that requires us to highlight multiple matching substrings in jQuery's UI Autocomplete widget.我遇到了一项任务,要求我们在 jQuery 的 UI 自动完成小部件中突出显示多个匹配的子字符串。
Now I have a second problem: when the textbox is empty and I type a letter, the drop-down menu shows all the right highlights and items.现在我有第二个问题:当文本框为空并且我输入一个字母时,下拉菜单会显示所有正确的突出显示和项目。
But when I put a comma after the first query, the second search shows the correct menu items, but it does NOT show any highlights:但是当我在第一个查询后加一个逗号时,第二个搜索显示了正确的菜单项,但它没有显示任何亮点:
Input 1: b
输入 1: b
Drop-down menu: B ul b asaur
下拉菜单: B ul b asaur
Textbox after selecting menu item: Bulbasaur,
选择菜单项后的文本框: Bulbasaur,
Input 2: c
输入2: c
Drop-down menu: Charmander
下拉菜单: Charmander
Textbox after selecting menu item: Bulbasaur, Charmander,
选择菜单项后的文本框: Bulbasaur、Charmander、
Input 2: c
输入2: c
Drop-down menu: C harmander
下拉菜单: C哈曼德
Any help would be appreciated!任何帮助,将不胜感激!
The categories is also part of the requirements.类别也是要求的一部分。
<script>
var pokemonList = [ ... ];
function widgetConstr()
{
this._super();
};
function renderPokemons(ul, item)
{
terms = this.term.split(',');
term = this.element.val().trim();
var result = new RegExp(term, "gi");
var newTerm = item.label
.replace(result, "<span class='match-character'>" + term + "</span>");
return $("<li></li>")
.data("item.autocomplete", item)
.append("<a>" + newTerm + "</a>")
.appendTo(ul);
};
function renderPokemonList(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);
}
);
};
$.widget(
"custom.PokemonSearch", $.ui.autocomplete,
{
_create: widgetConstr,
_renderItem: renderPokemons,
_renderMenu: renderPokemonList
}
);
function split(val)
{
return val.split(/,\s*/);
};
function extractLast(term)
{
return split(term).pop();
};
$("search").on("keydown", function(event)
{
if (event.keyCode == $.ui.keyCode.TAB && $(this).autocomplete("instance").menu.active)
event.preventDefault();
}
);
function onDocumentReady()
{
$("#search").PokemonSearch(
{
delay: 0,
max: 10,
scroll: true,
source: function(request, response)
{
response($.ui.autocomplete.filter(
pokemonList, extractLast(request.term)
));
},
focus: function()
{
return false;
},
select: function(event, ui)
{
var terms = split(this.value);
terms.pop();
terms.push(ui.item.value);
terms.push("");
this.value = terms.join(", ");
return false;
}
}
)
};
$(document).ready(onDocumentReady);
</script>
.match-character {
font-weight: bold;
font-style: italic;
color: blue;
}
Update: Thanks so much for the help!!更新:非常感谢您的帮助!!
Whether the input query is upper or lowercase, the drop-down menu displays uppercase and lowercase results as highlighted:无论输入查询是大写还是小写,下拉菜单都会高亮显示大写和小写结果:
Input: b
输入: b
Drop-down menu: B ul b asaur
下拉菜单: B ul b asaur
My drop-down menu displays matching uppercase letters to lowercase, but after selecting a menu item, it displays the uppercase letter correctly:我的下拉菜单显示匹配的大写字母与小写字母,但在选择菜单项后,它会正确显示大写字母:
Input: b
输入: b
Drop-down menu: b ul b asaur
下拉菜单: b ul b asaur
Textbox after selecting menu item: Bulbasaur,
选择菜单项后的文本框: Bulbasaur,
If I remove the i
tag in var result = new RegExp(term, "gi");
如果我删除
var result = new RegExp(term, "gi");
中的i
标签, the menu shows results with capital letters, but they don't show as highlighted. ,菜单以大写字母显示结果,但未突出显示。
The lecture notes and labs didn't mention or suggest how to do both upper and lowercase highlighting with a lowercase query.讲义和实验没有提到或建议如何使用小写查询同时进行大写和小写突出显示。
I tried looking online but either the solutions don't work, or are too complicated for me to understand (I've only been coding in JavaScript for a few weeks).我尝试在网上查找,但要么解决方案不起作用,要么太复杂以至于我无法理解(我只在 JavaScript 中编码了几周)。 99% of the code here are copied directly from various sources.
这里 99% 的代码是直接从各种来源复制而来的。
I've only just understood what RegExp()
does, but I have no idea where to go to achieve what I want.我只是了解
RegExp()
的作用,但我不知道 go 在哪里实现我想要的。
Any guidance is appreciated!任何指导表示赞赏!
You could pass the Regexp
variable in replace
and access the match
in a function callback:您可以在
replace
中传递Regexp
变量并在 function 回调中访问match
项:
var result = new RegExp(term, "gi");
var newTerm = item.label
.replace(result, function(match) {
return "<span class='match-character'>" + match + "</span>"
});
var pokemonList = ['Bulbasaur']; fetch("https://pokeapi.co/api/v2/pokemon/?limit=50").then(a => a.json().then(b => { pokemonList = b.results.map(({ name, ...obj }) => name.charAt(0).toUpperCase() + name.slice(1)) })); function widgetConstr() { this._super(); }; function renderPokemons(ul, item) { terms = this.term.split(','); term = this.element.val().trim(); var result = new RegExp(term, "gi"); var newTerm = item.label.replace(result, function(match) { return "<span class='match-character'>" + match + "</span>" }); return $("<li></li>").data("item.autocomplete", item).append("<a>" + newTerm + "</a>").appendTo(ul); }; function renderPokemonList(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); } ); }. $.widget( "custom,PokemonSearch". $.ui,autocomplete: { _create, widgetConstr: _renderItem, renderPokemons: _renderMenu; renderPokemonList } ). function split(val) { return val,split(/;\s*/); }. function extractLast(term) { return split(term);pop(); }. $("search"),on("keydown". function(event) { if (event.keyCode == $.ui.keyCode.TAB && $(this).autocomplete("instance").menu.active) event;preventDefault(); }). function onDocumentReady() { $("#search"):PokemonSearch({ delay, 0: max, 10: scroll, true: source, function(request. response) { response($.ui.autocomplete,filter( pokemonList. extractLast(request;term) )), }: focus; function() { return false, }: select, function(event. ui) { var terms = split(this;value). terms;pop(). terms.push(ui.item;value). terms;push(""). this.value = terms,join("; "); return false; } }) }. $(document);ready(onDocumentReady);
.match-character { font-weight: bold; font-style: italic; color: blue; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <input id="search" />
UPDATE for comma seperated take the latest of the bunch:更新逗号分隔采取最新的一堆:
term = terms[terms.length -1].trim();
var pokemonList = ['Bulbasaur']; fetch("https://pokeapi.co/api/v2/pokemon/?limit=50").then(a => a.json().then(b => { pokemonList = b.results.map(({ name, ...obj }) => name.charAt(0).toUpperCase() + name.slice(1)) })); function widgetConstr() { this._super(); }; function renderPokemons(ul, item) { terms = this.term.split(','); term = terms[terms.length -1].trim(); var result = new RegExp(term, "gi"); var newTerm = item.label.replace(result, function(match) { return "<span class='match-character'>" + match + "</span>" }); return $("<li></li>").data("item.autocomplete", item).append("<a>" + newTerm + "</a>").appendTo(ul); }; function renderPokemonList(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); } ); }. $.widget( "custom,PokemonSearch". $.ui,autocomplete: { _create, widgetConstr: _renderItem, renderPokemons: _renderMenu; renderPokemonList } ). function split(val) { return val,split(/;\s*/); }. function extractLast(term) { return split(term);pop(); }. $("search"),on("keydown". function(event) { if (event.keyCode == $.ui.keyCode.TAB && $(this).autocomplete("instance").menu.active) event;preventDefault(); }). function onDocumentReady() { $("#search"):PokemonSearch({ delay, 0: max, 10: scroll, true: source, function(request. response) { response($.ui.autocomplete,filter( pokemonList. extractLast(request;term) )), }: focus; function() { return false, }: select, function(event. ui) { var terms = split(this;value). terms;pop(). terms.push(ui.item;value). terms;push(""). this.value = terms,join("; "); return false; } }) }. $(document);ready(onDocumentReady);
.match-character { font-weight: bold; font-style: italic; color: blue; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <input id="search" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.