jQuery DataTable - 使用自定义JSON结构显示数据

时间:2015-07-22 11:10:10

标签: javascript jquery json datatable

如何使用jQuery Datatable填充自定义JSON数据结构。我找到了下面的解决方案(Datatable默认 - JSON结构),它工作正常,直到我有数据"然而,作为主要对象/数组,我想利用另一个JSON结构(My JSON结构)。是否可能或者我是否需要坚持使用Datatable默认的JSON结构?我正在使用DataTables 1.10.7。提前感谢您的帮助。

数据表默认值 - JSON结构

{
 "data": [
{
  "name": "Tiger Nixon",
  "position": "System Architect",
  "salary": "$320,800",
  "start_date": "2011/04/25",
  "office": "Edinburgh",
  "extn": "5421"
},
{
  "name": "Garrett Winters",
  "position": "Accountant",
  "salary": "$170,750",
  "start_date": "2011/07/25",
  "office": "Tokyo",
  "extn": "8422"
}
]
}

我的JSON结构

[
{
  "Date": "12/04/14",
  "MeterID": "56",
  "BlockID": "12",
  "Type": "sure",
  "Subtype": "truction",
  "Notes": ""
},
{
  "Date": "12/04/14",
  "MeterID": "3456",
  "BlockID": "123456",
  "Type": "alism",
  "Subtype": "working",
  "Notes": "Lorem"
}
]

jQuery Datatable代码段

$('#incident').DataTable({
    "ajax": "../../data/object.json",
    "columns": [
        { "data": "name" },
        { "data": "position" },
        { "data": "salary" },
        { "data": "start_date" },
        { "data": "office" },
        { "data": "extn" }
    ],
    "iDisplayLength": 10
})

2 个答案:

答案 0 :(得分:0)

我是这样做的。

$('#divWorkQueueTable').html('<table cellpadding="0" cellspacing="0" border="0" class="display" id="workQueueTable"></table>');
    $('#workQueueTable').DataTable({
        "data": requests,
        "columns": [{
            "title": 'ID',
            "data": "ID",
            "sWidth": "5%",
            "render": function(data) {
                return '<a href="javascript:;" onclick="modARBRequest.Load( \'' + data + '\')">' + data + '</a>';
            }
        }, {
            "title": "Project ID",
            "data": "ProjectID",
            "sWidth": "10%"
        }, {
            "title": "Phase",
            "data": "Phase",
            "sWidth": "5%"
        }, {
            "title": "Project Description",
            "data": "Title",
            "sWidth": "50%"
        }, {
            "title": "Date Requested",
            "data": "DateRequested",
            "sWidth": "10%",
            "render": function(data) {
                return modCommon.DateToString(data);
            }
        }, {
            "title": "Stage",
            "data": "Stage",
            "sWidth": "15%"
        }, {
            "title": "Status",
            "data": "Status",
            "sWidth": "10%"
        }]
    });

所以,如果我是你,我会尝试解析ajax请求并将已解析的数据传递到数据表中。看看你的数据&#34;他们似乎与你的json相对应的线条。我错过了什么吗?

因此您需要类似&#34;数据&#34;:&#34;日期&#34;,&#34;数据&#34;:&#34; MeterID&#34;,&#34;数据& #34;:&#34; BlockID&#34;等...

$('#incident').DataTable({
"ajax": "../../data/object.json",
"columns": [
    { "data": "Date" },
    { "data": "MeterID" },
    { "data": "BlockID" },
    { "data": "Type" },
    { "data": "Subtype" },
    { "data": "Notes" }
],
"iDisplayLength": 10

})

答案 1 :(得分:0)

阅读此solution,它将解决您的查询..