dynamically set which columns are searchable after a dataTable has been initialized

dynamically set which columns are searchable after a dataTable has been initialized

lsidenlsiden Posts: 2Questions: 2Answers: 0
edited August 2014 in Free community support

Hi, I found a reply to my question here, http://datatables.net/forums/discussion/9665/dynamically-changing-filter-column-bsearchable, but it applied to version 1.9.3 and I'm on 1.10.1 now and it no longer works.

Is there still a way to dynamically set which columns are searchable after a dataTable has been initialized?

Answers

  • SmiledawggSmiledawgg Posts: 1Questions: 0Answers: 0
    edited November 2014

    I was looking for the solution to this situation as well. It would be nice to have a column().searchable() which behaves like 1.10's column().visible() function, but short of submitting a pull request (which I might do when I get some time), the following worked for me:

    $.fn.dataTable.Api.register("isColumnSearchable()", function(name) {
      var idx = table.column(name + ":name").index();
      return table.settings()[0].aoColumns[idx].bSearchable;
    });
    
    $.fn.dataTable.Api.register("setColumnSearchable()", function(name, value) {
      var idx = table.column(name + ":name").index();
      table.settings()[0].aoColumns[idx].bSearchable = value;
      return value;
    });
    

    I think these would be more "correct" if they operated chained to a column() call instead of taking a name parameter, but I wanted to hide the whole name + ":name" manipulation.

    Note that these rely on columns having a name value and assume a single table (the [0] index).

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    edited November 2014

    I should point out that this is using a private API and therefore not directly supported. However I don't see any reason why it wouldn't work.

    This is something that will be included in the core API in future as it has been asked for a few times recently.

    Allan

This discussion has been closed.