2016-09-16 211 views
1

我试图在我的应用程序中添加DataTable。 https://datatables.net/ 我有一个网页,其中的数据表要在onclick事件上显示,当系统触发onclick事件时,ajax调用将被调用,并且数据将来自Java servlet。下面是我的代码Java Json不能正常工作的Jquery数据表

JSP:

<table id="testTable" class="display" cellspacing="0" 
          width="100%" cellpadding="0" border="0"> 
          <thead> 
           <tr> 
            <th>Test</th> 
            <th>Description</th> 
            <th>Result</th> 
            <th>Start Time</th> 
            <th>End Time</th> 
           </tr> 
          </thead> 

         </table> 

JQuery的:

function getTestData(name, e, bId) { 
     $('#testTable').dataTable({ 

      "serverSide": true, 
      "processing": true, 
      "ajax":{     
      "type" : "POST", 
      "dataSrc": "data", 
      "url" : "Servlet", 
      "dataType": "json", 
      "data" : { 
       name : name, 
       e : e, 
       bId : bId, 
       method : "getTestData" 
      }, 
      "sEcho": 0, 
      "processing": true, 
       "columns":[ 
         { "data": "tId" }, 
         { "data": "description" }, 
         { "data": "rst" }, 
         { "data": "startDate" }, 
         { "data": "endDate" } 
        ]  
      } 
     }); 
    } 

Servlet是返回以下JSON:

{ 
     "data": [ 
     { 
      "tId": "1", 
      "description": "desc", 
      "rst": "P", 
      "startDate": "2016-09-13 07:59:31.0", 
      "endDate": "2016-09-13 07:59:51.0" 
     }, 
     { 
      "tId": "2", 
      "description": "desc", 
      "rst": "S", 
       "startDate": "2016-09-13 07:59:51.0", 
      "endDate": "2016-09-13 07:59:51.0" 
      } 
     ] 
     } 

我得到以下错误:

DataTables warning: table id=testTable - Requested unknown parameter '0' for row 0, column 0. 

enter image description here

我知道错误的原因,但不知道我在上面的代码做错了,我想一些错误的jQuery代码。请帮忙。

在此先感谢

+0

数据表

 $.getJSON("http://example/servlet_data.json", function(fromServer) { $('#testTable').DataTable({ data: fromServer.data, columns: [ { data: "tId" }, { data: "description"}, { data: "rst"}, { data: "startDate"}, { data: "endDate"} ] }); }).fail(function(){ alert("Error occurred getting data from the server"); }) 

高兴,如果你使用'{“数据”:0},{“数据“:1}'等。它工作吗? – markpsmith

+0

是的,但没有运气...... – itin

+0

你的意思是它的工作原理,或者不是吗? – markpsmith

回答

0

的JSON应在下述格式

{ 
    "draw": 1, 
"recordsTotal": 57, 
"recordsFiltered": 57, 
"data": [ 
[ 
    "Airi", 
    "Satou", 
    "Accountant", 
    "Tokyo", 
    "28th Nov 08", 
    "$162,700" 
], 
[ 
    "Angelica", 
    "Ramos", 
    "Chief Executive Officer (CEO)", 
    "London", 
    "9th Oct 09", 
    "$1,200,000" 
] 
] 
} 

https://datatables.net/examples/data_sources/server_side.html

0

更改您的初始化代码到你的JSON数据格式相匹配,如下图所示。

$('#testTable').dataTable({ 
    "ajax": { 
     "type": "POST", 
     "url": "Servlet" 
    }, 
    "columns": [ 
     { "data": "tId" }, 
     { "data": "description"}, 
     { "data": "rst" }, 
     { "data": "startDate" }, 
     { "data": "endDate"} 
    ] 
}); 

查看this jsFiddle的代码和演示。

0

初始化数据表的方式不正确,可以通过不同的方式查看here

@ Gyrocode.com的答案从您可能正在努力的方向中挑选出来。 我只是给你一个答案,我想也许可以考虑获得数据并将它传递给编码