1

我正在初始化一个 dataTable 并使用 ajax 函数来加载数据。服务器端是spring mvc + hibernate。

以下是初始化数据表的代码

$('.hist_data').on('click', function(){
    $('.myTable').dataTable( {
        "bProcessing": true,
        "bServerSide": true,
        "bJQueryUI": true,
        "sAjaxSource": "/application-monitor/getTableData",
        "fnServerData" : function(sSource, aoData, fnCallback) {
            request = $.ajax({
                "dataType" : "json",
                "type" : "POST",
                "url" : sSource,
                "data" : aoData,
                "success" : fnCallback
            });
        },
        "aoColumns": [
            { "mData": "id" },
            { "mData": "name" },]
    } );

});

/application-monitor/getTableData 返回一个对象列表(一个具有 id 和 name 的 java bean,它们是我的列)。

当我运行它时,我在页面中看不到任何数据,我也使用 firebug 对其进行调试,但在初始化时看不到任何问题。

任何帮助表示赞赏。

4

2 回答 2

2

Your code works fine for me as it is.

I created some basic markup like this:

<div class="hist_data">Click here</div>
<table class="myTable"></table>

And I made a test backend that returned json like this:

{"aaData": [
  {"id":"1","name":"One"},
  {"id":"2","name":"Two"}
]}

Combined with the javascript you provided, and some links to the jquery and dataTables scripts, that was all I had to do to make it work.

I suspect the problem is in your backend code. In particular, note that you should be returning an object with an aaData field containing the array of items. You can't just return the array by itself.

于 2013-07-05T23:36:43.050 回答
1

我不确定,但首先你需要检查 firebug 或 devtools 是否正确获取数据。如果是,那么您的代码可能是错误的。我从文档中复制了这个:

"fnServerData": function ( sSource, aoData, fnCallback, oSettings ) {
    oSettings.jqXHR = $.ajax( {
        "dataType": 'json', 
        "type": "POST", 
        "url": sSource, 
        "data": aoData, 
        "success": fnCallback
   });
},//....

我不知道request您的代码中有什么,但请尝试上面的代码以确定。

于 2013-07-05T22:50:34.857 回答