1. 程式人生 > >錯誤碼:Uncaught TypeError: Cannot read property 'field' of undefined

錯誤碼:Uncaught TypeError: Cannot read property 'field' of undefined

錯誤碼:Uncaught TypeError: Cannot read property ‘field’ of undefined

具體錯誤資訊:

Uncaught TypeError: Cannot read property 'field' of undefined
    at HTMLTableCellElement.<anonymous> (bootstrap-table.js:506)
    at Function.each (jquery-1.11.3.min.js:2)
    at m.fn.init.each (jquery-1.11.3.min
.js:2) at HTMLTableRowElement.<anonymous> (bootstrap-table.js:505) at Function.each (jquery-1.11.3.min.js:2) at m.fn.init.each (jquery-1.11.3.min.js:2) at BootstrapTable.initTable (bootstrap-table.js:497) at BootstrapTable.init (bootstrap-table.js:414) at new BootstrapTable (bootstrap-table.js:165
) at HTMLTableElement.<anonymous> (bootstrap-table.js:2281)

錯誤原始碼

<table id="table" data-toggle="table">
    <tr>
        <th class="number title-cell">序號</th>
        <th class="name title-cell">產品名稱</th>
        <th class="kind title-cell">產品種類</th
>
<th class="bank title-cell">銀行名稱</th> <th class="custorm title-cell">客戶屬性</th> <th class="rate title-cell">利率</th> <th class="year title-cell">貸款年限</th> <th class="money title-cell">貸款額度</th> <th class="attribute title-cell">屬性列表</th> <th class="sort title-cell">排序</th> <th class="operate title-cell">操作</th> </tr> </table>

修改後程式碼

<table id="table" data-toggle="table">
    <thead>
        <tr>
            <th class="number title-cell">序號</th>
            <th class="name title-cell">產品名稱</th>
            <th class="kind title-cell">產品種類</th>
            <th class="bank title-cell">銀行名稱</th>
            <th class="custorm title-cell">客戶屬性</th>
            <th class="rate title-cell">利率</th>
            <th class="year title-cell">貸款年限</th>
            <th class="money title-cell">貸款額度</th>
            <th class="attribute title-cell">屬性列表</th>
            <th class="sort title-cell">排序</th>
            <th class="operate title-cell">操作</th>
        </tr>
    </thead>
</table>

原因

  1. 缺少<thead>
  2. 在表格裡的<td>標籤數量與<th>的標籤數量不一樣。

解決方法

  1. 確保表格裡有<thead>標籤;
  2. 確保表格裡的<td>數量與<th>數量一致。

參考連線

  1. https://www.gyrocode.com/articles/jquery-datatables-common-javascript-console-errors/#typeerror-cannot-read-property-mdata-of-undefined
  2. https://datatables.net/manual/installation#HTML