简体   繁体   English

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

[英]jQuery DataTable - Displaying data with custom JSON structure

How to populate custom JSON data structure using jQuery Datatable. 如何使用jQuery Datatable填充自定义JSON数据结构。 I found below solution(Datatable default - JSON structure) and it's working fine until I have 'Data" as main object/array, however, I want to utilize another JSON structure(My JSON structure). Is it possible or do I need to stick with Datatable default JSON structure? I am using DataTables 1.10.7. Thanks in advance for any help. 我在下面找到解决方案(Datatable默认-JSON结构),在我将“数据”作为主要对象/数组之前,它可以正常工作,但是,我想使用另一个JSON结构(我的JSON结构)。是否可能或我需要坚持使用Datatable默认的JSON结构吗?我正在使用DataTables 1.10.7。预先感谢您的帮助。

Datatable default - JSON Structure 数据表默认-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"
}
]
}

My JSON structure 我的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 code snippet jQuery Datatable代码片段

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

Here is how I have done it. 这是我的方法。

$('#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%"
        }]
    });

So if I were you I would attempt to resolve the ajax request and pass the resolved data into the data table. 因此,如果我是您,我将尝试解决ajax请求并将已解决的数据传递到数据表中。 Looking at your "data" lines they don't seem to correspond to your json. 查看您的“数据”行,它们似乎与您的json不对应。 Am I missing something? 我想念什么吗?

so you would need something like "data":"Date", "data": "MeterID", "data": "BlockID" etc... 因此您将需要“ data”:“ Date”,“ data”:“ MeterID”,“ data”:“ BlockID”等内容。

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

}) })

阅读此解决方案 ,它将解决您的查询。

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

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