2014-04-28 64 views
0

这是我第一次尝试datatables。我想调用ajax函数从数据库返回数据(MariaDB),并填充php文件中现有tablerowscells没有数据从ajax调用返回给jquery数据表

有关数据域代码的简化版本是这样的:

$('#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没有经验不知道是否使用aDataaaData,还是什么尝试更多。

回答

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来启动表格数据。
除此之外,您可以返回更多选项(如sEchoiTotalRecords,iTotalDisplayRecords,...)以完成渲染。

+0

有一些我必须错过...我用你的例子。在'getLines'中''Php'函数做了一个'回应'你的回答';'并且逃脱了引号。在firebug中,作为对'http'' GET'方法的响应,有:'{“aaData”:[“Trident”,“Internet Explorer 4.0”,“Win 95 +”,“4”], [ “Trident”,“Internet Explorer 5.0”,“Win 95 +”,“5”], [“Trident”,“Internet Explorer 5.5”,“Win 95 +”,“5.5”], [“Trident” Internet Explorer 6“,”Win 98 +“,”6“], [”Trident“,”Internet Explorer 7“,”Win XP SP2 +“,”7“] ]。但仍然得到'TypeError:aData是未定义的'错误。 – Luis

+0

您是否尝试从示例(通过复制源代码)开始,并试图替换您需要的内容? – PoulsQ

+0

这个例子不像我打算做的那样:''sAjaxSource“:'../ examples_support/json_source.txt',而不是使用'ajax'调用一个php函数。 – Luis

0

Datatables.net示例不能与MariaDB服务器一起使用(对于使用不同形式的root用户登录不同的旧密码加密)。我不知道它为什么没有,但它不响应任何数据(ajax)。在Windows MariaDB服务器上进行测试。

解决方案:如果您不想卸载MariaDB服务器并迁移(导出/导入)数据,请在不同的端口上安装MySQL服务器服务/守护程序。

+0

真正的解决方案:在MariaDB/MySQL连接字符串上进行明确的字符集定义(例如:charset = utf8)。 – Titonus