简体   繁体   English

通过Ajax用来自servlet的数据加载JQuery Datatables

[英]Load the JQuery Datatables with the data from the servlet via Ajax

I have looked for similar posts here, but i couldn't find post that matches my requirements. 我在这里寻找过类似的职位,但找不到符合我要求的职位。 I am trying to display the jquery datatables . 我正在尝试显示jquery数据表 On the UI, i get to date parameters and make a ajax call to the servlet. 在UI上,我获取了最新的参数,并对servlet进行了ajax调用。 The servlet will process and return the json data. Servlet将处理并返回json数据。 Once i get the data i want to show the results in the datatables. 一旦获得数据,我想在数据表中显示结果。 But my code is not working. 但是我的代码无法正常工作。 I am new to Datatables. 我是Datatables的新手。 Here's my code : 这是我的代码:

function fetchLogs(){
$.ajax({
    type: "POST",
    url: "LogsServlet",
    data: 'FromDate='+from+'&'+'ToDate='+to,
    dataType: 'json',
    success: function(data){
        /*$('#logs').dataTable({
            "aaData": data,
            "aoColumns": [{ "mDataProp": "Executed_AT" }, { "mDataProp": "User_Name"}]
        });*/
        $('#logs').dataTable( {
            "bProcessing": true,
            "sAjaxSource": data
        } );

    }

});

}

The json data that the servlet returns: servlet返回的json数据:

[{"user_id":"rams.orvz@xyz.com","executed_at":"Jul 8, 2013 7:22:59 PM"}]

Got the solution. 得到了解决方案。 Here's the code that solved it: 这是解决它的代码:

 function fetchLogs(){
$.ajax({
    type: "POST",
    url: "LogsServlet",
    data: 'FromDate='+from+'&'+'ToDate='+to,
    dataType: 'json',
    success: AjaxFetchDataSucceeded,
    error: AjaxFetchDataFailed

});

}

function AjaxFetchDataSucceeded(result) {
    if (result != "[]") {
        //var dataTab = $.parseJSON(result);
        $('#logs').dataTable({
            "bProcessing": true,
            "aaData": result,
            //important  -- headers of the json
            "aoColumns": [{ "mDataProp": "user_id" }, { "mDataProp": "executed_at" }],
            "sPaginationType": "full_numbers",
            "aaSorting": [[0, "asc"]],
            "bJQueryUI": true

        });
    }
}

function AjaxFetchDataFailed(result) {
    alert(result.status + ' ' + result.statusText);
}

However the displayed data tables are totally awkward in terms of appearance. 但是,显示的数据表在外观上完全尴尬。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM