简体   繁体   English

jQuery筛选器,显示与img最接近的li

[英]JQuery Filter that shows closest li with img

I have a List that contains a lot of list items and I want to create a custom filter which I have almost completed except for one issue. 我有一个包含很多列表项的列表,我想创建一个自定义过滤器,除了一个问题外,我几乎已经完成了。 Within the list there are different sections that act as headers and if there are any items under that header then the header list item should be shown as well. 列表中有不同的部分充当标题,如果该标题下有任何项目,则标题列表项目也应显示。

this is the format of the List: 这是列表的格式:

<ul id="forms-list">
   <li><a><img/></a></li> <-----This is a Header
   <li><a></a></li>
   <li><a></a></li>
   <li><a></a></li>
   <li><a></a></li>
   <li><a><img/></a></li> <-----This is a Header
   <li><a></a></li>
   <li><a></a></li>
   <li><a></a></li>
   <li><a></a></li>
</ul>

If there are no items under that header then the header should stay hidden. 如果该标题下没有任何项目,则标题应保持隐藏状态。

Here is the code that I currently have: 这是我目前拥有的代码:

$('#filter').keyup(function () {
    var rex = new RegExp($(this).val(), 'i');
    $('#forms-list li').hide();
    var $filtered = $('li').filter(function () {
        return rex.test($(this).text());
    });

    $filtered.each(function () {
        $(this).closest("li").has("img").show();
        $(this).show();
    });
});

Could be done more optimally but this probably works without diverging from your original code too much. 可以更优化地完成,但是这可能在不与原始代码相差太多的情况下起作用。 You would just need to organize the html a little differently if possible. 如果可能的话,您只需要稍微不同地组织html。 If you have thousands of these to filter, you probably would want to be a little more optimal and concise in searching. 如果您要筛选成千上万个这样的文件,则可能希望在搜索时更加优化和简洁。

http://jsfiddle.net/7tg39a9x/1/ http://jsfiddle.net/7tg39a9x/1/

<input type="text" id="filter" />

<ul class="filterable">
    <li class="header"><img src="http://icons.iconarchive.com/icons/famfamfam/silk/16/layout-header-icon.png" /> Header 1</li>
    <li>item</li>
    <li>item 2</li>
    <li>abc</li>
</ul>

<ul class="filterable">
    <li class="header"><img src="http://icons.iconarchive.com/icons/famfamfam/silk/16/layout-header-icon.png" /> Header 2</li>
    <li>item</li>
    <li>item 2</li>
    <li>abc</li>
</ul>

<ul class="filterable">
    <li class="header"><img src="http://icons.iconarchive.com/icons/famfamfam/silk/16/layout-header-icon.png" /> Header 3</li>
    <li>item</li>
    <li>item 2</li>
    <li>abc</li>
</ul>

And with some script 并带有一些脚本

$(function() {
    $('#filter').keyup(function () {
        var rex = new RegExp($(this).val(), 'i');
        $('.filterable li').hide();
        var $filtered = $('li').filter(function () {
            return rex.test($(this).text());
        });

        $filtered.each(function () {
            $(this).show();
            $(this).parent().find('li').first().show();
        });
    });
});

Try this ( demo ): 试试这个( 演示 ):

$('#filter').keyup(function () {
    var rex = new RegExp($(this).val(), 'i');
    $('#forms-list li').hide();
    var $filtered = $('li').filter(function () {
        return rex.test($(this).text());
    });

    $filtered.each(function () {
        $(this).prevUntil("li:has(img)").prev().show();
        $(this).show();
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM