简体   繁体   English

如何遍历DataTables 2.1中的对象数组

[英]How to iterate over an array of objects in DataTables 2.1

HTML: 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: 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 } }); }

Returned JSON: 返回的JSON:

{ "iTotalRecords": 100, "iTotalDisplayRecords": 100, "aaData": [ { "billAccountName": "FAKE CORP", "customerNumber": "123", "customerName": "Fake", "serviceLocationName": "Fake Corp", "serviceLocationAddress": "PO BOX 123 Denver CO", "serviceComponentProductName": "Product X" }, { "billAccountName": "REAL CORP", "customerNumber": "456", "customerName": "Real", "serviceLocationName": "Real Corp", "serviceLocationAddress": "PO BOX 456 Ft Collins CO", "serviceComponentProductName": "Product Z" }, [...] } {“ iTotalRecords”:100,“ iTotalDisplayRecords”:100,“ aaData”:[{“ billAccountName”:“ FAKE CORP”,“ customerNumber”:“ 123”,“ customerName”:“ Fake”,“ serviceLocationName”:“ Fake Corp”,“ serviceLocationAddress”:“ PO BOX 123 Denver CO”,“ serviceComponentProductName”:“ Product X”},{“ billAccountName”:“ REAL CORP”,“ customerNumber”:“ 456”,“ customerName”:“ Real” ,“ serviceLocationName”:“ Real Corp”,“ serviceLocationAddress”:“邮政信箱456 Ft Collins CO”,“ serviceComponentProductName”:“产品Z”},[...]}

So basiclly aaData is an array of 100 objects, each of which has a "billAccountName". 因此,aaData基本上是100个对象的数组,每个对象都有一个“ billAccountName”。 When I run my code, the number of records on the table is showing up correctly, but the table is empty. 当我运行代码时,表上的记录数正确显示,但表为空。 Not sure how to go about iterating all of these objects and mapping their respective field names to the table. 不确定如何迭代所有这些对象并将它们各自的字段名称映射到表。 Please help. 请帮忙。

The JSON which is returned has "iTotalRecords": 100, "iTotalDisplayRecords": 100, which are actually causing problem. 返回的JSON具有“ iTotalRecords”:100,“ iTotalDisplayRecords”:100,这实际上是引起问题的原因。 Your Json should an array of objects which has a values to entered in the datatable. 您的Json应该是一个对象数组,该数组具有要在数据表中输入的值。 You can either create a new array and store ["aaData" : [ { "billAccountName": "FAKE CORP", "customerNumber": "123", "customerName": "Fake", "serviceLocationName": "Fake Corp", "serviceLocationAddress": "PO BOX 123 Denver CO", "serviceComponentProductName": "Product X" }, { "billAccountName": "REAL CORP", "customerNumber": "456", "customerName": "Real", "serviceLocationName": "Real Corp", "serviceLocationAddress": "PO BOX 456 Ft Collins CO", "serviceComponentProductName": "Product Z" }, {...},]] or the JSON returned should be like this(strictly in this format): 您可以创建一个新数组并存储[“ aaData”:[{“ billAccountName”:“ FAKE CORP”,“ customerNumber”:“ 123”,“ customerName”:“ Fake”,“ serviceLocationName”:“ Fake Corp”, “ serviceLocationAddress”:“邮政信箱123丹佛”,“ serviceComponentProductName”:“产品X”},{“ billAccountName”:“ REAL CORP”,“ customerNumber”:“ 456”,“ customerName”:“真实”,“ serviceLocationName “:” Real Corp“,” serviceLocationAddress“:”邮政信箱456 Ft Collins CO“,” serviceComponentProductName“:”产品Z“},{...},]]或返回的JSON应该像这样(严格地在此格式):

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

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

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