admin管理员组文章数量:1335543
Basically:
I have a page with a textbox, and a
<ul>
list below it. The<ul>
is populated by a list of the user's friends.The user begins typing in the name of a friend in the textbox, e.g pressing 'r'
I want to immediately update the
<ul>
with each keypress to show only those friends whose names begin with R, e.g 'Richard, Redmond, Raheem', etc.As the user types more, I want to further restrict the names, e.g if user types 'Ri' then I only want 'Richard' in the list.
I'm looking for ideas on how to implement the searching. Specifically, if I should use an Array or JSON class for storing the list of friends, if there's any regular expression I should use, etc?
Also which jQuery event should I use for listening to the keypress events?
Basically:
I have a page with a textbox, and a
<ul>
list below it. The<ul>
is populated by a list of the user's friends.The user begins typing in the name of a friend in the textbox, e.g pressing 'r'
I want to immediately update the
<ul>
with each keypress to show only those friends whose names begin with R, e.g 'Richard, Redmond, Raheem', etc.As the user types more, I want to further restrict the names, e.g if user types 'Ri' then I only want 'Richard' in the list.
I'm looking for ideas on how to implement the searching. Specifically, if I should use an Array or JSON class for storing the list of friends, if there's any regular expression I should use, etc?
Also which jQuery event should I use for listening to the keypress events?
Share Improve this question edited Apr 16, 2011 at 15:11 Felix Kling 817k181 gold badges1.1k silver badges1.2k bronze badges asked Apr 16, 2011 at 14:56 AliAli 267k268 gold badges591 silver badges785 bronze badges2 Answers
Reset to default 7Working example: http://jsfiddle/peeter/gAAth/
This is how I'd do it, I wouldn't duplicate data in an array.
An optimized version provide by Raynos: http://jsfiddle/gAAth/12/
example
Another option based on the @Peeter code .
HTML:
<input type="text" id="input1"/>
<ul id="list">
<li>Rich</li>
<li>Rajim</li>
<li>Andres</li>
<li>Jorge</li>
<li>Pedro</li>
...
<li>Raul</li>
<li>Paula</li>
<li>Delfina</li>
</ul>
CSS:
li.h {display:none;}
JS:
containsi selector
$.extend($.expr[':'], {
'containsi': function(elem, i, match, array)
{
return (elem.textContent || elem.innerText || '').toLowerCase()
.indexOf((match[3] || "").toLowerCase()) >= 0;
}
});
// first option
$("#input1").keyup(function() {
var search = $(this).val().toLowerCase();
$._list = ($._list) ? $._list : $("#list li"); //cache
$._list
.removeClass("h")
.filter(":not(:containsi('"+search+"'))").addClass("h");
});
EDIT
I think a little in the code written, I like the option to hide first and then display.
example
JS:
// second option
$("#input1").keyup(function() {
var search = $(this).val().toLowerCase();
$._list = ($._list) ? $._list : $("#list li");
$._list
.addClass(function(index, currentClass) {
var addedClass;
if (currentClass !== "h" )
addedClass = "h";
return addedClass;
}).filter(":containsi('"+search+"')").removeClass("h");
});
// third opcion
$("#input1").keyup(function() {
var search = $(this).val().toLowerCase();
$._list = ($._list) ? $._list : $("#list li");
$._list
.hide()
.filter(":containsi('"+search+"')").show();
});
本文标签: jqueryImplementing a Fast Javascript SearchStack Overflow
版权声明:本文标题:jquery - Implementing a Fast Javascript Search? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742396566a2467041.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论