[英]Using column names with DataTables with AJAX data source
I'm trying to upgrade my system to use 1.10 instead of 1.9 of DataTables and I'm trying to find a way to pass back the row contents using a JSON object instead of a list. 我正在尝试升级我的系统使用1.10而不是1.9的DataTables,我正在尝试找到一种方法来使用JSON对象而不是列表来传回行内容。 Specifically instead of passing back data in the format
[['data','data','data'],['data','data','data'],etc..]
I want to put it in the format [['colA':'data','colB':'data','colC':'data']]
. 具体而言,不是以
[['data','data','data'],['data','data','data'],etc..]
格式传回数据[['data','data','data'],['data','data','data'],etc..]
我想把它放在格式中[['colA':'data','colB':'data','colC':'data']]
。
Right now I've got my AJAX function returning the data in that format and I'm trying to initialize with this code: 现在我已经让我的AJAX函数以该格式返回数据,我正在尝试使用以下代码进行初始化:
$("table").DataTable({
"columnDefs": [
{"name": "wo_status", "title": "wo_status", "targets": 0},
//repeat for each of my 20 or so fields
],
"serverSide": true,
"ajax": "url/to/ajax/function"
});
The results are coming back from my AJAX function correctly but DataTables is trying to find an index of 0 in row 0 and failing to find it because my table cells are indexed by their column name instead of a numerical index. 结果正确地从我的AJAX函数返回,但是DataTables试图在第0行中找到0的索引并且未能找到它,因为我的表格单元格被列名而不是数字索引索引。 Does anyone know how to tell DataTables to use the column names specified in
columnDefs
(or in some other option I haven't found) instead of numerical indices? 有谁知道如何告诉DataTables使用
columnDefs
(或其他一些我没有找到的选项)中指定的列名而不是数字索引?
Use columns.data
option to specify property names as shown below: 使用
columns.data
选项指定属性名称,如下所示:
$("table").DataTable({
"columns": [
{ "data": "colA", "name": "colA", "title": "colA" },
{ "data": "colB", "name": "colB", "title": "colB" },
{ "data": "colC", "name": "colC", "title": "colC" }
//repeat for each of my 20 or so fields
],
"serverSide": true,
"ajax": "url/to/ajax/function"
});
Use forEach in fnServerParams function... 在fnServerParams函数中使用forEach ...
$("table").DataTable({
"columns": [{
"data": "colA"
}, {
"data": "colB"
}, {
"data": "colC"
}],
"serverSide": true,
"ajax": "url/to/ajax/function",
fnServerParams: function(data) {
data['order'].forEach(function(items, index) {
data['order'][index]['column'] = data['columns'][items.column]['data'];
});
},
});
Thanks @ahmeti I've updated your approach :) 谢谢@ahmeti我已经更新了你的方法:)
ajax: {
url: fetchUrl,
data: function ( data ) {
data['columns_map'] = {};
data['columns'].forEach(function(item, index) {
data['columns_map'][item.data] = data['columns'][index];
});
data['order'].forEach(function(item, index) {
data['order'][index]['column'] = data['columns'][item.column]['data'];
});
return {"data": JSON.stringify(data)};
}
},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.