如何使用jQuery过滤表行

Ren*_*ier 3 javascript jquery html-table

我在这里制作了一张桌子:小提琴,但我似乎无法让我的桌面过滤器工作.我尝试做的是在顶部创建一个菜单,其中特定的过滤器只显示这些行.我试着用.Data它.

过滤行脚本

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

行悬停脚本

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

行隐藏脚本

$(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');
    });    
});
Run Code Online (Sandbox Code Playgroud)

blo*_*loC 7

这是一个有效的例子

基本思路是首先隐藏所有行,然后在符合条件时递归显示它们.

找到所有tr的tbody和隐藏

var trs = $("#questTable").find("tbody tr");
trs.hide();
Run Code Online (Sandbox Code Playgroud)

我会利用过滤功能

.filter(function (i, v) {})
Run Code Online (Sandbox Code Playgroud)

检查是否应该显示该行.

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();
Run Code Online (Sandbox Code Playgroud)

另外我修复了你的拼写错误msq - > mcq for tr中的data-qtype

编辑:刚刚用更多评论更新了小提琴并修复了thead区域