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. 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']]
.
我正在尝试升级我的系统以使用1.10而不是1.9的DataTables,我正在尝试找到一种方法来使用JSON对象而不是列表来传回行内容。具体而言,不是以[['数据','数据','数据'],['数据','数据','数据']等格式传回数据。]我想把它放在格式中[[ '可乐': '数据', 'COLB': '数据', 'COLC': '数据']]。
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. 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?
结果正确地从我的AJAX函数返回,但DataTables试图在第0行中找到0的索引并且未能找到它,因为我的表格单元格被列名而不是数字索引索引。有谁知道如何告诉DataTables使用columnDefs(或其他一些我没有找到的选项)中指定的列名而不是数字索引?
3 个解决方案
#1
9
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"
});
#2
3
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'];
});
},
});
#3
0
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)};
}
},
#1
9
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"
});
#2
3
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'];
});
},
});
#3
0
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)};
}
},