下拉过滤器jquery数据表

时间:2012-03-08 11:20:19

标签: jquery datatables

这是我的代码:

$(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 datatables插件,它的工作方式与此示例完全相同:

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

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

所以我认为我需要改变:

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

但我不知道该放什么。非常感谢任何帮助,提前谢谢。

7 个答案:

答案 0 :(得分:18)

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

<select id="mySelect">
    <option value=""></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>

答案 1 :(得分:10)

如果您只需要在一列上进行操作

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

答案 2 :(得分:3)

也许时间已经改变,但是没有插件并且使用dataTables 1.10.12并且它是@api,作为评论建议中的人,您可以使用来自的基于零的索引整数对应表的数组。示例代码,重要位在下面的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

答案 3 :(得分:2)

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

这是数据表的第二级插件。

约万

答案 4 :(得分:2)

您可以使用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" }
                ]

        });
});

答案 5 :(得分:1)

我认为像下面这样的事情可能会成功

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

答案 6 :(得分:-1)

<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();
});
});
相关问题