简体   繁体   English

使用Java Json的Jquery数据表无法正常工作

[英]Jquery Datatables with Java Json not working

I was trying to add the DataTable in my application. 我试图在我的应用程序中添加DataTable。 https://datatables.net/ I have a WebPage in which data table to be shown on onclick event, when system trigger onclick event ajax call get made and data will come from the Java servlet. https://datatables.net/我有一个WebPage,其中数据表显示在onclick事件上,当系统触发onclick事件ajax调用时,数据将来自Java servlet。 below is my code 下面是我的代码

JSP: 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: 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 is returning below JSON: 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"
           }
        ] 
      }

I am getting below error : 我收到以下错误:

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

在此输入图像描述

I know the reason of error but not sure what i am doing wrong in the above code, I guess something wrong in the jquery code. 我知道错误的原因,但不知道我在上面的代码中做错了什么,我想在jquery代码中有问题。 Please help. 请帮忙。

Thanks in advance 提前致谢

The JSON should be in below mentioned format 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 https://datatables.net/examples/data_sources/server_side.html

Change your initialization code to match your JSON data format as shown below. 更改初始化代码以匹配您的JSON数据格式,如下所示。

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

See this jsFiddle for code and demonstration. 有关代码和演示,请参阅此jsFiddle

The way you initializing the datatable is not correct, can look here for different ways to do it. 初始化数据表的方式不正确,可以在这里查看不同的方法。

@Gyrocode.com's answer picks up from what you probably struggling on. @ Gyrocode.com的回答可以从你可能正在努力的事情中找到答案。 Am just giving you one more answer, am thinking maybe can consider getting the data and passing it to your data table 我只是给你一个答案,我想也许可以考虑获取数据并将其传递给你的数据表

        $.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");
    })

Happy coding 快乐的编码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM