将json数组转换为数据格式
我的currrent数组格式没有被datatables aaData格式解释为im传递列值:
{ "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" }, ] }
如何删除列id并仅显示值,以便我可以将数据表作为ajax调用读取?
编辑2012年8月29日
从1.9开始,您可以禁用必须具有根JSON属性。
"sAjaxDataProp": "",
这可能是大多数JSON序列化程序的结果。
或者自定义它
"sAjaxDataProp": "myData",
在datatables 1.8中,你可以像这样格式化你的json:
{ "aaData": [ { "DT_RowClass": "", "description": "", "pkgLineTree": { "treeId": { "name": "Jacksonville" } } }, { "DT_RowClass": "", "description": "", "pkgLineTree": { "treeId": { "name": "Jacksonville" } } } ] }
在您的数据表属性中添加此项
"aoColumns": [ { "mDataProp": "pkgLineTree.treeId.name" }, { "mDataProp": "shortname" }, { "mDataProp": "description" }, { "mDataProp": "lineStatus" } ],
好吧,基本上你所拥有的是一个对象数组的JSON表示(具有属性startDate , endDate ,…),但你需要的是一个字符串数组数组 。
我假设您正在进行服务器端处理,因此如果您不想更改服务器代码,您可以在获取过程中立即修改数据,然后再将其提供给数据表的回调。
我接下来要做的就是遍历获取的数据中的每个对象并创建值数组:
$('#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) } ); } } );
});
希望它有效!
尝试使用方括号而不是打开/关闭花括号{和}。 有关详细信息,请参阅我对此问题的回答: 使用php的datatables和json格式错误