【问题标题】:Filter LIs with text input - nested LIs not showing?使用文本输入过滤列表 - 嵌套列表未显示?
【发布时间】:2013-10-17 16:33:42
【问题描述】:

我正在尝试使用 keyup 文本输入过滤特定 LI 的 UL。问题是,LI 嵌套在树中,过滤器只能看到最顶部的 LI,并且似乎没有正确过滤。输入宾夕法尼亚州应该只显示宾夕法尼亚州,上面什么都没有。有任何想法吗?提前致谢。

http://www.jsfiddle.net/CDAVZ/412

HTML

<input type='text' value='[Enter search term followed by Return]' class='allW treeSearch' />
  <ul id="treeview">
    <li data-expanded="true"><span class="icon-location-7 md-moon delBlue treeSpace" data-icon="&#xe6b5;"></span>
    <span class="icon-location-7 md-moon white treeSpace" data-icon="&#xe6b5;"></span>Root
        <ul>
            <li data-expanded="true"><span class="icon-stack-6 md-moon delLtBlue treeSpace" data-icon="&#xe6a0;"></span>
            <span class="icon-stack-6 md-moon white treeSpace" data-icon="&#xe6a0;"></span>Gas Model
              <ul>
                  <li data-expanded="true"><span class="glyphicon glyphicon-globe md-moon delGreen treeSpace"></span>
                  <span class="glyphicon glyphicon-globe md-moon white treeSpace"></span>United States
                    <ul>
                        <li data-expanded="true"><span class="icon-pie md-moon delBlue treeSpace" data-icon="&#xe708;"></span>
                        <span class="icon-pie md-moon white treeSpace" data-icon="&#xe708;"></span>Pennsylvania

                        </li>
                    </ul>
                  </li>
              </ul>
            </li>
         </ul>
      </li>
  </ul>

jQuery

$('.treeSearch').click(function(){
    $(this).val(''); 
});

$('.treeSearch').keyup(function(){

    var searchText = $(this).val();

    $('#treeview ul').each(function(){

        var currentLiText = $(this).text(),
            showCurrentLi = currentLiText.indexOf(searchText) !== -1;

        $(this).toggle(showCurrentLi);

    });     
}); 

【问题讨论】:

标签: javascript jquery filter nested html-lists


【解决方案1】:

如果您不想更改 html,可以将 .toggle() 更改为 .css("visibility")

$('.treeSearch').click(function(){
    $(this).val(''); 
});
$('.treeSearch').keyup(function(){
    var searchText = $(this).val();
$('#treeview li').contents().filter(function() {
    return this.nodeType == 3;
}).each(function(){
var currentLiText = $(this).text();
    if(currentLiText.replace(/\s+/g, '')!=""){
        if(currentLiText.indexOf(searchText) !== -1){
            $(this).parent("li").css({"visibility": "visible"});
        }
        else{
         $(this).parent("li").css({"visibility": "hidden"});
        }
    }
});     
});    

http://jsfiddle.net/HLWMv/1/
这只会显示实际的“li”
要删除 if(currentLiText.replace(/\s+/g, '')!=""){ 部分,您需要删除 html 中的多余空格和新行
更新
不区分大小写

$('.treeSearch').click(function(){
$(this).val(''); 
});
$('.treeSearch').keyup(function(){

var searchText = $(this).val();

$('#treeview li').contents().filter(function() {
    return this.nodeType == 3;
}).each(function(){

    var currentLiText = $(this).text().toLowerCase();
        if(currentLiText.indexOf(searchText.toLowerCase()) !== -1){
            $(this).parent("li").css({"visibility": "visible"});
        }
        else{
         $(this).parent("li").css({"visibility": "hidden"});
        }
});     
}); 

http://jsfiddle.net/HLWMv/2/
我删除了 HTML 中的空格

【讨论】:

  • 这个可以不区分大小写吗?
  • 不幸的是,这个完美的解决方案不适用于我正在使用的 kendo ui 小部件。看来您无法显示/隐藏 li 的。 jsfiddle.net/CDAVZ/412
  • 要使用剑道,您需要像这样更改#treeview 的 ID http://jsfiddle.net/CDAVZ/413/
  • @AbrahamUribe,这可以不区分大小写吗?
  • @AbrahamUribe,漂亮。谢谢。
【解决方案2】:

注意:这会造成大量的 dom 操作......请注意与之相关的成本

据我了解,你需要改变dom结构来实现这个

$('.treeSearch').click(function () {
    $(this).val('');
});

RegExp.quote = function (str) {
    return (str + '').replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1");
};

$('#treeview li').each(function () {
    var $this = $(this);
    var text = $this.contents().map(function () {
        return this.nodeType == 3 && $.trim($(this).text()) != '' ? $.trim($(this).text()) : undefined;
    }).get().join(' ');

    $this.data('parent', $this.parent()).data('text', text);
})


$('.treeSearch').keyup(function () {

    var regex = new RegExp(RegExp.quote(this.value), 'i');

    var $selected = $('#treeview li').removeClass('selected').hide().filter(function () {
        return regex.test($(this).data('text'));
    }).addClass('selected').show();

    $selected.each(function () {
        var $this = $(this),
            $parent = $this.parent(),
            $ul = $this.data('parent');

        var $li = $this;
        while ($ul.is(':not(#treeview)') && !$ul.parent().hasClass('selected')) {
            $li = $ul.parent();
            $ul = $li.parent();
        }
        $this.appendTo($ul)
    })

});

【讨论】:

  • 你错过了 ",上面什么也没有。"
猜你喜欢
  • 2018-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-11
相关资源
最近更新 更多