这是我第一次尝试使用datatables
。我想调用ajax
函数从数据库(MariaDB
)返回数据,并填充现有rows
中的cells
和table
php
档案。
有关数据字段的代码的简化版本如下:
$('#tbl_resultados').dataTable( {
"bProcessing": true,
"sAjaxSource": "/apls/indicadores/indic_acess.php",
"sAjaxDataProp": "data",
"fnServerData": function ( sSource, aoData, fnCallback ) {
request = $.ajax({
"dataType": 'json',
"type": "GET",
"url": sSource,
"data":{
"fn": "lines",
"indic_per": indic_per,
},
"success": fnCallback
});
},
"aoColumns": [
{ "mDataProp": "id"},
{ "mDataProp": "per"},
],
} );
我尝试使用和不使用"bServerSide": true,
,但无法在table
中显示数据。 Firebug
会返回TypeError: aData is undefined
。
按照the example中的说法,我在php
中构建了一个返回json
类似结构的函数。
function getLines()
{
echo "{ \"aData\":[
[
\"25983\",
\"2010\"
],
[
\"90420\",
\"2011\"
]
]
}";
}
就像我说的那样,我没有使用datatables
的经验,不知道是使用aData
还是aaData
,还是要尝试更多。
答案 0 :(得分:0)
这是ajax源的另一个例子:
http://datatables.net/examples/data_sources/ajax.html
它说:
DataTables expects an object with an array called "aaData" with the data source.
以下是他们的一些ajax回报:
{ "aaData": [
["Trident","Internet Explorer 4.0","Win 95+","4","X"],
["Trident","Internet Explorer 5.0","Win 95+","5","C"],
["Trident","Internet Explorer 5.5","Win 95+","5.5","A"],
["Trident","Internet Explorer 6","Win 98+","6","A"],
["Trident","Internet Explorer 7","Win XP SP2+","7","A"],
.
.
.
["Misc","Links","Text only","-","X"],
["Misc","Lynx","Text only","-","X"],
["Misc","IE Mobile","Windows Mobile 6","-","C"],
["Misc","PSP browser","PSP","-","C"],
["Other browsers","All others","-","-","U"]
] }
就我而言,我总是使用aaData
来发起表格数据
除此之外,您还可以返回更多选项(例如sEcho
,iTotalRecords
,iTotalDisplayRecords
,...)以完成渲染。
答案 1 :(得分:0)
Datatables.net示例不适用于MariaDB服务器(对于使用不同形式的root~旧密码加密的用户登录相同)。我不知道它为什么没有,但它没有响应任何数据(ajax)。在Windows MariaDB服务器上测试。
解决方案:如果您不想卸载MariaDB服务器,并且迁移(导出/导入)数据,请在不同端口上安装MySQL服务器服务/守护程序。