gpt4 book ai didi

javascript - 如何迭代 DataTables 2.1 中的对象数组

转载 作者:行者123 更新时间:2023-12-03 06:55:16 25 4
gpt4 key购买 nike

HTML:

<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/t/bs-3.3.6/jq-2.2.0,dt-1.10.11/datatables.min.css"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script> <script type="text/javascript" src="https://cdn.datatables.net/t/bs-3.3.6/jq-2.2.0,dt-1.10.11/datatables.min.js"></script> <table id="dTExample" class="display" cellspacing="0" width="100%"> <thead> <tr> <th>Account #</th> <th>Customer #</th> <th>Customer Name</th> <th>Location</th> <th>Address</th> <th>Product</th> </tr> </thead> </table>

JS:

function populateData(table) {
var aTable;
aTable = $(table).dataTable({
sAjaxSource: 'SamplePage.aspx',
fnServerData: function (sSource, aoData, fnCallback) {
JsonLoader("SamplePage.aspx/GetServiceLookupList", "{'idType': 'an', 'idValue': '123'}", fnCallback);
},
columns: [
{ data: "billAccountNumber" },
{ data: "customerNumber" },
{ data: "customerName" },
{ data: "serviceLocationName" },
{ data: "serviceLocationAddress" },
{ data: "serviceComponentProductName" }
]
});
}

function JsonLoader(url, data, fnCallback) {
$.ajax({
type: "POST",
url: url,
contentType: "application/json; charset=utf-8",
dataType: "json",
data: data,
success: function (result) {
var objAjax = JSON.parse(result.d);
fnCallback(objAjax);
},
error: function (err, ajaxOptions, thrownError) {
//Error
}
});
}

返回的 JSON:

{“iTotalRecords”:100,“iTotalDisplayRecords”:100,“aa数据”:[{"billAccountName": "FAKE CORP","客户编号": "123","客户名称": "假的","serviceLocationName": "假公司","serviceLocationAddress": "PO BOX 123 Denver CO",“serviceComponentProductName”:“产品 X”},{"billAccountName": "REAL CORP","客户编号": "456","客户名称": "真实","serviceLocationName": "真实公司","serviceLocationAddress": "PO BOX 456 Ft Collins CO","serviceComponentProductName": "产品 Z"}, [...] }

所以基本上 aaData 是一个包含 100 个对象的数组,每个对象都有一个“billAccountName”。当我运行代码时,表上的记录数显示正确,但表是空的。不知道如何迭代所有这些对象并将它们各自的字段名称映射到表。请帮忙。

最佳答案

返回的 JSON 有 "iTotalRecords": 100, "iTotalDisplayRecords": 100,这实际上导致了问题。您的 Json 应该是一个对象数组,其中包含要在数据表中输入的值。您可以创建一个新数组并存储 ["aaData": [ { "billAccountName": "FAKE CORP", "customerNumber": "123", "customerName": "Fake", "serviceLocationName": "Fake Corp", "serviceLocationAddress": "PO BOX 123 Denver CO", "serviceComponentProductName": "产品 X"}, { "billAccountName": "REAL CORP", "customerNumber": "456", "customerName": "Real", "serviceLocationName ": "Real Corp", "serviceLocationAddress": "PO BOX 456 Ft Collins CO", "serviceComponentProductName": "Product Z"}, {...},]] 或返回的 JSON 应该像这样(严格在此格式):

{"aaData": [ { "billAccountName": "FAKE CORP", "customerNumber": "123", "customerName": "Fake", "serviceLocationName": "Fake Corp", "serviceLocationAddress": "PO BOX 123 丹佛 CO", "serviceComponentProductName": "产品 X"}, { "billAccountName": "REAL CORP", "customerNumber": "456", "customerName": "Real", "serviceLocationName": "Real Corp", "serviceLocationAddress": "PO BOX 456 Ft Collins CO", "serviceComponentProductName": "产品 Z"}]}

关于javascript - 如何迭代 DataTables 2.1 中的对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37306491/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com