DataTables-动态设置可搜索的列 [英] DataTables - dynamically set columns searchable

查看:374
本文介绍了DataTables-动态设置可搜索的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要动态设置列的可搜索属性(在表格后)在里面).有解决方案吗?用例是,我使用按钮扩展来切换列可见性.但是我不希望只搜索可见列.

I need to set the searchable property of a column dynamicaly (after table init). Is there a solution? The use-case is, that I'm using the button extension to toggle column-visibility. But I wan't that only visibile columns are searched.

推荐答案

我想在表中添加过滤器按钮".他们应该只在一个排除的&中搜索(过滤)隐藏的无法搜索的列.

I wanted to add "filter buttons" to the table. They should search(filter) just in one excluded & hidden & not-searchable column.

  • 排除在&隐藏,因为用户不应该看到它
  • 不可搜索,因为我想将其从全局搜索中排除.

在多个支持论坛中dt.作者承诺实现此功能,但直到今天他还没有实现.但是我确实找到了线程如何自行完成.

In multiple support-forums the dt. author promises to implement this functionality, but until this day he didn't. But I did find a thread how you can do it on your own.

但是您必须对其进行一些更改.我的版本:

But you would have to change it up a bit. My Version:

    $.fn.dataTable.Api.register("isColumnSearchable()", function(colSelector) {
        var idx = this.column(colSelector).index();
        return this.settings()[0].aoColumns[idx].bSearchable;
    });

    $.fn.dataTable.Api.register("setColumnSearchable()", function(colSelector, value) {
        if(value!==this.isColumnSearchable(colSelector)) {
            var idx = this.column(colSelector).index();
            this.settings()[0].aoColumns[idx].bSearchable = value;
            if(value===true)
             this.rows().invalidate();
        }
        return value;
    });

我的setFilterFunction:

My setFilterFunction:

function setFilter(table,col,value){
    if(value== undefined || value=="" || value==0) {
        value = "";
        table.rows().invalidate();
    }
    else {
        value = "\\b" + value + "\\b";
    }
    var oldsearchable = table.isColumnSearchable(col);
    if (!oldsearchable)
        table.setColumnSearchable(col, true);
    table.column(col).search(value,true).draw();
   if (!oldsearchable)
        table.setColumnSearchable(col, false);
} 

重要的是

this.rows().invalidate();

this.rows().invalidate();

这是非常昂贵的,但是您必须清除DataTable的内部缓存才能使解决方法起作用(还要使过滤器按钮和全局搜索协同工作). 我真的希望该功能尽快集成到内核中!

this is very costly, but you would have to clear the internal cache of the DataTable in order for the workaround to work (Also in order for filter Buttons and global search to work together). I really hope this functionality get integrated into the core soon!

这篇关于DataTables-动态设置可搜索的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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