Change the data set in javascript and update data table with new values.

Change the data set in javascript and update data table with new values.

ketanmhatreketanmhatre Posts: 4Questions: 0Answers: 0
edited October 2012 in DataTables 1.9
Hi,
I am drawing one data table with json data. I have two such data sets.
Initially I will be drawing table with first data set. Now clicking on link I need to populate table with another data set.
Not sure how do I do it. Can you pls help.

var aDataSet = [
["Act one ", "700", "1000","300"],
["Act two", "700", "1000","300"],
["Act three", "700", "1000","300"],
["Acct four", "700", "1000","300"]
];

var aDataSetNew = [
["Act one new ", "700", "1000","300"],
["Act two new", "700", "1000","300"],
["Act three new", "700", "1000","300"],
["Acct four new", "700", "1000","300"]
];


$('#projDataTable').dataTable( {
"aaData": aDataSet,
"sScrollY": "200px",
"sDom": 't',
"bScrollInfinite": true,
"bDeferRender": true,
"iDisplayLength": -1,
"bSort": false,
"aoColumns" : [
{ },
{ sClass: "alignRight" },
{ sClass: "alignRight" },
{ sClass: "alignRight" }
]
} );


LOAD NEW DATA



Account Name1
Value one
value two
Net total

Replies

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    Use the fnClearTable and fnAddData API methods to first clear the old data and then add the new data.

    Allan
  • ketanmhatreketanmhatre Posts: 4Questions: 0Answers: 0
    As if now I am doing it by clearing the table by
    oTable.fnClearTable();

    and the use fnAddData in a loop to add rows from another data set.
    Is there any better way to do it.
  • ketanmhatreketanmhatre Posts: 4Questions: 0Answers: 0
    The only reason I am asking is I have to loop through the data like below
    $.each(aDataSet1, function(i, item) {
    $('#projDataTable').dataTable().fnAddData(
    aDataSet1[i]
    );
    });

    Is this the correct way to do it. Or is there any other better way just to change dataset and update table.
    And thanks for so quick reply Allan.
  • avioliavioli Posts: 4Questions: 0Answers: 0
    edited October 2012
    You can add 2D array of arrays - add multiple rows in a single call. Just make sure to prepare your 2D array properly, but by the looks of it you should be fine. No need for $.each()
  • ketanmhatreketanmhatre Posts: 4Questions: 0Answers: 0
    Thanks Avioli...I will try your solution. Sounds like good Idea to me. ;)
  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    What avioli says :-)
This discussion has been closed.