jquery - 将 DataTable 与 colspan 一起使用时,无法读取未定义的属性 'mData'

标签 jquery datatables

我正在使用数据表来显示来自服务器的数据。问题是我在控制台中不断收到错误消息:

datatables.min.js:145 Uncaught TypeError: 无法读取未定义的属性“mData”

我几乎访问了互联网上与此相关的每个链接,但没有任何对我有用。

我已使用 colspans 确保 thead 和 tbody 中的列数相同。

我可能遗漏了一些东西,但在花了相当长的时间之后,我希望在这里得到一些帮助。

代码如下

HTML:

<table id="data-table" class="display table" width="100%">
  <thead>
    <tr>
      <th colspan="4" class="center-align solid-left-border" style="border-bottom: none; text-decoration: underline; text-transform: uppercase; padding: 5px 18px;">
        Tier 2 Contributions Report
      </th>
    </tr>

    <tr>
      <th class="left-align solid-left-border" style="border-bottom: none; text-decoration: none; text-transform: uppercase; font-weight: normal; font-weight: normal; padding: 5px 18px; font-size: 12.5px">
        Employer's FIle No/Registration No:
      </th>

      <th colspan="3" class="left-align solid-left-border" style="border-bottom: none; font-weight: normal; padding: 5px 18px; font-size: 12.5px; text-transform: uppercase;">
        <%= company.getSSNITRegistration() || '-' %>
      </th>
    </tr>

    <tr>
      <th class="left-align solid-left-border" style="border-bottom: none; text-decoration: none; text-transform: uppercase; font-weight: normal; padding: 5px 18px; font-size: 12.5px;">
        Name of Employer:
      </th>

      <th colspan="3" class="left-align solid-left-border" style="border-bottom: none; font-weight: normal; padding: 5px 18px; font-size: 12.5px; text-transform: uppercase;">
        <%= company.getName() || '-' %>
      </th>
    </tr>

    <tr>
      <th class="left-align solid-left-border" style="border-bottom: none; text-decoration: none; text-transform: uppercase; font-weight: normal; padding: 5px 18px; font-size: 12.5px;">
        Address of Employer:
      </th>

      <th colspan="3" class="left-align solid-left-border" style="border-bottom: none; font-weight: normal; padding: 5px 18px; font-size: 12.5px; text-transform: uppercase;">
        <%= company.getAddress() || '-' %>
      </th>
    </tr>


    <tr>
      <th colspan="4" style="border-bottom: none;"></th>
    </tr>
  </thead>

  <tfoot>
   <tr>
     <th colspan="2" class="left-align">Totals</th>
     <th class="center-align"><%= addCommas(totals.basicSalary) %></th>
     <th class="right-align"><%= addCommas(totals.contribution) %></th>
   </tr>
  </tfoot>

  <tbody>
    <tr>
      <th class="left-align">Social Sec. No.</th>
      <th class="left-align">Full Name</th>
      <th class="center-align">Basic Salary</th>
      <th class="right-align">Contribution (5%)</th>
    </tr>

    <% employees.forEach(function(employee) { %>
      <tr>
        <td class="left-align"><%= employee.ssnitNumber %></td>
        <td class="left-align"><%= employee.lastName + ', ' + employee.firstName + ' ' + employee.otherNames%></td>
        <td class="center-align"><%= addCommas(employee.basicSalary) %></td>
        <td class="right-align"><%= addCommas(employee.contribution) %></td>
      </tr>
    <% }) %>
  </tbody>
</table>

JS

$('#data-table').DataTable( {
  "bPaginate": true,
  "bLengthChange": true,
  "bFilter": true,
  "bSort": false,
  "bInfo": true,
  "bAutoWidth": false,
  "dom": 'Bfrtip',
  "buttons": [
    'copy', 'csv', 'excel', 'pdf', 'print'
  ]

});

最佳答案

此类问题的一般原因是

  1. 标题列和页脚列不匹配。

  2. Body 元素与 header 不匹配(一行中的 td 数量应与 header 中的 <th> 匹配)。

  3. 表 ID 重复。

在您的情况下,请从 <tbody> 移动标题(如社交账号等)至<thead> ,并且您的页眉和页脚不匹配,请使它们相同,因为我看到页眉中有 4 个元素,页脚中只有 3 个元素。

关于jquery - 将 DataTable 与 colspan 一起使用时,无法读取未定义的属性 'mData',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36684959/

相关文章:

javascript - Angular js + 选择插件 + 将值设置为在下拉列表中选择

javascript - Highcharts 显示当月数据与周间隔

php - JavaScript/Jquery : download multiple images and save locally

javascript - 使用css3 和Jquery 为图像制作3d 效果?

jquery - 检测 Bootstrap 轮播是向前还是向后

JQuery DataTables - JQuery UI 主题,箭头包裹在大腿上

javascript - 无法使用 Java 脚本在 asp.net 中绑定(bind) HTML DataTable

jquery - 当表头单元格中出现 colspan 时,DataTables 抛出错误

带有对象的 jQuery DataTables fnrender

java - 为什么这个 *jquery.dataTables.js* 示例不起作用?