列筛选器不适用于行分组

时间:2015-09-24 05:40:57

标签: javascript jquery datatables

当我整合jQuery DataTables column filterrow grouping时,jQuery DataTables列过滤器无效。

我尝试了demo,但似乎在演示列过滤器中也无效。

1 个答案:

答案 0 :(得分:1)

  

<强>解

不再开发插件Row GroupingColumn Filtering,我不建议使用它们。使用DataTables选项和API方法执行行分组和单个列搜索,如Row grouping exampleIndividual column searching example所示。

// Setup - add a text input to each footer cell
$('#example tfoot th').each( function () {
    var title = $('#example thead th').eq( $(this).index() ).text();
    $(this).html( '<input type="text" placeholder="Search '+title+'" />' );
} );

// DataTable
var table = $('#example').DataTable({
    "order": [[2, 'asc']],
    "drawCallback": function (settings){
        var api = this.api();

        // Zero-based index of the column for row grouping
        var col_name = 2;

        // If ordered by column containing names
        if (api.order()[0][0] === col_name) {
            var rows = api.rows({ page: 'current' }).nodes();
            var group_last = null;

            api.column(col_name, { page: 'current' }).data().each(function (name, index){
                var group = name;

                if (group_last !== group) {
                    $(rows).eq(index).before(
                        '<tr class="group"><td colspan="6">' + group + '</td></tr>'
                    );

                    group_last = group;
                }
            });
        }
    }
});

// Apply the search
table.columns().every( function () {
    var that = this;

    $( 'input', this.footer() ).on( 'keyup change', function () {
        if ( that.search() !== this.value ) {
            that
                .search( this.value )
                .draw();
        }
    } );
} );    
  

<强>样本

请参阅this jsFiddle以获取代码和演示。