嵌套 table 数据 tables

Nested table with data tables

我有以下嵌套 HTML table。我正在使用 Datatables API 使我的 tables 可搜索。我遇到的一个问题是嵌套的 tables 我不确定如何使嵌套的 tables 可搜索?我已尝试向嵌套的 tables HTML 代码添加额外的 ID 标记并将其添加到 dataTables JS 代码调用中,但这没有用。知道如何使 sub-table 可搜索吗?

<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>

如果您通过 "searchable" 表示 DataTables 中的 DataTables,您可以使用 createdRow 回调来初始化 <tr> 列中的 tables。

您必须有一个 columns 部分以补偿嵌套 table 中缺失的 <thead>(避免 TypeError: col is undefined错误):

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

createdRow 回调中初始化嵌套的 tables:

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

注意 dom 部分。这里只显示 table 本身和过滤框。你 可以用

删除 header
table.dataTable td table thead {
  display: none;
}

如果您想在嵌套的 table 中排序 table 列,请不要添加此 CSS。

使用相关标记的演示 -> http://jsfiddle.net/davidkonrad/8pzkr6yn/


更新。如果您只关心能够在嵌套的 table 的内容中进行搜索(例如 HTML 标记被删除),只需将相关列 type 定义为 'html' (https://datatables.net/reference/option/columns.type) :

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