Datatable TypeError:f未定义

时间:2016-09-27 13:56:24

标签: javascript php jquery datatable

Js代码:

$('#large-page-data').DataTable({
"processing":true,
"serverSide":true,
"ajax": 'getlargedataTables',
});

数据:

[{"Name":"Janet Andor","Term":"First Term","Date":"2016-09-26","Class":"J.H.S 1","Subject":"ENGLISH LANGUAGE","Exam":"CLASS WORK","Marks":"20"},{"Name":"Henry Kumi Kumson","Term":"First Term","Date":"2016-09-26","Class":"J.H.S 1","Subject":"ENGLISH LANGUAGE","Exam":"CLASS WORK","Marks":"24"},   
{"Name":"Jones Alonso","Term":"First Term","Date":"2016-09-26","Class":"J.H.S 1","Subject":"ENGLISH LANGUAGE","Exam":"CLASS WORK","Marks":"27"},{"Name":"Kingsley Offei Kumi","Term":"First Term","Date":"2016-09-26","Class":"J.H.S 1","Subject":"ENGLISH LANGUAGE","Exam":"CLASS WORK","Marks":"21"},
{"Name":"Mary Anderson","Term":"First Term","Date":"2016-09-26","Class":"J.H.S 1","Subject":"ENGLISH LANGUAGE","Exam":"CLASS WORK","Marks":"25"},{"Name":"Mary Boatemaa Duodu","Term":"First Term","Date":"2016-09-26","Class":"J.H.S 1","Subject":"ENGLISH LANGUAGE","Exam":"CLASS WORK","Marks":"22"}]

表:

<table class="table table-hover table-striped table-bordered" id="large-page-data">
   <thead>
      <tr>
          <th>NAME</th>
          <th>TERM</th>
          <th>DATE</th>
          <th>CLASS</th>
          <th>EXAM</th>
          <th>SUBJECT</th>
          <th>MARKS</th>
     </tr>
   </thead>
</table>

如果有人能指出我出错的地方,我将不胜感激

2 个答案:

答案 0 :(得分:1)

您的JSON数据格式错误。它应该是这样的:

{
data: [
    ["Janet Andor","First Term","2016-09-26","J.H.S 1","ENGLISH LANGUAGE","CLASS WORK","20"],
    ["Janet Andor","First Term","2016-09-26","J.H.S 1","ENGLISH LANGUAGE","CLASS WORK","20"],
    ["Janet Andor","First Term","2016-09-26","J.H.S 1","ENGLISH LANGUAGE","CLASS WORK","20"],
    ["Janet Andor","First Term","2016-09-26","J.H.S 1","ENGLISH LANGUAGE","CLASS WORK","20"]
]}

您可以参考此数据表示例:https://datatables.net/examples/data_sources/ajax.html

答案 1 :(得分:0)

我知道这是一个比较老的问题,但这是第一个在搜索中弹出的问题。 对我来说,答案是添加 datasrc:“” ,因为我使用的是ASP.NET Core Razor页面而不是MVC。

示例:

$('#BookList').DataTable({
            "ajax": {
                "url": "/api/Books/GetAllBooks",
                "method": "GET",
                "dataSrc": ""
            },
            "columns": [
                { "data": "id", "visible": false },
                { "data": "name", "title": "Name" },

            ],
            "order": [[1, "asc"]]
        });