【发布时间】:2014-11-13 04:03:39
【问题描述】:
我正在努力从我的控制器获取序列化的 JSON 数据以显示在数据表中。我以前能够在将数据作为二维数组发送时显示数据。现在我希望能够使用我的对象并将其与列匹配(如第二个 JavaScript 所示)。我试图关注this solution,但在查看 Datatables 文档后我看不出有什么问题。
更新 请参阅下面我目前拥有的。我试图让数据通过命名参数通过,因为我希望父行带有子项列表。这些项目在选择时可见,如 this DataTables link 所示。虽然我仍然无法通过 JSON 数据让表格使用命名参数。
数据:
for (int i = 0; i < 26; i++)
{
list.Add(new MembershipTransactionHistoryModel
{
TransactionDate = "01 May 2014",
StoreName = "Store Name",
CardNumber = "23423566",
TransactionType = "Purchase",
TransactionValue = "$134.25",
PointsEarned = "100",
PointsUsed = "23",
PointsBalance = "40000"
});
}
//return new JavaScriptSerializer().Serialize(list);
return from l in list
select new[] {
l.TransactionDate,
l.StoreName,
l.CardNumber,
l.TransactionType,
l.TransactionValue,
l.PointsEarned,
l.PointsUsed,
l.PointsBalance
};
JavaScript:
var grid = $('#transactionHistoryTable').dataTable({
"bProcessing": true,
"sAjaxSource": 'MembershipTransactionHistoryData',
"fnServerParams": function (aoData) {
aoData.push({ "name": "membershipId", "value": 7 })
},
"aoColumnDefs": [
{
"sTitle": "",
"class": 'details-control',
"orderable": false,
"mData": null,
"aTargets": [0],
"defaultContent": ''
},
{ "sTitle": "Transaction Date", "mData" : "TransactionDate", "aTargets": [1]},
{ "sTitle": "Store Name", "mData": "StoreName", "aTargets": [2] },
{ "sTitle": "Card Number", "mData": "CardNumber", "aTargets": [3] },
{ "sTitle": "Type", "mData": "Type", "aTargets": [4] },
{ "sTitle": "Value", "mData": "Value", "aTargets": [5] },
{ "sTitle": "Points Earned", "mData": "PointsEarned", "aTargets": [6] },
{ "sTitle": "Points Used", "mData": "PointsUsed", "aTargets": [7] },
{ "sTitle": "Points Balance", "mData": "PointsBalance", "aTargets": [8] }
],
"paginate": true,
"scrollY": maxHeight,
"scrollCollapse": false,
"sort": true
});
HTML:
<table id="transactionHistoryTable" class="display" cellspacing="0">
<thead>
<tr>
</tr>
</thead>
<tbody>
</tbody>
</table>
产生的错误:
DataTables warning: table id=transactionHistoryTable - Requested unknown parameter 'TransactionDate' for row 0. For more information about this error, please see http://datatables.net/tn/4
【问题讨论】:
-
是的,您的 JSON 不正确。如果您的表中有任何其他额外的列,那么您还需要传递数据(至少为空)。我看到有一个空列
<th></th>,因为您需要从服务器调用返回数据。这就是为什么error of undefined。 -
@RJK 我已经完全删除了
元素,因为我现在正在根据 aoColumnDefs JavaScript 定义我的列。我将更新我的问题,使其更简洁并显示我目前拥有的内容。
标签: asp.net-mvc json datatables