在jQuery中对表应用多个选择过滤器

时间:2014-02-28 00:40:17

标签: javascript jquery filtering

我有三个选择框,用不同的标准过滤表格。例如,如果用户想要查看具有中等优先级的记录,则“是在空中关键”和“是”。关闭状态。 Closed是每行的数据属性。

我可以这样做但我的问题是如果用户想要再次选择一个过滤器,它只显示空白结果。这是javascript:

$("#input_filter_priority").change(function() {
        var data = this.value.split(" ");

        var $tr_row = $('#project_table').find("tr:not(.hidden)");
        var priority_column = $('#project_table').find("tr :not(.hidden) td:nth-child(5)");
        if (this.value == "") {
            $tr_row.show();
            return;
        }
        $tr_row.hide();

        if (filtered_row !== null) {
            filtered_row = filtered_row.filter(function() {
                $r = $(this);
                if ($r.is(":contains('" + data + "')")) {
                    return true;
                }
                return false;
            })
                .show();
        } else {
            filtered_row = $tr_row.filter(function() {
                $tr_text = $(this).val();
                $r = $(this);
                if ($r.is(":contains('" + data + "')")) {

                    return true;

                }
                return false;
            })
                .show();
        }


    });



    $("#input_closed_filter").change(function() {
        var data = this.value.split(" ");
        var $tr_row = $('#project_table').find("tr:not(.hidden)");

        if (this.value == "") {
            $tr_row.show();
            return;
        }
        $tr_row.hide();


        if (filtered_row !== null) {
            filtered_row = filtered_row.filter(function() {
                $r = $(this);
                if ($(this).attr('data-closed').match(data)) {
                    return true;
                }
                return false;
            })
                .show();

        } else {
            filtered_row = $tr_row.filter(function() {
                $tr_text = $(this).val();
                if ($(this).attr('data-closed').match(data)) {
                    return true;
                }
                return false;
            })
                .show();
        }


    });


    $("#input_on_air_filter").change(function() {
        var data = this.value.split(" ");

        var $tr_row = $('#project_table').find("tr ");
        var on_air_column = $('#project_table').find("tr td:nth-child(6)");
        if (this.value == "") {
            $tr_row.show();
            return;
        }
        $tr_row.hide();



        if (filtered_row !== null) {

            filtered_row = filtered_row.filter(function() {
                $r = $(this);
                if ($(this).attr('data-critical').match(data)) {
                    return true;
                }
                return false;
            })
                .show();
        } else {
            filtered_row = $tr_row.filter(function() {
                $tr_text = $(this).val();
                $r = $(this);
                if ($(this).attr('data-critical').match(data)) {
                    return true;
                }
                return false;
            })
                .show();
        }
    });

如果用户选择相同的过滤器两次或任意次,我应该怎么做(假设他改变了主意)。

以下是相关的JSBin

1 个答案:

答案 0 :(得分:1)

这是一个简化/工作版本:

var priorityFilterData=null;
var onAirCriticalFilterData=null;
var closedFilterData=null;


  function applyFilters(){
    var $tr_row = $('#project_table').find("tr");
    $tr_row.hide();//hide all rows by default

    //Show only the rows that meet each filter condition
    $tr_row.filter(function(){    
      var closedFilterCondition = (closedFilterData === null || $(this).attr('data-closed').match(closedFilterData));         
      var onAirFilterCondition = (onAirCriticalFilterData === null || $(this).attr('data-critical').match(onAirCriticalFilterData));            
      var priorityFilterCondition = (priorityFilterData === null || $(this).is(":contains('" + priorityFilterData + "')"));                 
      return closedFilterCondition && onAirFilterCondition && priorityFilterCondition;
    }).show();
  }


  $("#input_closed_filter").change(function() {
    closedFilterData = this.value.split(" ");
    applyFilters();
  });

  $("#input_on_air_filter").change(function() {    
    onAirCriticalFilterData = this.value.split(" ");
    applyFilters();
  });

 $("#input_filter_priority").change(function() {
    priorityFilterData = this.value.split(" ");
    applyFilters();
  });