DataTables 1.10: get column filter values

DataTables 1.10: get column filter values

FParsonsFParsons Posts: 18Questions: 3Answers: 0
edited July 2014 in Free community support

Hi! I have a page for which I constructed a filter input for each column. I've haven't used the new API until now. There was no problem converting the code from the old API, and the column filtering works fine with the new API. (BTW, it's initialized with var obj=$("#table").DataTables(options) so that 'obj' is the instance.)

However, I'd like to get the values of all filters applied to the datatable, not from the inputs, but from the DataTables instance. To get the global filter, "obj.columns().search()" works ( the manual says that "search()" without arguments gets the filter applied.) But I can't figure out how to get the filter for each column. The manual suggests that obj.column(idx).search() should work, but I seem to be missing something.

With the old API, I used a aoPreSearchCols object, something like this:

for(var i=0,j=objSettings.aoPreSearchCols.length; i<j; i++){
                  if(objSettings.aoPreSearchCols[i].sSearch!=""){
                      this.columnFiltersText+=(this.columnTitles[i] + " = '" + objSettings.aoPreSearchCols[i].sSearch + "'; ");
                  }
                };

I'm also not sure of an easy way to get the number of columns from the dataTables instance so I can loop through the indexes. I would have expect something like:

var colLength= ???
for var i=0, j=colLength,i<j; i++{
   (do something with) obj.column(i).search()
}

Can anyone point me in the right direction? Much Appreciated!

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,740Questions: 1Answers: 10,111 Site admin
    Answer ✓

    Thanks for pointing that out - a bug in DataTables! It was configured incorrectly for the column().search() method. I've committed a fix now, and here is a little example of it working as expected: http://live.datatables.net/foqehoq/2/edit

    Regards,
    Allan

  • FParsonsFParsons Posts: 18Questions: 3Answers: 0

    Thanks very much! I tried it and it works as expected! I did previously stringify the return of 'column(i).search' and noticed it was returning a huge object.

    One question: why is it necessary to do an each for table.columns().eq(0) instead of table.columns() ?

    Thanks again for your work in creating one of the most awesome and professional jquery plugins I've seen so far!

  • allanallan Posts: 61,740Questions: 1Answers: 10,111 Site admin

    One question: why is it necessary to do an each for table.columns().eq(0) instead of table.columns() ?

    Because the API is multi-table aware. The columns() function populates the data set for the API instance with what is basically a 2D array. The outer array for each table in the context (even if there is only one table) and the inner array for the column indexes for that table.

    In that way you can do things like tables.search( 'Allan' ); and have that search phrase applied to all tables in the content.

    The downside is that it does mean you need to put eq(0) or use flatten() now and then!

    Allan

This discussion has been closed.