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?