DataTables - a dynamic set of columns for searching

I need to set the searchable property of a dynamic column (after the init table). Is there a solution? In case of use, I use the button extension to switch the visibility of the columns. But I will not search only visible columns.

0
source share
1 answer

I wanted to add “filter buttons” to the table. They should search (filter) only in one excluded and hidden and not searchable column.

  • excluded and hidden because the user should not see him.
  • not searchable because I wanted to exclude it from the global search.

dt. promises, , . thread, .

. :

    $.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;
    });

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

, DataTable, ( , ). , !

0

Source: https://habr.com/ru/post/1667428/


All Articles