0

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. On the UI, i get to date parameters and make a ajax call to the servlet. The servlet will process and return the json data. 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. 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:

[{"user_id":"[email protected]","executed_at":"Jul 8, 2013 7:22:59 PM"}]

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.