【问题标题】:Properly formatted table throw error for DataTablesDataTables 的格式正确的表抛出错误
【发布时间】:2019-07-08 20:19:40
【问题描述】:

我们正在使用Datatables Bootstrap 4 库来格式化我们的表格。即使我们有正确格式化的表格,在页面加载后我们仍然会收到以下错误消息。

我在下面添加了 JS 和 HTML,但代码也可以在 codepen.io 上找到。

  • codepen 上的代码已更新(2018 年 2 月 14 日)以反映解决方案,即如果一个或多个表在单个页面上需要它,则所有表都具有 <thead>

我们如何解决错误信息?

目标

  1. .enable-dt 元素内的所有表都应启用DataTable
  2. 我们正在尝试复制 Multiple tables 功能。

无法读取未定义的属性“mData”

JavaScript

$(function () {
    $('.enable-dt table').addClass('table table-striped table-bordered nowrap');
    $('.enable-dt table').DataTable({
        'paging': false,
        'searching': false,
    });
});

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="//cdn.datatables.net/1.10.19/css/dataTables.bootstrap4.min.css" media="all">
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.1/css/bootstrap.css" media="all">
<div class="enable-dt">
    <script src="//cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js" defer=""></script>
    <script src="//cdn.datatables.net/1.10.19/js/dataTables.bootstrap4.min.js" defer=""></script>
    <table border="1" cellpadding="1" cellspacing="1">
        <tbody>
            <tr>
                <td>Synergistically envisioneer.</td>
            </tr>
            <tr>
                <td>Continually utilize client-focused.</td>
            </tr>
            <tr>
                <td>Conveniently procrastinate</td>
            </tr>
        <tbody>
    </table>
    <table border="1" cellpadding="1" cellspacing="1">
        <thead>
            <tr>
                <th>Column 1</th>
                <th>Column 2</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>Completely architect.</td>
                <td>Quickly incubate global</td>
            </tr>
            <tr>
                <td>Seamlessly transform.</td>
                <td>ethical e-services</td>
            </tr>
            <tr>
                <td>Efficiently develop customer directed</td>
                <td>high-yield users</td>
            </tr>
        <tbody>
    </table>
</div>

【问题讨论】:

    标签: javascript jquery html-table datatables


    【解决方案1】:

    编辑:

    根据 OP 更新,数据表的正确表格式要求每个表都有一个表头。第一张桌子缺少标题。您需要将适当的标题添加到第一个表中。

        <thead>
            <tr>
                <th>Column Header</th>
            </tr>
        </thead>
    

    【讨论】:

    • 我已更新问题以包含目标,因此更清晰。请参阅Multiple tables 示例。
    • @usernameabc 在多个表示例中,两个表都有标题,但您的第一个没有。你试过在里面添加吗?
    • 当我添加&lt;thead&gt; 时,它确实解决了问题。您能否更新您的答案以反映这一点,以免它在 cmets 中丢失?之后我会适当地标记它。
    • @usernameabc 我已将答案更新为仅包含标题。
    猜你喜欢
    • 2017-11-13
    • 2018-08-06
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 2015-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多