admin管理员组

文章数量:1390531

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=".10.20/css/jquery.dataTables.min.css" rel="stylesheet" />
<script src=".3.1.js"></script>
<script src=".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>

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/1.10.20/css/jquery.dataTables.min.css" rel="stylesheet" />
<script src="https://code.jquery./jquery-3.3.1.js"></script>
<script src="https://cdn.datatables/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>

Share Improve this question edited Dec 2, 2020 at 22:25 davidkonrad 85.6k17 gold badges209 silver badges271 bronze badges asked Mar 30, 2020 at 23:34 Max PowersMax Powers 1,1995 gold badges28 silver badges58 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 2

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 pensate 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/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/reference/option/columns.type) :

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

本文标签: javascriptNested table with data tablesStack Overflow