javascript - How to filter table rows with jQuery - Stack Overflow

I have made a table here : fiddle but I seem unable to get my table filter to work. What I tried to do

I have made a table here : fiddle but I seem unable to get my table filter to work. What I tried to do was create a menu at the top where a specific filter would only show those rows. I tried to use .Data for this.

Filter Row Script

$('.filterMenu a').on('click', function(e){
  e.preventDefault();
  var c= $(this).data('qtype');
  $('#questTable')[0].className = c;
});

Row Hover Script

$(document).ready(function() {
    $('.table-row').hover(function() {             
        $(this).addClass('current-row');
        }, function() {
            $(this).removeClass('current-row');
        });
    });

Row hide Script

$(document).ready(function() {
    $('tr')
    .filter(':has(:checkbox:checked)')
    .addClass('selected')
    .end()
    .click(function(event) {
        if (event.target.type !== 'checkbox') {
            $(':checkbox', this).trigger('click');
        }
    })
    .find(':checkbox')
    .click(function(event) {
        $(this).parents('tr:first').toggleClass('selected');
    });    
});

I have made a table here : fiddle but I seem unable to get my table filter to work. What I tried to do was create a menu at the top where a specific filter would only show those rows. I tried to use .Data for this.

Filter Row Script

$('.filterMenu a').on('click', function(e){
  e.preventDefault();
  var c= $(this).data('qtype');
  $('#questTable')[0].className = c;
});

Row Hover Script

$(document).ready(function() {
    $('.table-row').hover(function() {             
        $(this).addClass('current-row');
        }, function() {
            $(this).removeClass('current-row');
        });
    });

Row hide Script

$(document).ready(function() {
    $('tr')
    .filter(':has(:checkbox:checked)')
    .addClass('selected')
    .end()
    .click(function(event) {
        if (event.target.type !== 'checkbox') {
            $(':checkbox', this).trigger('click');
        }
    })
    .find(':checkbox')
    .click(function(event) {
        $(this).parents('tr:first').toggleClass('selected');
    });    
});
Share Improve this question edited Aug 13, 2015 at 7:19 Alexis Tyler 9686 gold badges32 silver badges51 bronze badges asked Aug 13, 2015 at 6:18 RenierRenier 731 silver badge10 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

Here is a working example

The basic idea is to first hide all rows and then recursively show them, when they match your criteria.

Find all tr in tbody and hide them

var trs = $("#questTable").find("tbody tr");
trs.hide();

I would make use of the filter function

.filter(function (i, v) {})

to check if the row should be shown.

trs.filter(function (i, v) {
  if ($(this).data("qtype") == c) {
      return true;
  }
  if(c=="all"){
      return true;
  }
    return false;
})

//just show the row if it fits the criteria
.show();

Additionally I have fixed your typo msq -> mcq for the data-qtype in tr

Edit: Just updated the fiddle with more ments and fixed the thead area

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

相关推荐

  • javascript - How to filter table rows with jQuery - Stack Overflow

    I have made a table here : fiddle but I seem unable to get my table filter to work. What I tried to do

    8小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信