问题描述
我最近在实现 jquery DataTables 的 ajax 功能时遇到了一个问题.直到我真的给我的 json 对象集合一个明确的名字,我才能显示任何东西.如果没有named返回,不应该有一个默认的数据源吗?
I recently ran into a problem when implementing the ajax functionality of jquery DataTables. Until I actually gave my json object collection an explicit name I couldn't get anything to display. Shouldn't there be a default data source if nothing named is returned?
客户端控件设置(包括向动态锚点提供数据的隐藏字段:
Client Side control setup (includes hidden field that supplies data to dynamic anchor:
$('#accountRequestStatus').dataTable(
{
"destroy": true, // within a method that will be called multiple times with new/different data
"processing": true,
"ajax":
{
"type": "GET",
"url": "@Url.Action("SomeServerMethod", "SomeController")",
"data": { methodParam1: 12341, methodParam2: 123423, requestType: 4123421 }
}
, "paging": false
, "columns": [
{ "data": "DataElement1" },
{ "data": "DataElement2", "title": "Col1" },
{ "data": "DataElement3", "title": "Col2" },
{ "data": "DataElement4", "title": "Col3" },
{ "data": "DataElement5", "title": "Col4" },
]
, "columnDefs": [
{
"targets": 0, // hiding first column, userId
"visible": false,
"searchable": false,
"sortable": false
},
{
"targets": 5, // creates action link using the hidden data for that row in column [userId]
"render": function (data, type, row) {
return "<a href='@Url.Action("ServerMethod", "Controller")?someParam=" + row["DataElement1"] + "'>Details</a>"
},
"searchable": false,
"sortable": false
}
]
});
这是返回 json 集合的服务器端代码片段.
tableRows 是包含要显示的数据的模型集合.
Here's a snippet of my server side code that returns the json collection.
tableRows is a collection of models containing the data to be displayed.
var json = this.Json(new { data = tableRows });
json.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
return json;
正如我之前所说,ajax 调用返回了数据,但在我为集合命名之前不会显示.也许我错过了文档中的这个必需步骤,但是将控件连接到单个返回的集合作为默认数据源而不需要名称是否有意义?弄清楚名称的事情相当于大约 2 个多小时的混乱尝试不同的事情.这就是我要说的.
As I said before, the ajax call returned data but wouldn't display until I gave the collection a name. Maybe I missed this required step in the documentation, but wouldn't it make sense for the control to wire up to a single returned collection as the default data source and not require the name? Figuring out the name thing equated to about 2+ hours of messin' around trying different things. That's all I'm saying.
也许这对其他人也有帮助...
Maybe this'll help someone else too...
推荐答案
dataTables 实际上有一个 dataSrc
属性!dataTables 将在 JSON 中查找 data
或 aaData
部分.这就是为什么你最终让它与 new { data=tableRows }
一起工作的原因.即if dataSrc
没有指定!如果您的 JSON 与该概念不同,您必须指定 dataSrc
:
dataTables does actually have a dataSrc
property! dataTables will look for either a data
or an aaData
section in the JSON. Thats why you finally got it to work with new { data=tableRows }
. That is, if dataSrc
is not specified! If your JSON differs from that concept you must specify dataSrc
:
如果你返回一个未命名的数组/集合 [{...},{...}]
:
If you return a not named array / collection [{...},{...}]
:
ajax: {
url: "@Url.Action("SomeServerMethod", "SomeController")",
dataSrc: ""
}
如果你返回一个不同于 data
或 aaData
的 JSON 数组,比如 customers
:
If you return a JSON array named different from data
or aaData
, like customers
:
ajax: {
url: "@Url.Action("SomeServerMethod", "SomeController")",
dataSrc: "customers"
}
如果内容像 { a : { b : [{...},{...}] }}
ajax: {
url: "@Url.Action("SomeServerMethod", "SomeController")",
dataSrc: "a.b"
}
如果您有非常复杂的 JSON 或需要以任何方式操作 JSON,例如从内容中挑选樱桃 - dataSrc
也可以是一个函数:
If you have really complex JSON or need to manipulate the JSON in any way, like cherry picking from the content - dataSrc
can also be a function :
ajax: {
url: "@Url.Action("SomeServerMethod", "SomeController")",
dataSrc: function(json) {
//do what ever you want
//return an array containing JSON / object literals
}
}
希望以上能解决问题!
这篇关于DataTables ajax 需要为返回的数据源显式 json 集合名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!