DataTables 警告 - 请求第 0 行第 0 列的未知参数“0”
Posted
技术标签:
【中文标题】DataTables 警告 - 请求第 0 行第 0 列的未知参数“0”【英文标题】:DataTables warning - Request unknown parameter '0' for row 0, column 0 【发布时间】:2016-09-24 08:30:23 【问题描述】:我正在使用 jQuery DataTable 来显示使用存储过程和 Web 服务从数据库中获取的数据。我可以很好地使用 Fiddler 运行 SP 或服务,但是在填充 DataTable 时,我得到了错误记录 here。在我的具体情况下,消息是:
“DataTables 警告:表 id=tblCashRecord - 请求第 0 行第 0 列的未知参数‘0’”
然后发生的事情是我的 DataTable 显示了正确的行数,但所有单元格都是空的。
我很确定 html 表中的列数与我使用 aoColumns 推动的列数相同(它是四个),但我可能错了。我知道有很多相同的问题被问到,但this 是我发现唯一有用的可能相关的问题,而且我也尝试过this,但没有成功。
我的 HTML 表格:
<table id="tblCashRecord" class="table table-bordered">
<thead>
<tr>
<th>Kiosk Name</th>
<th>Service Type</th>
<th>Transaction Timestamp</th>
<th>Amount (RM)</th>
</tr>
</thead>
<tbody>
</tbody>
<tfoot>
<tr>
<th colspan="3" style="text-align: right">Total:</th>
<th><span id="totalAmount" style="margin-left: -8px;"></span></th>
</tr>
</tfoot>
</table>
我的 javascript:
$.ajax(
type: "POST",
url: "../Services/Report.svc/GetCashPaymentRecord/?s=" + session + "&r=" + reference,
data: "\"kioskID\":" + JSON.stringify(kioskID) + "," + "\"startDate\": " + JSON.stringify(startDate) + "," + "\"endDate\":" + JSON.stringify(endDate) + "",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response)
if (response.success == true)
if (response.cashPayment != null && response.cashPayment.length > 0)
cashList = response.cashPayment;
var data = "aaData": [] ;
$('#tblCashRecord').dataTable().fnClearTable();
$('#tblCashRecord').dataTable().fnDestroy();
$.each(response.cashPayment, function (item)
data.aaData.push(
"id": item.id,
"kioskName": item.kioskName,
"total": item.total,
"transactionTimestamp": moment.utc(item.transactionTimestamp).format("DD-MM-YY HH:mm:ss"),
"serviceType": item.serviceType,
"paymentType": item.paymentType, //remove later in SP
"paymentRecordID": item.paymentRecordID
);
);
table = $('#tblCashRecord').DataTable(
"paging": false,
"ordering": false,
"bAutoWidth": false,
"bSortable": false,
"bFilter": false,
"bInfo": false,
dom: 'Blfrtip',
"aaData": data.aaData,
"aaColumns": [
"mData": "kioskName" ,
"mData": "serviceType" ,
"mData": "transactionTimestamp" ,
"mData": "total"
]
);
$('#tblCashRecord tbody tr').each(function ()
var col = $(this).find('td:eq(3)').html();
total += parseFloat(col);
);
$('#totalAmount').html(total.toFixed(2));
$('.sorting_asc').removeClass('sorting_asc');
else
$('#tblCashRecord').dataTable().fnClearTable();
$('#tblCashRecord').dataTable().fnDestroy();
$('#tblCashRecord').dataTable(
"paging": false,
"ordering": false,
"bAutoWidth": false,
"bSortable": false,
"bFilter": false,
"bInfo": false,
"oLanguage":
"sEmptyTable": "No Record Found."
);
$('#totalAmount').html("");
$('.sorting_asc').removeClass('sorting_asc');
else
$('#tblCashRecord').dataTable().fnClearTable();
$('#tblCashRecord').dataTable().fnDestroy();
$('#tblCashRecord').dataTable(
"paging": false,
"ordering": false,
"bAutoWidth": false,
"bSortable": false,
"bFilter": false,
"bInfo": false,
"oLanguage":
"sEmptyTable": "Error: Could not load data."
);
$('#totalAmount').html("");
$('.sorting_asc').removeClass('sorting_asc');
);
我的网络服务:
cashPaymentResponse IReport.GetCashPaymentRecord(string session, string reference, cashPaymentRequest request)
Guid sessionID, referenceID, kioskID;
Guid.TryParse(session, out sessionID);
Guid.TryParse(reference, out referenceID);
Guid.TryParse(request.kioskID, out kioskID);
if (sessionID == Guid.Empty)
return new cashPaymentResponse("Invalid Session.");
DateTime startDate, endDate;
try
startDate = new DateTime(Convert.ToInt32(request.startDate.Substring(6, 4)), Convert.ToInt32(request.startDate.Substring(3, 2)), Convert.ToInt32(request.startDate.Substring(0, 2)), 0, 0, 0);
endDate = new DateTime(Convert.ToInt32(request.endDate.Substring(6, 4)), Convert.ToInt32(request.endDate.Substring(3, 2)), Convert.ToInt32(request.endDate.Substring(0, 2)), 23, 59, 59);
catch (Exception ex)
return new cashPaymentResponse("No Date Selected.");
List<ReportCashPaymentRecord_Result> result;
try
using (MyDBEntities context = new MyDBEntities())
result = context.ReportCashPaymentRecord(sessionID, kioskID, startDate, endDate).ToList();
catch (Exception ex)
if (isDebug() == false)
return new cashPaymentResponse("Database connection failed.");
else
return new cashPaymentResponse(ex.Message);
if (result.Count > 0)
cashPaymentResponse response = new cashPaymentResponse();
cashPaymentItem item;
response.cashPayment = new List<cashPaymentItem>();
for (int i = 0; i < result.Count; i++)
item = new cashPaymentItem();
if (result[i].kioskName == "session")
return new cashPaymentResponse("Invalid Session.");
else
item.id = (Guid)result[i].cashID;
item.paymentRecordID = (Guid)result[i].paymentRecordID;
item.total = (decimal)result[i].total;
item.transactionTimestamp = JsonConvert.SerializeObject(new DateTime(result[i].transactiontimestamp.Value.Year, result[i].transactiontimestamp.Value.Month, result[i].transactiontimestamp.Value.Day, result[i].transactiontimestamp.Value.Hour, result[i].transactiontimestamp.Value.Minute, result[i].transactiontimestamp.Value.Second, 0, DateTimeKind.Utc)).Replace("\"", "");
item.kioskName = result[i].kioskName;
item.serviceType = (result[i].serviceType.ToString() == "0") ? "Assessment" : (result[i].serviceType.ToString() == "1") ? "Water Bill" : (result[i].serviceType.ToString() == "2") ? "Rental" : (result[i].serviceType.ToString() == "3") ? "Compound" : "None";
item.paymentType = (result[i].paymentType.ToString() == "1") ? "Cash" : (result[i].paymentType.ToString() == "3") ? "Credit Card" : (result[i].paymentType.ToString() == "2") ? "Cheque" : "None";
response.cashPayment.Add(item);
return response;
else
return new cashPaymentResponse();
我的 JSON 响应(实际上有 8 个条目,但我只包括第一个以减少混乱):
"success": true,
"cashPayment": [
"id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"kioskName": "00001",
"paymentRecordID": "yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy",
"paymentType": "Cash",
"serviceType": "Water Bill",
"total": 100,
"transactionTimestamp": "2016-01-21T10:15:21Z"
]
编辑:我已编辑上述响应中的 GUID 以删除敏感信息。
【问题讨论】:
【参考方案1】:经典的匈牙利符号拼写错误。 aaColumns
应该是 aoColumns
- 用于 array object。然后我相信它会起作用。 aoColumns
是 1.10.x 的顺便说一句,现在称为 columns
(但仍然支持这两个名称)。
一般来说,使用新的 1.10.x 驼峰命名约定,请跳过所有围绕文字名称的引用:
table = $('#tblCashRecord').DataTable(
paging: false,
ordering: false,
autoWidth: false,
sortable: false,
filter: false,
info: false,
dom: 'Blfrtip',
data: data.aaData,
columns: [
data: "kioskName" ,
data: "serviceType" ,
data: "transactionTimestamp" ,
data: "total"
]
);
更新每条评论。更正“aaColumn
”后出现空单元格的原因是您使用了错误的方式$.each
:
$.each(response.cashPayment, function (item)
应该是
$.each(response.cashPayment, function (index, item) //<----
data.aaData.push(
"id": item.id,
你无意中试图从索引中取出属性,而不是对象。
【讨论】:
感谢您的建议,我已将 aaColumns 编辑为 aoColumns,但它仍然给出相同的错误,只是现在 transactionTimestamp 列已填满,全部使用当前日期时间而不是它们所在的日期时间应该有。我还将 aoColumns 更改为列,将 mData 更改为数据,没有任何变化。 @Alycus,查看更新 - 你使用$.each
的方式错误。
就是这样,我没想过将索引也传递到 $.each 中。非常感谢。以上是关于DataTables 警告 - 请求第 0 行第 0 列的未知参数“0”的主要内容,如果未能解决你的问题,请参考以下文章
DataTables 警告:从数据源请求未知参数 '4' 用于行 ''
DataTables 错误:从第 0 行的数据源请求未知参数“1”