javascript 将json数组转换为aadata格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5105731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
convert json array to aadata format
提问by glenn
my currrent array format is not being interpreted by datatables aaData format as im passing column values:
我当前的数组格式没有被数据表 aaData 格式解释为我传递列值:
{
"aaData": [
{
"startDate": "09/08/2010 12:00:00 AM",
"endDate": "13/08/2010 12:00:00 AM",
"runDate": "16/08/2010 12:00:00 AM",
"clientId": "40272",
"clientType": "C",
"plannerName": "Adrian Mcfly",
"plannerRegion": "s1",
"contact": "Vera chaniqua",
"email": " ",
"interviewDate": "09/08/2010 12:00:00 AM"
},
]
}
how do i remove the column id and display just the values so that i can be read by datatables as a ajax call?
我如何删除列 id 并只显示值,以便我可以作为 ajax 调用被数据表读取?
回答by Drew H
EDIT 8/29/2012
编辑 8/29/2012
As of 1.9 you can disable having to have a root JSON property.
从 1.9 开始,您可以禁用必须拥有根 JSON 属性。
"sAjaxDataProp": "",
This is likely what you will get with most JSON serializers.
这可能是大多数 JSON 序列化程序的结果。
Or customize it
或者自定义
"sAjaxDataProp": "myData",
In datatables 1.8 you can format your json like this:
在数据表 1.8 中,您可以像这样格式化您的 json:
{
"aaData": [
{
"DT_RowClass": "",
"description": "",
"pkgLineTree": {
"treeId": {
"name": "Hymansonville"
}
}
},
{
"DT_RowClass": "",
"description": "",
"pkgLineTree": {
"treeId": {
"name": "Hymansonville"
}
}
}
]
}
And in your datatable properties add this
在你的数据表属性中添加这个
"aoColumns": [
{
"mDataProp": "pkgLineTree.treeId.name"
},
{
"mDataProp": "shortname"
},
{
"mDataProp": "description"
},
{
"mDataProp": "lineStatus"
}
],
回答by david
Well, basically what you've got there is a JSON representation of an array of objects(with properties startDate, endDate, ...), but what you need is an array of arraysof strings.
好吧,基本上你得到的是一个对象数组的 JSON 表示(具有属性startDate、endDate,...),但你需要的是一个字符串数组数组。
I'm assuming you are doing server-side processing, so if you don't want to change the server code, you can get just in the middle of the fetching process and modify the data right before giving it to the callback of datatables.
我假设您正在进行服务器端处理,因此如果您不想更改服务器代码,您可以在获取过程的中间修改数据,然后将其提供给数据表的回调。
What I do next is just going through each object in the fetched data and create the array of values:
我接下来要做的只是遍历获取的数据中的每个对象并创建值数组:
$('#example').dataTable( {
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "scripts/server_processing.php",
"fnServerData": function ( sSource, aoData, fnCallback ) {
$.getJSON( sSource, aoData, function (json) {
/* --- Here is where we massage the data --- */
/* if the variable "json" is just a string (I forgot) then evaluate it first into an object (download a json library)*/
var aaData=[];
$.each(json, function(index, object) {
var aData=[];
$.each(object, function(key, value) {
aData.push(value); //be careful here, you might put things in the wrong column
});
aaData.push(aData);
});
/* --- And after we're done, we give the correctly formatted data to datatables --- */ /* --- if "json" was a string and not an object, then stringify aaData first (object to json notation, download a library) --- */
fnCallback(aaData)
} );
}
} );
} );
});
Hope it works!
希望它有效!
回答by qais
Try using square brackets instead of opening/closing curly braces { and }. For details, see my answer to this question: datatables and json formatting error with php
尝试使用方括号而不是打开/关闭大括号 { 和 }。有关详细信息,请参阅我对这个问题的回答:datatables and jsonformatting error with php

