如何使用jQuery过滤表行

时间:2015-08-13 06:18:06

标签: javascript jquery html-table

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

过滤行脚本

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

行悬停脚本

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

行隐藏脚本

$(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');
    });    
});

1 个答案:

答案 0 :(得分:7)

Here是一个有效的例子

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

查找tr中的所有tbody并隐藏它们

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

我会使用过滤功能

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

检查是否应该显示该行。

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();

此外,我已修复您的拼写错误msq - > mc为tr中的data-qtype

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