Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataTables search child row content

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

like image 870
cooltoast Avatar asked May 27 '15 00:05

cooltoast


People also ask

What is DataTable in HTML5?

An HTML table is used for storing all the details in rows and columns. In JavaScript part of the code, the DataTable is initialized using the plugin. On click, events are handled to show and hide more information for a particular data row.

How do I display the content of a child row?

This is implemented by using the API’s row.child.show () and row.child.hide () methods. There are other methods as well. The getChildRow () function in the following code defines the design/display content for the child row clicked by the user.

How to set the features of paging or searching in DataTable?

A DataTable is initialized. The developer can set the features of paging or searching as per the need as shown in the script part of the code. The column () API is used to select all the columns of the table.

How to show and hide a row in a DataTable in JavaScript?

In JavaScript part of the code, the DataTable is initialized using the plugin. On click, events are handled to show and hide more information for a particular data row. This is implemented by using the API’s row.child.show () and row.child.hide () methods.


1 Answers

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 using columns.visible option as shown below:

JavaScript:

    "columns": [
        {
            "class":          'details-control',
            "orderable":      false,
            "data":           null,
            "defaultContent": ''
        },
        { "data": "name" },
        { "data": "position" },
        { "data": "office" },
        { "data": "salary" },
        { "data": "extn", "visible": false }            
    ],

HTML:

<thead>
    <tr>
        <th></th>
        <th>Name</th>
        <th>Position</th>
        <th>Office</th>
        <th>Salary</th>
        <th>Extn.</th>
    </tr>
</thead>

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.

like image 62
Gyrocode.com Avatar answered Oct 24 '22 10:10

Gyrocode.com