DataTables过滤器选择关闭

时间:2014-06-25 05:53:13

标签: javascript jquery datatables

我正在使用jquery DataTables库来修改表格,并且我遇到了一些问题,过滤选择显示在错误的位置。

问题是我在'filter'元素上使用th类来指定是否应过滤列。但是,如果我跳过一列,其余部分就会出现故障。

因此,在下面(使用DataTables示例数据)and in this jsFiddle,不会过滤排名,而名称,办公室和年龄则是Office select以上位置,{{1年龄有办公室。

enter image description here

我正在使用的有问题的代码如下,但是heavily based on the DataTables filtering example如果我跳过它,我似乎无法正确排列选项。

select

2 个答案:

答案 0 :(得分:2)

你需要添加一个if条件来检查是否有类过滤器,jQuery就像

$("#nhpaCompare thead tr:first th").each( function ( i ) {

    if ($(this).hasClass("filter")) {       
        var select = $('<select><option value=""></option></select>')
            .appendTo( $(this).empty() )
            .on( 'change', function () {
                table.column( i )
                    .search( ''+$(this).val()+'')
                    .draw();
            } );

        table.column( i ).data().unique().sort().each( function ( d, j ) {
            select.append( '<option value="'+d+'">'+d+'</option>' )
        } );
    }
    } );

<强> Demo

答案 1 :(得分:0)

难道你不知道我遇到了所有的麻烦,并尝试了最后一件事......

i调用中的.each()只是迭代次数,而不是与实际列对应的数字。所以,虽然我会0,2,3,但它实际上是0,1,2

所以这个:

$("#nhpaCompare thead tr th.filter").each( function ( i ) {
    ...
} );

应改为:

$("#nhpaCompare thead tr:first th").each( function ( i ) {
    if ($(this).hasClass("filter")) {
        ...
    }
} );

It works fine in jsFiddle now.

enter image description here