【问题标题】:Dropdown filter jquery datatables下拉过滤 jquery 数据表
【发布时间】:2012-03-25 21:18:17
【问题描述】:

这是我的代码:

$(document).ready(function() {
    /* Initialise the DataTable */
    var oTable = $('#example').dataTable({
        "oLanguage": {
            "sSearch": "Search all columns:"
        },
        "iDisplayLength": 10,
        "bJQueryUI": true,
        "sPaginationType": "full_numbers",
        "bFilter": true,
    }); 

    /* Add a select menu for each TH element in the table footer */
    $("thead th").each( function ( i ) {
        this.innerHTML = fnCreateSelect( oTable.fnGetColumnData(i) );
        $('select', this).change( function () {
            oTable.fnFilter( $(this).val(), i );
        } );
    } );        
} );

我正在使用 jquery 数据表插件,它就像这个例子一样完美地工作:

http://www.datatables.net/release-datatables/examples/api/multi_filter_select.html

我想做的不是为每一列都有一个下拉列表,我只想在一个特定列上设置一个下拉列表。

所以我想我需要改变:

$("thead th").each( function ( i ) {

但我不知道该放什么。任何帮助将不胜感激,在此先感谢。

【问题讨论】:

  • 我认为使用 'i' 您可以控制要显示到哪一列

标签: jquery datatables


【解决方案1】:

您还可以创建自己的选择列表并将其放置在表格之外的任何位置。

<select id="mySelect">
    <option value="">Select</option>
    <option value="1">1</option>
    ...
</select>

<script>
    $('#mySelect').on('change',function(){
        var selectedValue = $(this).val();
        oTable.fnFilter("^"+selectedValue+"$", 0, true); //Exact value, column, reg
    });
</script>

【讨论】:

  • 我认为这是最好的答案和用法
  • 这假设您已经知道该列的值,有时情况并非如此
  • 但是如果bStateSave设置为true,刷新后就不会得到选中的值
  • var oTable = $('#example').DataTable(); $('#main_cat_select').on('change',function(){ var selectedValue = $(this).val(); oTable.column( 3 ) .search( selectedValue ) .draw(); });
  • 3 是列号。 @user9437856 你可以这样使用。
【解决方案2】:

如果你只需要一列,你可以这样做

var indexOfMyCol = 2;//you want it on the third column
$("thead th").each( function ( i ) {
    if(i === indexOfMyCol){ 
      this.innerHTML = fnCreateSelect( oTable.fnGetColumnData(i) );
      $('select', this).change( function () {
        oTable.fnFilter( $(this).val(), i );
      } );
    }
} );   

【讨论】:

  • 谢谢,想想就这么简单 :) 我可以给下拉菜单起个标题吗?喜欢....请选择
  • @Codded in fnCreateSelect 将 var r='&lt;select&gt;&lt;option value=""&gt;&lt;/option&gt;', 替换为 var r='&lt;select&gt;&lt;option value=""&gt;Please Select&lt;/option&gt;',
  • 我今天没有开机,本来可以解决这个问题的 :) 感谢您的帮助
  • 按字母顺序排列选择怎么样?
  • @Codded 在return asResultData; 之前的第 52 行,您应该对asResultData 进行排序
【解决方案3】:

也许时代变了,但是没有插件并且使用dataTables1.10.12@api,正如cmets中的人所建议的那样,您可以使用数组中的从零开始的索引整数用于相应的表。示例代码,重要位在下面的2 行。我正在搜索第 4 列,这是 coffeescript,但你明白了。

    $('#example').DataTable initComplete: ->
                    @api().columns([3]).every ->
                            column = this
                            select = $('<select><option value="">Sort Column(All)</option></select>').appendTo($(column.header()).empty()).on('change', ->
                                    val = $.fn.dataTable.util.escapeRegex($(this).val())
                                    column.search(val ? '^'+val+'$' : '', true, false).draw()
                                    return
                            )
                            column.data().unique().sort().each (d, j) ->
                                    select.append '<option value="' + d + '">' + d + '</option>'
                                    return
                            return
                    return

【讨论】:

    【解决方案4】:

    您可以使用日期表列过滤器,请参阅http://jquery-datatables-column-filter.googlecode.com/svn/trunk/customFilters.html

    它是数据表的二级插件。

    约万

    【讨论】:

      【解决方案5】:

      你可以使用 columnfilter 插件...

      $(document).ready(function(){
           $('#example').dataTable()
                .columnFilter({
                  aoColumns: [ { type: "select", values: [ 'Gecko', 'Trident', 'KHTML', 'Misc', 'Presto', 'Webkit', 'Tasman']  },
                           { type: "text" },
                           null,
                           { type: "number" },
                   { type: "select" }
                      ]
      
              });
      });
      

      【讨论】:

        【解决方案6】:

        我认为像下面这样的东西可能会解决问题

        $("thead th").each( function ( i ) {
            if(i==1)
            {
                this.innerHTML = fnCreateSelect( oTable.fnGetColumnData(i) );
                $('select', this).change( function () {
                    oTable.fnFilter( $(this).val(), i );
                } ); 
            }
        } );  
        

        【讨论】:

        • 谢谢,我已经使用了其他答案,但这也可以:)
        【解决方案7】:
        <select id="dropdown1">
        <option value="">Select</option>
        <option value="London">London</option>
        <option value="San Francisco">San Francisco</option>
        </select>
        
        $(document).ready(function() {
        var table = $('#example').DataTable();
        $('#dropdown1').on('change', function() {
        table.columns(1).search(this.value).denter code hereraw();
        });
        });
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多