jQuery dataTable错误无法读取属性'mData'的undefined

5t7ly7z5  于 2023-06-05  发布在  jQuery
关注(0)|答案(3)|浏览(503)

我有下面的dataTable有6列。
在渲染表之后,我初始化.DataTable()。但得到的错误:
'mData' of undefined TypeError:无法读取未定义的属性“mData”
正如在一些帖子中提到的,我已经添加了tfoot,但仍然有错误。

$(document).ready(function() {
    $('#pTable').DataTable();
});
<link href="https://cdn.datatables.net/1.10.19/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.19/js/jquery.dataTables.min.js"></script>

<table id="pTable" class="display" style="width:100%">
    <thead>
        <tr>
            <th>Status</th>
            <th>Name</th>
            <th>NOK</th>
            <th>Phone</th>
            <th>IsEnabled</th>
            <th>Add/Edit</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td class="text-muted" data-bind="text: ID, visible: false" style="display: none;">288f60e3-0d8f-4a4a-92f9-4f4eeb737909</td>
            <td><span class="label label-accent">Active</span></td>
            <td class="text-muted" data-bind="text: NAME">Buddy1</td>
            <td class="text-muted" data-bind="text: NOK">SomeOne</td>
            <td class="text-muted" data-bind="text: SYMPTOMS">12354475541</td>
            <td class="text-muted">
                <div class="pretty p-round p-fill p-icon">
                    <input type="checkbox" data-bind="value: ISENABLED, checked: ISENABLED, event: { change: $parent.togglePatients}" value="true">
                    <div class="state p-primary">
                        <i class="icon fa fa-check"></i>
                        <label data-bind="text: ISENABLED">true</label>
                    </div>
                </div>
            </td>
            <td class="text-muted">
                <div class="btn-group pull-right">
                    <button data-bind="click: $parent.showPatients" type="button" class="btn btn-default btn-xs" data-toggle="modal" data-target="#PatientsModal">
                                                    <i class="fa fa-edit"></i> Edit
                                                </button>
                </div>
            </td>
        </tr>
        <tr>

            <td class="text-muted" data-bind="text: ID, visible: false" style="display: none;">ae2dc252-4940-455e-a528-766f3d7d8fe9</td>
            <td><span class="label label-accent">Active</span></td>
            <td class="text-muted" data-bind="text: NAME">Buddy2</td>
            <td class="text-muted" data-bind="text: NOK">SomeOne Else</td>
            <td class="text-muted" data-bind="text: SYMPTOMS">0564654654734</td>
            <td class="text-muted">
                <div class="pretty p-round p-fill p-icon">
                    <input type="checkbox" data-bind="value: ISENABLED, checked: ISENABLED, event: { change: $parent.togglePatients}" value="false">
                    <div class="state p-primary">
                        <i class="icon fa fa-check"></i>
                        <label data-bind="text: ISENABLED">false</label>
                    </div>
                </div>
            </td>
            <td class="text-muted">
                <div class="btn-group pull-right">
                    <button data-bind="click: $parent.showPatients" type="button" class="btn btn-default btn-xs" data-toggle="modal" data-target="#PatientsModal">
                                                    <i class="fa fa-edit"></i> Edit
                                                </button>
                </div>
            </td>

        </tr>

    </tbody>
    <tfoot>
        <tr>
            <th>Status</th>
            <th>Name</th>
            <th>Patients</th>
            <th>Phone</th>
            <th>IsEnabled</th>
            <th>Add/Edit</th>
        </tr>
    </tfoot>
</table>
zf9nrax1

zf9nrax11#

你有6个th,但两行都有7个tdth的数量必须与td的数量相匹配。添加隐藏的th修复了它

$(document).ready(function() {
    $('#pTable').DataTable();
});
<link href="https://cdn.datatables.net/1.10.19/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.19/js/jquery.dataTables.min.js"></script>

