1

I'm using DataTables plugin to work with my table. I want to set JSON object as a datasource for my table and update the content every time when this object is changed.

To initialize my table I use the following code:

     var obj = [];

     // if I make start initialization of the object - the row in a table appears.
     //var obj = [{"Name": "name1", "Id": "id1", "Value":"value1"}];

     var table;

        $(document).ready(function () {

            table = $('#example').dataTable({
                "sPaginationType": "full_numbers",
            "sScrollY": "250px",
            "aaSorting": [],
            "aaData": obj,
            "aoColumns": [
            { "mDataProp": "Name" },
            { "mDataProp": "Id" },
            { "mDataProp": "Value" }
        ]
        });
    }
    );

To update my JSON object:

function updateObject() {
    var response = $.ajax({
        type: "GET",
        datatype: "json",
        url: myURL,
        success: function (result) {
            obj = jQuery.parseJSON(result);
            //table.fnDraw(); - tried this but it doesn't work
        }
    });
}

JSON data which I get from the server:

 [{"Name": "name1", "Id": "id1", "Value":"value1"}, 
     {"Name": "name2", "Id": "id2", "Value":"value2"},...]

Is it possible to refresh table content after I get new data from the server?

UPDATE:

table.fnClearTable();
table.fnAddData(obj, false);
table.fnStandingRedraw();

After inserting new rows, current page gets lost and current scroll position also. fnStandingRedraw method helps to keep page position. But scroll jumps to the first row. Is there any way to calculate current scroll position to make a jump back after updating (http://datatables.net/docs/Scroller/1.0.1/) or some other solution?

1
  • did you find a solution to this issue ? Commented Nov 21, 2014 at 23:59

2 Answers 2

1

I think this can help you. You should first remove all the data and add the new data again, document is here

table.fnClearTable().fnAddData(obj);
Sign up to request clarification or add additional context in comments.

3 Comments

I also use fnStandingRedraw() plugin to keep my paging position after update. But I can't keep scroll position. Maybe you know how to handle this problem ?
Not really, let me see what the problem is?
You shouldn't have to remove all the data and insert new data. This isn't a good solution for someone who has large data sets and is trying to append data on scroll.
0

I had a similar scenario. So eventually I solved this way:

function UpdateDatatable(data, $table) {
    $.ajax({
        type: "GET",
        dataType: "JSON",
        url: "../biz/datahandler.ashx",
        data: ({data: data}),
        async: true,
        error: function (xhr, ajaxOptions, thrownError) {
            /* manage the error */
        },
        success: function (dataget) {
            /* maange the success */
            $table.dataTable({
                ...
            })
    });
}

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.