0

我有多个选择菜单,用于使用 jquery 数据表过滤表。

我使用的以下代码效果很好,但对于这个例子,我只使用了 3 个选择菜单。我现在有一张桌子将使用超过 10 个。

有没有更好的方法来写这个,所以我不必写匹配的每一个变化。

//更新

如果我将 select vars 和 tabledata 列 vars 放在数组中,我可以遍历它们。

$.fn.dataTableExt.afnFiltering.push(
    function( oSettings, aData, iDataIndex ) {
          if ( oSettings.nTable == document.getElementById( 'logtable' ))
          {

          var nature_of_complaint = document.getElementById('nature_of_complaint_select').value;
          var division = document.getElementById('division_select').value;
          var resolved = document.getElementById('resolved_select').value;

          var tabledata_nature_of_complaint = aData[22];
          var tabledata_division = aData[12];
          var tabledata_resolved = aData[26];

          if (nature_of_complaint == "" && division == "" && resolved == "")
          {  return true;  }
          else if (tabledata_division == division && nature_of_complaint == "" && resolved == "")
          {  return true;  }
          else if (tabledata_nature_of_complaint == nature_of_complaint && division == "" && resolved == "")
          {  return true;  }
          else if (tabledata_resolved == resolved && division == "" && nature_of_complaint == "")
          {  return true;  }
          else if (tabledata_nature_of_complaint == nature_of_complaint && tabledata_division == division && resolved == "")
          {  return true;  }
          else if (tabledata_division == division && tabledata_resolved == resolved && nature_of_complaint == "")
          {  return true;  }
          else if (tabledata_resolved == resolved && tabledata_nature_of_complaint == nature_of_complaint && division == "")
          {  return true;  }
          else if (tabledata_nature_of_complaint == nature_of_complaint && tabledata_division == division && tabledata_resolved == resolved)
          {  return true;  }
          return false;
          } else
          return true;
    }
);
4

1 回答 1

0

使用本教程弄清楚了。

http://www.flynsarmy.com/2011/12/save-custom-filter-state-jquery-data-tables/

只需将“dtable_filter”类添加到每个选择。

这是简化的代码:

$.fn.dataTableExt.afnFiltering = new Array();
    var oControls = $('#adv_search_filters').find(':input[name]');
    oControls.each(function() {
        var oControl = $(this);

        //Add custom filters
        $.fn.dataTableExt.afnFiltering.push(function( oSettings, aData, iDataIndex ) {
            if ( !oControl.val() || !oControl.hasClass('dtable_filter') ) return true;

            for ( i=0; i<aData.length; i++ )
                if ( aData[i].indexOf(oControl.val()) != -1 )
                    return true;

            return false;
        });
    });
于 2013-08-15T15:43:38.210 回答