DataTables search on specific columns

青春壹個敷衍的年華 提交于 2021-02-08 15:05:04

问题


I have adapted the following webpage to my needs and added a bit of code so it now has select2 dropdown menus instead of regular select menus (which I like a LOT better). However I have not been able to figure out how to adapt the code so I can use it only on specific columns instead of on every column. Any guidance would be greatly appreciated.

http://datatables.net/examples/api/multi_filter_select.html (code I adapted)

initComplete: function () {
        this.api().columns().every( function () {
            var column = this;
            var select = $("<select class='searchs2'><option value=''></option></select>")
                .appendTo( $(column.header()).append() )
                .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( '<option value="'+d+'">'+d+'</option>' )});
        });
        $(".searchs2").select2();
    }

Also - if there is an option to hide the 'generic' search box I would like to hide it on top as well and just provide these select2 boxes. Thank you.

(edited to update the fix so select2 would initialize for every dropdown box, last one was not initializing)


回答1:


You can use columns([array]). Instead of

this.api().columns().every( function () {

and you want to perform the search on the column 1,2,5 and 6 only :

this.api().columns([1,2,5,6]).every( function () {


来源:https://stackoverflow.com/questions/31304606/datatables-search-on-specific-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!