DataTable在特定列上搜索 [英] DataTables search on specific columns

查看:184
本文介绍了DataTable在特定列上搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经根据需要调整了以下网页,并添加了一些代码,因此它现在具有select2下拉菜单,而不是常规选择菜单(我更喜欢它).但是我无法弄清楚如何修改代码,因此我只能在特定的列而不是每一列上使用它.任何指导将不胜感激.

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 (我改编的代码)

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

此外-如果可以选择隐藏通用"搜索框,我也希望将其隐藏在顶部,仅提供这些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.

(已编辑以更新此修补程序,因此select2将为每个下拉框初始化,最后一个未初始化)

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

推荐答案

您可以使用columns([array]).代替

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

,而您只想对第1,2,5和6列进行搜索:

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 () {

这篇关于DataTable在特定列上搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