Using a dropdown list to filter a table (dataTables)

前端 未结 3 1696
被撕碎了的回忆
被撕碎了的回忆 2021-02-14 23:30

I\'m using the dataTables jQuery plugin (which is totally awesome), but I\'m having trouble getting my table to filter based on the change of my select box.

Function:

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-15 00:07

       $.extend( true, $.fn.dataTable.defaults, {
                "bFilter": true,
                    initComplete: function () {
                        this.api().column(1).every( function () {
                            var column = this;
                            var select = $('')
                                .appendTo( $(column.header()).empty() )
                                .on( 'change', function () {
                                    var val = $.fn.dataTable.util.escapeRegex($(this).val());
                                    column
                                        .search( val ? '^'+val+'$' : '', true, false )
                                        .draw();
                                } );
                            column.data().unique().sort().each( function ( d, j ) {
                                select.append( '' )
                            } );
                        } );
                    },
            } );
    

提交回复
热议问题