2016-09-24 63 views
1

我嘗試在數據表中使用AJAX財產「長度」,但我得到這個錯誤.. 在這裏,我解釋一下我的代碼:jquery.dataTables.min.js:48遺漏的類型錯誤:無法讀取的不確定

腳本:

$(document).ready(function() { 
     $('#tblEmployee').DataTable({ 
      "ajax": { 
       url: '/Employee/GetEmployees', 
       dataSrc: '' 
      }, 
      "columns": [ 
       { '': 'sEmpName' }, 
       { '': 'sGender' } 
      ] 
     }); 
    }); 

JSON回報:

[{"sEmpName":"Andi","sGender":"Man"},{"sEmpName":"Mark Will","sGender":"Women"},{"sEmpName":"Edward","sGender":"Man"}] 

HTML:

<table id="tblEmployee" class="table table-bordered"> 
     <thead> 
      <tr> 
       <th> 
        @Html.DisplayNameFor(model => model.sEmpName) 
       </th> 
       <th> 
        @Html.DisplayNameFor(model => model.sGender) 
       </th>      
      </tr> 
     </thead> 

    </table> 

我已經讀過這份文件https://datatables.net/manual/ajax,但仍然得到錯誤..

+1

https://legacy.datatables.net/ref#sAjaxDataProp參考這一點 - 因爲你的反應僅僅是一個數組您需要使用sAjaxDataProp來告訴DataTables這個事實 - 將其設置爲空字符串(請參閱文檔)。來源:https://datatables.net/forums/discussion/11901/uncaught-typeerror-cannot-read-property-length-of-undefined-jquery-datatables-js-1918 – Pat

+0

你可以看看這[[小提琴] (https://jsfiddle.net/ahaa821j/)讓我知道? – gaetanoM

+0

感謝@Pat爲您的頁面建議,它的工作原理以及..我會爲這篇文章創建一個答案.. :) –

回答

2

由該源建議:https://datatables.net/forums/discussion/11901/uncaught-typeerror-cannot-read-property-length-of-undefined-jquery-datatables-js-1918

我改變了我的劇本是這樣的:

$(document).ready(function() { 
     $('#tblEmployee').DataTable({ 
      "sAjaxSource": '/Employee/GetEmployees', 
      "sAjaxDataProp": "", 
      "aoColumns": [ 
       { "mDataProp": "sEmpName" }, 
       { "mDataProp": "sGender" } 
      ] 
     }); 
    }); 

它的工作原理好。謝謝.. :)

注: 我用的jQuery 3.1.0和數據表1.10.12

相關問題