开发者问题收集

数据表:无法读取未定义的属性“mData”

2014-08-19
489446
3个回答

仅供参考 dataTables 需要格式正确的表格。它必须包含 <thead><tbody> 标签,否则会抛出此错误。还请检查以确保所有行(包括标题行)的列数相同。

以下操作将抛出错误(没有 <thead><tbody> 标签)

<table id="sample-table">
    <tr>
        <th>title-1</th>
        <th>title-2</th>
    </tr>
    <tr>
        <td>data-1</td>
        <td>data-2</td>
    </tr>
</table>

以下操作也会抛出错误(列数不等)

<table id="sample-table">
    <thead>
        <tr>
            <th>title-1</th>
            <th>title-2</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>data-1</td>
            <td>data-2</td>
            <td>data-3</td>
        </tr>
    </tbody>
</table>

有关更多信息,请 在此处阅读更多

Moses Machua
2014-09-22

导致 无法读取未定义的属性“fnSetData” 的常见原因是列数不匹配,例如以下错误代码:

<thead>                 <!-- thead required -->
    <tr>                <!-- tr required -->
        <th>Rep</th>    <!-- td instead of th will also work -->
        <th>Titel</th>
                        <!-- th missing here -->
    </tr>
</thead>
<tbody>
    <tr>
        <td>Rep</td>
        <td>Titel</td>
        <td>Missing corresponding th</td>
    </tr>
</tbody>

而以下代码中每个 <td> 有一个 <th> (列数必须匹配)可以正常工作:

<thead>
    <tr>
        <th>Rep</th>       <!-- 1st column -->
        <th>Titel</th>     <!-- 2nd column -->
        <th>Added th</th>  <!-- 3rd column; th added here -->
    </tr>
</thead>
<tbody>
    <tr>
        <td>Rep</td>             <!-- 1st column -->
        <td>Titel</td>           <!-- 2nd column -->
        <td>th now present</td>  <!-- 3rd column -->
    </tr>
</tbody>

使用格式正确且带有 colspan 但没有第二行的 thead 时也会出现此错误。

对于具有 7 列的表格,以下代码不起作用,我们会在 javascript 控制台中看到“无法读取未定义的属性‘mData’”:

<thead>
    <tr>
        <th>Rep</th>
        <th>Titel</th>
        <th colspan="5">Download</th>
    </tr>
</thead>

而以下代码可以正常工作:

<thead>
    <tr>
        <th rowspan="2">Rep</th>
        <th rowspan="2">Titel</th>
        <th colspan="5">Download</th>
    </tr>
    <tr>
        <th>pdf</th>
        <th>nwc</th>
        <th>nwctxt</th>
        <th>mid</th>
        <th>xml</th>
    </tr>
</thead>
PaulH
2014-11-07

使用相同数量的 <thead><tbody> 以及相同数量的 <th><td> 解决了我的问题。

Masoud Darvishian
2019-05-20