Nested table with data tables

Viewed 9530

I have the following nested HTML table. I am using the Datatables API to make my tables searchable. One problem I have faced is with the nested tables I'm not sure how to make the nested tables searchable? I have tried adding additional ID tags to the nested tables HTML code and adding that into the dataTables JS code call but that did not work. Any idea how to make the sub-tables searchable?

<link href="https://cdn.datatables.net/1.10.20/css/jquery.dataTables.min.css" rel="stylesheet" />
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>


<script>
  $(document).ready(function() {
    $('#example').DataTable();
} );
</script>


<div class="container">
    <table id="example" class="table table-striped">
        <thead>
            <tr>
                <th>Image</th>
                <th>Date</th>
                <th>TC NAME</th>
                <th>NS FLOW</th>
                <th>SN FLOW</th>
                <th>NS FLOW</th>
                <th>SN FLOW</th>
                <th>NS FLOW</th>
                <th>SN FLOW</th>
            </tr>
        </thead>
        <tbody>

            <tr>
                <td>721</td>
                <td>3/15/20</td>

                <td>
                    <table id="example1" class="table table-nostriped">
                        <tr>
                            <td>TC1</td>
                        </tr>
                        <tr>
                            <td>TC2</td>
                        </tr>
                        <tr>
                            <td>TC3</td>
                        </tr>
                    </table>

                </td>

                <td>
                 <table class="table table-nostriped">
                        <tr>
                            <td>100</td>
                        </tr>
                        <tr>
                            <td>300</td>
                        </tr>
                        <tr>
                            <td>600</td>
                        </tr>
                    </table>
                </td>

                <td>
                 <table class="table table-nostriped">
                        <tr>
                            <td>100</td>
                        </tr>
                        <tr>
                            <td>800</td>
                        </tr>
                        <tr>
                            <td>400</td>
                        </tr>
                    </table>
                </td>

                <td>
                 <table class="table table-nostriped">
                        <tr>
                            <td>100</td>
                        </tr>
                        <tr>
                            <td>200</td>
                        </tr>
                        <tr>
                            <td>300</td>
                        </tr>
                    </table>
                </td>

                <td>
                 <table class="table table-nostriped">
                        <tr>
                            <td>100</td>
                        </tr>
                        <tr>
                            <td>300</td>
                        </tr>
                        <tr>
                            <td>200</td>
                        </tr>
                    </table>
                </td>


                <td>
                 <table class="table table-nostriped">
                        <tr>
                            <td>200</td>
                        </tr>
                        <tr>
                            <td>200</td>
                        </tr>
                        <tr>
                            <td>200</td>
                        </tr>
                    </table>
                </td>

                <td>
                 <table class="table table-nostriped">
                        <tr>
                            <td>100</td>
                        </tr>
                        <tr>
                            <td>200</td>
                        </tr>
                        <tr>
                            <td>300</td>
                        </tr>
                    </table>
                </td>

            </tr>





        <tr>
                <td>722</td>
                <td>3/16/20</td>

                <td>
                    <table class="table table-nostriped">
                        <tr>
                            <td>TC1</td>
                        </tr>
                        <tr>
                            <td>TC2</td>
                        </tr>
                        <tr>
                            <td>TC3</td>
                        </tr>
                    </table>

                </td>

                <td>
                 <table class="table table-nostriped">
                        <tr>
                            <td>200</td>
                        </tr>
                        <tr>
                            <td>300</td>
                        </tr>
                        <tr>
                            <td>600</td>
                        </tr>
                    </table>
                </td>

                <td>
                 <table class="table table-nostriped">
                        <tr>
                            <td>100</td>
                        </tr>
                        <tr>
                            <td>800</td>
                        </tr>
                        <tr>
                            <td>400</td>
                        </tr>
                    </table>
                </td>

                <td>
                 <table class="table table-nostriped">
                        <tr>
                            <td>100</td>
                        </tr>
                        <tr>
                            <td>200</td>
                        </tr>
                        <tr>
                            <td>300</td>
                        </tr>
                    </table>
                </td>

                <td>
                 <table class="table table-nostriped">
                        <tr>
                            <td>100</td>
                        </tr>
                        <tr>
                            <td>300</td>
                        </tr>
                        <tr>
                            <td>200</td>
                        </tr>
                    </table>
                </td>


                <td>
                 <table class="table table-nostriped">
                        <tr>
                            <td>200</td>
                        </tr>
                        <tr>
                            <td>200</td>
                        </tr>
                        <tr>
                            <td>200</td>
                        </tr>
                    </table>
                </td>

                <td>
                 <table class="table table-nostriped">
                        <tr>
                            <td>100</td>
                        </tr>
                        <tr>
                            <td>200</td>
                        </tr>
                        <tr>
                            <td>300</td>
                        </tr>
                    </table>
                </td>

            </tr>




        <tr>
                <td>633</td>
                <td>3/17/20</td>

                <td>
                    <table class="table table-nostriped">
                        <tr>
                            <td>TC1</td>
                        </tr>
                        <tr>
                            <td>TC2</td>
                        </tr>
                        <tr>
                            <td>TC3</td>
                        </tr>
                    </table>

                </td>

                <td>
                 <table class="table table-nostriped">
                        <tr>
                            <td>300</td>
                        </tr>
                        <tr>
                            <td>300</td>
                        </tr>
                        <tr>
                            <td>600</td>
                        </tr>
                    </table>
                </td>

                <td>
                 <table class="table table-nostriped">
                        <tr>
                            <td>100</td>
                        </tr>
                        <tr>
                            <td>800</td>
                        </tr>
                        <tr>
                            <td>400</td>
                        </tr>
                    </table>
                </td>

                <td>
                 <table class="table table-nostriped">
                        <tr>
                            <td>100</td>
                        </tr>
                        <tr>
                            <td>200</td>
                        </tr>
                        <tr>
                            <td>300</td>
                        </tr>
                    </table>
                </td>

                <td>
                 <table class="table table-nostriped">
                        <tr>
                            <td>100</td>
                        </tr>
                        <tr>
                            <td>300</td>
                        </tr>
                        <tr>
                            <td>200</td>
                        </tr>
                    </table>
                </td>


                <td>
                 <table class="table table-nostriped">
                        <tr>
                            <td>200</td>
                        </tr>
                        <tr>
                            <td>200</td>
                        </tr>
                        <tr>
                            <td>200</td>
                        </tr>
                    </table>
                </td>

                <td>
                 <table class="table table-nostriped">
                        <tr>
                            <td>100</td>
                        </tr>
                        <tr>
                            <td>200</td>
                        </tr>
                        <tr>
                            <td>300</td>
                        </tr>
                    </table>
                </td>

            </tr>

        </tbody>
    </table>
</div>

1 Answers

If you by "searchable" mean DataTables inside DataTables you can use the createdRow callback to initialize tables inside a <tr>'s columns.

You must have a columns section in order to compensate for the missing <thead> in the nested tables (avoiding the TypeError: col is undefined error) :

const columns = [
  { title: '' },
]

Init the nested tables in the createdRow callback:

let table = $('#example').DataTable({
  createdRow: function(row) {
    $(row).find('td table')
      .DataTable({
        columns: columns,
        dom: 'tf'
      })
  }
})

Notice the dom section. Here only showing the table itself and the filter box. You can remove the header with

table.dataTable td table thead {
  display: none;
}

Dont add this CSS if you want sortable columns in the nested tables.

demo using the markup in question -> http://jsfiddle.net/davidkonrad/8pzkr6yn/


Update. If your concern just is to be able to search within the content of the nested tables (e.g the HTML markup scraped away) just define the relevant columns type as 'html' (https://datatables.net/reference/option/columns.type) :

let table = $('#example').DataTable({
  columnDefs: [
    { targets: [2,3,4,5,6,7], type: 'html' },
  ],
  ...
})
Related