<table id="pTable" class="display" style="width:100%">
    <thead>
        <tr>
            <th style="display:none"></th>
            <th>Status</th>
            <th>Name</th>
            <th>NOK</th>
            <th>Phone</th>
            <th>IsEnabled</th>
            <th>Add/Edit</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td class="text-muted" data-bind="text: ID, visible: false" style="display: none;">288f60e3-0d8f-4a4a-92f9-4f4eeb737909</td>
            <td><span class="label label-accent">Active</span></td>
            <td class="text-muted" data-bind="text: NAME">Buddy1</td>
            <td class="text-muted" data-bind="text: NOK">SomeOne</td>
            <td class="text-muted" data-bind="text: SYMPTOMS">12354475541</td>
            <td class="text-muted">
                <div class="pretty p-round p-fill p-icon">
                    <input type="checkbox" data-bind="value: ISENABLED, checked: ISENABLED, event: { change: $parent.togglePatients}" value="true">
                    <div class="state p-primary">
                        <i class="icon fa fa-check"></i>
                        <label data-bind="text: ISENABLED">true</label>
                    </div>
                </div>
            </td>
            <td class="text-muted">
                <div class="btn-group pull-right">
                    <button data-bind="click: $parent.showPatients" type="button" class="btn btn-default btn-xs" data-toggle="modal" data-target="#PatientsModal">
                                                    <i class="fa fa-edit"></i> Edit
                                                </button>
                </div>
            </td>
        </tr>
        <tr>

            <td class="text-muted" data-bind="text: ID, visible: false" style="display: none;">ae2dc252-4940-455e-a528-766f3d7d8fe9</td>
            <td><span class="label label-accent">Active</span></td>
            <td class="text-muted" data-bind="text: NAME">Buddy2</td>
            <td class="text-muted" data-bind="text: NOK">SomeOne Else</td>
            <td class="text-muted" data-bind="text: SYMPTOMS">0564654654734</td>
            <td class="text-muted">
                <div class="pretty p-round p-fill p-icon">
                    <input type="checkbox" data-bind="value: ISENABLED, checked: ISENABLED, event: { change: $parent.togglePatients}" value="false">
                    <div class="state p-primary">
                        <i class="icon fa fa-check"></i>
                        <label data-bind="text: ISENABLED">false</label>
                    </div>
                </div>
            </td>
            <td class="text-muted">
                <div class="btn-group pull-right">
                    <button data-bind="click: $parent.showPatients" type="button" class="btn btn-default btn-xs" data-toggle="modal" data-target="#PatientsModal">
                                                    <i class="fa fa-edit"></i> Edit
                                                </button>
                </div>
            </td>

        </tr>

    </tbody>
    <tfoot>
        <tr>
            <th>Status</th>
            <th>Name</th>
            <th>Patients</th>
            <th>Phone</th>
            <th>IsEnabled</th>
            <th>Add/Edit</th>
        </tr>
    </tfoot>
</table>
lc8prwob

lc8prwob2#

每一行的表头数和单元格数应该相同(页脚也一样)。一旦你这样做,这个错误将被删除。

'<table id="pTable" class="display" style="width:100%">
<thead>
    <tr>
        <th>Status</th>
        <th>Name</th>
        <th>NOK</th>
        <th>Phone</th>
        <th>IsEnabled</th>
        <th>Add/Edit</th>
        <th>Some Heading</th>
    </tr>
</thead>
<tbody>
    <tr>
        <td class="text-muted" data-bind="text: ID, visible: false" style="display: none;">288f60e3-0d8f-4a4a-92f9-4f4eeb737909</td>
        <td><span class="label label-accent">Active</span></td>
        <td class="text-muted" data-bind="text: NAME">Buddy1</td>
        <td class="text-muted" data-bind="text: NOK">SomeOne</td>
        <td class="text-muted" data-bind="text: SYMPTOMS">12354475541</td>
        <td class="text-muted">
            <div class="pretty p-round p-fill p-icon">
                <input type="checkbox" data-bind="value: ISENABLED, checked: ISENABLED, event: { change: $parent.togglePatients}" value="true">
                <div class="state p-primary">
                    <i class="icon fa fa-check"></i>
                    <label data-bind="text: ISENABLED">true</label>
                </div>
            </div>
        </td>
        <td class="text-muted">
            <div class="btn-group pull-right">
                <button data-bind="click: $parent.showPatients" type="button" class="btn btn-default btn-xs" data-toggle="modal" data-target="#PatientsModal">
                                                <i class="fa fa-edit"></i> Edit
                                            </button>
            </div>
        </td>
    </tr>
    <tr>

        <td class="text-muted" data-bind="text: ID, visible: false" style="display: none;">ae2dc252-4940-455e-a528-766f3d7d8fe9</td>
        <td><span class="label label-accent">Active</span></td>
        <td class="text-muted" data-bind="text: NAME">Buddy2</td>
        <td class="text-muted" data-bind="text: NOK">SomeOne Else</td>
        <td class="text-muted" data-bind="text: SYMPTOMS">0564654654734</td>
        <td class="text-muted">
            <div class="pretty p-round p-fill p-icon">
                <input type="checkbox" data-bind="value: ISENABLED, checked: ISENABLED, event: { change: $parent.togglePatients}" value="false">
                <div class="state p-primary">
                    <i class="icon fa fa-check"></i>
                    <label data-bind="text: ISENABLED">false</label>
                </div>
            </div>
        </td>
        <td class="text-muted">
            <div class="btn-group pull-right">
                <button data-bind="click: $parent.showPatients" type="button" class="btn btn-default btn-xs" data-toggle="modal" data-target="#PatientsModal">
                                                <i class="fa fa-edit"></i> Edit
                                            </button>
            </div>
        </td>

    </tr>

</tbody>
<tfoot>
    <tr>
        <th>Status</th>
        <th>Name</th>
        <th>Patients</th>
        <th>Phone</th>
        <th>IsEnabled</th>
        <th>Add/Edit</th>
        <th>Some Heading</th>
    </tr>
</tfoot>

jum4pzuy

jum4pzuy3#

几年后。。
我来这里是因为我遇到了同样的问题,我的<th><td>计数匹配。问题是:我没有使用<thead><tbody>

相关问题