数据表和Ajax数据格式化?
我正在使用数据表,并且希望能够发送AJAX请求以获取我的数据.
I am using Datatables and I want to be able to send an AJAX request to get my data.
我的jQuery-
$('.valid-tags').DataTable( {
"ajax": {
"url": "/ajax/getValidTags.php",
"type": "POST",
"data": {
ruleID: ruleID
}
}
} );
这是从ajax请求返回的数据-
{"data":["K":2,"B":1,"C":2]}
我希望在自己的行中的标签名称下看到"K","B","C".
Im expecting to see 'K', 'B', 'C' under tag name in their own rows.
我的数据表还没有加载任何数据吗?
My Datatables doesn't load any data though?
我需要能够将每个键值对包装在其自己的数组中,这样-
I need to be able to wrap each key value pair in its own array so this -
{"data":["K":2,"B":1,"C":2]}
将会-
{"data":[["K":2],["B":1],["C":2]]}
这是构建它的PHP(我在哪里将键值包装在对象中,就像上面那样)?-
This is the PHP that builds it (where do i wrap the key values in an object so it like the above)?-
$validTagsArray = array();
$validArray = array();
foreach ($cursor as $key => $value) {
foreach ($value['AutoFix'] as $k => $v) {
$x = 0;
foreach ($v as $key => $value) {
$x++;
$validValueCount = $validTagsArray[$k] = $x;
}
}
}
$validArray['data'] = array($validTagsArray);
echo json_encode($validArray);
更改返回的JSON格式,如下所示.有关更多信息,请参见数据源类型.
Change the format of the JSON that you're returning as shown below. See Data source types for more information.
{
"data": [
[ "K", 2 ],
[ "B", 1 ],
[ "C", 2 ]
]
}
如下所示更改初始化选项.
Change your initialization options as shown below.
$('.valid-tags').DataTable( {
"ajax": {
"url": "/ajax/getValidTags.php",
"type": "POST",
"data": {
ruleID: ruleID
}
},
"columnDefs": [{
"targets": 2,
"render": function(data, type, full, meta){
return '<button type="button">Manage</button>';
}
}]
} );
有关代码和演示,请参见此jsFiddle .
See this jsFiddle for code and demonstration.