The DataTables search bar does not let me search for content within child rows.
I have searched extensively to find the answer to this (1, 2, 3, 4, 5, 6, 7, 8, 9), but there are little to no responses on the issue.
Here's a simple jsfiddle and DataTables debugger results.
I want to search the table for an extension number (which is in the child row), but typing one of the extension numbers into the search bar leaves no search results.
I tried the solution from this post, by adding this:
table.columns().every( function () {
var that = this;
var header = this.header();
$( 'input', this.footer() ).on( 'keyup change', function () {
that
.column( header.getAttribute('data-search-index')*1 ) // *1 to make it a number
.search( this.value )
.draw();
} );
} );
...but it still doesn't work, as you can see in the jsfiddle linked above.
Can someone please help me out?
Thanks
SOLUTION
In order for jQuery DataTables to search child rows you need to add data displayed in the child rows to the main table as hidden columns.
For example, you can add hidden column for
extn
data property usingcolumns.visible
option as shown below:JavaScript:
HTML:
DEMO
See this jsFiddle for code and demonstration. Search for
5407
and the first row will be shown even though data appears only in child row.if you have a list of extensions in one column then you would want to split them like this.
I have to ask : What make you believe you can search in child row content you are injecting dynamically only when the child rows are shown? And how should a
column()
search could cover content from other rows, ever?When this said, there is of course a workaround. Instead of creating the child row content over and over, keep it in an array :
Now, when you are initialising the table, you initialise the child row content as well :
In the format() function, add a class to the Extension Number field for easy access :
When you show child rows, insert the prerendered content from
details[]
instead of callingformat()
:Create a filter that returns only rows which have a
details[]
child row holding a certain Extension Number :Use that custom filter instead of the
column()
search in your input handlers :forked fiddle -> https://jsfiddle.net/7o67vhrz/
Update. To apply the above filter to the general searchbox :
yet another forked fiddle -> https://jsfiddle.net/ds3qp41g/
Last example. Combine details search and "native" search
fiddle -> https://jsfiddle.net/h2u4fowj/
This is quite an old thread, and the accepted answer does work, but I wanted to propose an alternate solution.
I was having the same issue not being able to search within child rows, and my solution was to make a hidden
<td>
on the end of my table that contained the data in the child rows - this way, the indexer sees it but the user does not.For the limited HTML, I added a new column:
Then, within the DataTables call:
Then you just need to hide this column. You could do this either through DataTables' recommended method:
https://datatables.net/examples/basic_init/hidden_columns.html
or through the method I chose:
You're left with one
<td>
at the end of your table that contains everything from the child rows, but it's invisible, and it works with the search box/filter thingy.