尝试将JSON数据插入到数据表小部件中

时间:2012-11-06 19:40:20

标签: javascript jquery ajax json datatables

我有一个Java Web项目,我有一个GET端点,我想要检索JSON数据。 Firebug显示我正在以

的形式获取JSON数据
[{"id":7,"serial":"7bc530","randomDouble":0.0,"randomDouble2":0.0,"randomDouble3":0.0,"date":1352228474000,"removed":null},
{"id":8,"serial":"4a18d27","randomDouble":0.0,"randomDouble2":0.0,"randomDouble3":0.0,"date":1352228474000,"removed":null},
{"id":9,"serial":"f30ef","randomDouble":0.0,"randomDouble2":0.0,"randomDouble3":0.0,"date":1352228474000,"removed":null},
{"id":10,"serial":"9e6d","randomDouble":0.0,"randomDouble2":0.0,"randomDouble3":0.0,"date":1352228474000,"removed":null},
{"id":11,"serial":"4d8665a3","randomDouble":0.0,"randomDouble2":0.0,"randomDouble3":0.0,"date":1352228474000,"removed":null},
{"id":12,"serial":"4fe1457","randomDouble":0.0,"randomDouble2":0.0,"randomDouble3":0.0,"date":1352228474000,"removed":null}]

在HTML方面我有这个,

<table id="table_id">
   <thead>
      <tr>
      <th>id</th>
      <th>serial</th>
      <th>randomDouble</th>
      <th>randomDouble2</th>
      <th>randomDouble3</th>
      <th>date</th>
      <th>removed</th>
      </tr>
</thead>
 <tbody>
</tbody>
</table>

以下是我在javascript方面的内容,我在这里发现了另一个帖子,这个格式对他们有效。

$(document).ready(function() {

    var Table = $("#table_id").dataTable({
       "bFilter":false,
       "bPaginate":false,
       "bProcessing": true,
       "bServerSide":true,
       "bInfo":false,
       "sAjaxSource": ApiUrl(),
       "fnServerData": function (sSource, aoData, fnCallback){
           $.ajax({
            "dataType": 'json',
               "type": "GET",
               "url": sSource,
               "data": aoData,
               "success": fnCallback
           }); 
       }
    });

该表正在显示列,但它没有提取数据。正如我所说,我确实通过Firebug验证了JSON数据是否已经发送到网页 - 通过GET请求。

我发现数据表非常混乱,我无法让这个JSON实际填充......

非常感谢任何帮助。

编辑:

我试过了,

var Table = $("#table_id").dataTable({
       "bFilter":false,
       "bPaginate":false,
       "bProcessing": true,
       "bServerSide":true,
       "bInfo":false,
       "sAjaxSource": ApiUrl(),
       "sAjaxDataProp": ""
    });

2 个答案:

答案 0 :(得分:1)

您可以从任意数据源读取。查看下面的文档(找到here)。这适用于服务器端处理和ajax数据源。

  

此外,可以将sAjaxDataProp设置为空   string,导致DataTables将给定的数据源视为   表数据数组(而不是对象的属性)。

答案 1 :(得分:1)

这适用于服务器端:

 var Table = $("#table_id").dataTable({
   "bFilter":false,
   "bPaginate":false,
   "bProcessing": true,
   "bServerSide":true,
   "bInfo":false,
    "aoColumns": [
    { "mData": "id" },
    { "mData": "serial" },
    { "mData": "randomDouble" },
    { "mData": "randomDouble2" },
    { "mData": "randomDouble3" },
    { "mData": "date" },
    { "mData": "removed" }

    ],
   "sAjaxSource": "url",
   "fnServerData": function ( sSource, aoData, fnCallback ) {
    $.getJSON( sSource, aoData, function (json) { 
        map = {}
        map["aaData"] = json
        fnCallback(map)
    } );
}
});

关于最后评论的更新:

"fnServerData": function ( sSource, aoData, fnCallback ) {
  $.getJSON( sSource, aoData, function (json) { 
        $.getJSON('second_json_url', function(secondjson) {
            $.each(secondjson, function(index, object) {
              json[index].serial = secondjson[index].name
            })
            map = {}
            map["aaData"] = json
            fnCallback(map)
        })
  });
}