从json文件渲染DataTable行

时间:2016-05-12 11:37:51

标签: javascript jquery html json datatables

我试图从json文件渲染DataTable-rows,放在同一目录下。但不幸的是,DataTable-Body保持空白,并且控制台中没有显示错误。 enter image description here

这是(简化的)HTML代码:

<table id="opTable">
    <thead>
      <tr><th class="partner_id">ID</th><th>ShortName</th><th>DisplayName</th><th>LogoUrl</th><th>Incent</th><th>JTS-URL</th></tr>
    </thead>
    <tbody>
    </tbody>
</table>

以下是我用来从json文件加载数据的JavaScript:

      <script>
    $(document).ready(function() {  
        $('#onlinePartnerTable').DataTable({
             "ajax" : "../test2.json",
            "columns": [{
                "data": "pId"
            }, {
                "data": "shrName"
            }, {
                "data": "DplayName"
            }, {
                "data": "lgo"
            }, {
                "data": "inc"
            }, {
                "data": "featured"
            }]
        });
    });
  </script>

这是我的json文件的示例:

{
"partnerList": [ 
         {
            "shrname": "bchan",
            "Dplayname": "bchang-Shop",
            "pId": "id12345",
            "featured": "0",
            "lgo": "https://url.here.com/url",
            "inc": "1 to 1",
            "dets": {
                "tmage": "someUrl/here",
                "desp": "desciption",
                "jturl": "jtUrl/here",
                "jtbut": "Shop",
                "termy": [
                    {
                        "heady": "",
                        "body": ""
                    }
                ]
            }
        },
         {
            "shrname": "hereIsIt",
            "Dplayname": "HereIs-Shop",
            "pId": "id65432",
            "featured": "0",
            "lgo": "https://url.here.com/url",
            "inc": "2 to 1",
            "dets": {
                "tmage": "someUrl/here",
                "desp": "desciption",
                "jturl": "jtUrl/here",
                "jtbut": "Shop",
                "termy": [
                    {
                        "heady": "",
                        "body": ""
                    }
                ]
            }
        }
  ]
}

我现在正在考虑这个错误..见下图。

enter image description here

1 个答案:

答案 0 :(得分:0)

根据评论Datatables Ajax Data Source (objects)中提供的Turnip链接之一,您的数据应如下所示:

{
  "data": [
        {
            "shrname": "bchan",
            "Dplayname": "bchang-Shop",
            "pId": "id12345",
            "featured": "0",
            "lgo": "https://url.here.com/url",
            "inc": "1 to 1",              
        },
         {
            "shrname": "hereIsIt",
            "Dplayname": "HereIs-Shop",
            "pId": "id65432",
            "featured": "0",
            "lgo": "https://url.here.com/url",
            "inc": "2 to 1",             
        }
    ]
}

我不确定您要对数据的“dets”部分做什么,但