【发布时间】:2020-04-10 18:46:00
【问题描述】:
我正在使用 Datatables.net 在我的 Shopify 网站上显示数据。例如,我只是将 JSON 数据设置为具有“itemcode”的 1 个键值对,然后是实际产品的项目代码。这是我的数据示例:
JSON 数据
{
"jsonData": [
{
"itemcode": "0735340080 - Bearings"
},
{
"itemcode": "BL208-Z - Bearings"
},
{
"itemcode": "9109K - Bearings"
},
{
"itemcode": "735330016 - Bearings"
},
{
"itemcode": "699-ZZ - Bearings"
},
{
"itemcode": "698-ZZ - Bearings"
},
{
"itemcode": "697-ZZ - Bearings"
}
]
}
我正在使用此代码来加载数据。为了澄清,JSON 是从页面上隐藏的 div 元素加载的:
Javascript 代码
$(document).ready(function () {
var jsonDataDiv = document.getElementById("json-data").innerText;
var jsonData = JSON.parse(jsonDataDiv);
var table = $('#tableAppend').DataTable({
orderCellsTop: true,
pageLength: 25,
data: jsonData,
"columns": [{jsonData: "itemcode"}],
columnDefs: [
{"className": "dt-center", "targets": "_all"}
],
});
});
调试窗口中未报告任何错误,但我的表格显示没有可用数据。我的 HTML 代码在这里:
HTML 代码
<table class="display" id="tableAppend" style="margin-left: 20px;margin-right: 20px;">
<thead>
<tr>
<th class="dt-center">Item Code</th>
</tr>
</thead>
</table>
我一直在关注在线问答,但它们似乎与我的问题无关。
【问题讨论】:
标签: javascript jquery html datatables shopify