javascript - Search and Filter Bootstrap Panels using jQuery - Stack Overflow

I have lots(30+) of Bootstrap panels, which are as follows (each has a different title and content):<

I have lots(30+) of Bootstrap panels, which are as follows (each has a different title and content):

<div class="panel">
    <div class="panel-heading">
        <h3 class="panel-title">Title</h3>
    </div>
    <div class="panel-body">
        Content
    </div>
    <div class="panel-footer">
        Footer
    </div>
</div>

And I have a search input

<input type="text" id="filter" placeholder="Filter Algorithms">

What I want to do it when someone types into the filter box it searches the title of the panels and filters them down as required. I have seen this done before, but I'm not quite sure where to start. This is the code I have so far:

$('#filter').keyup(function(){
    $('body').find('.panel-title').find($('#filter').val());
});

I have lots(30+) of Bootstrap panels, which are as follows (each has a different title and content):

<div class="panel">
    <div class="panel-heading">
        <h3 class="panel-title">Title</h3>
    </div>
    <div class="panel-body">
        Content
    </div>
    <div class="panel-footer">
        Footer
    </div>
</div>

And I have a search input

<input type="text" id="filter" placeholder="Filter Algorithms">

What I want to do it when someone types into the filter box it searches the title of the panels and filters them down as required. I have seen this done before, but I'm not quite sure where to start. This is the code I have so far:

$('#filter').keyup(function(){
    $('body').find('.panel-title').find($('#filter').val());
});
Share Improve this question asked Oct 8, 2013 at 12:06 Benedict LewisBenedict Lewis 2,8237 gold badges41 silver badges81 bronze badges 3
  • What do you mean when you say "filters them down as required"? What's the expected behaviour? – André Dion Commented Oct 8, 2013 at 12:09
  • Well, if a user enters foo in the input, it hides all panels that don't contain foo in the title. If they change it to bar, it shows all those with bar in the title etc. – Benedict Lewis Commented Oct 8, 2013 at 12:10
  • Ok, the hiding functionality is what I was after. – André Dion Commented Oct 8, 2013 at 12:10
Add a ment  | 

2 Answers 2

Reset to default 5

Try this:

var $panels = $('.panel');

$('#filter').on('keyup', function() {
    var val = this.value.toLowerCase();

    $panels.show().filter(function() {
        var panelTitleText = $(this).find('.panel-title').text().toLowerCase();
        return panelTitleText.indexOf(val) < 0;
    }).hide();
});

References

  1. jQuery.filter(): http://api.jquery./filter

this jquery plugin could be useful: https://github./Mixtags/jquery-filterbox

$('#list').filterbox({
        container: '.list-group',
        child: '.list-group-item',
        childKey: '.list-group-item > .title'
    });

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744749232a4591491.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信