Dynamically loading table rows with json data

Dynamically loading table rows with json data

Bill_VA2Bill_VA2 Posts: 4Questions: 2Answers: 0

I'm loading my table dynamically with a returned json file via API call. The json is just a bunch of delimited values without keys

[["this info","2017\/04\/03","$259.47","$259.47","$260.42","$2,315.50","$2,315.50","83.30","24.50","58.80","67.30","-8.50","16.00","-16.00","2017\/03\/21","0.36","0.80","$2.07","$0.00","$0.00","1.68","$4.36","$164.88","$63.54","0.55","$1.42","$125.04","$48.19"],["this is a test","2017\/04\/03","$288.79","$288.79","$289.47","$2,515.00","$2,515.00","88.67","37.00","51.67","56.67","-5.00","32.00","-16.00","2017\/03\/21","0.24","0.70","$2.03","$0.00","$0.00","0.92","$2.67","$183.51","$63.54","0.00","$0.00","$0.00","$0.00"]]

I'm looping through the returned json values like this, but there must be a better way, in case the number of columns change, currently 28.

t.clear().draw();
        for(var i = 0; i < s.length; i++) { 
        t.row.add( [
            s[i][0], 
            s[i][1], 
            s[i][2], 
            s[i][3], 
            s[i][4], 
            s[i][5], 
            s[i][6], 
            s[i][7], 
            s[i][8], 
            s[i][9], 
            s[i][10], 
            s[i][11], 
            s[i][12], 
            s[i][13], 
            s[i][14], 
            s[i][15], 
            s[i][16], 
            s[i][17], 
            s[i][18], 
            s[i][19], 
            s[i][20], 
            s[i][21], 
            s[i][22], 
            s[i][23], 
            s[i][24], 
            s[i][25], 
            s[i][26], 
            s[i][27], 
            s[i][28] 
            ]).node().id = 'nt_row_id'+i;  
        } // End For  
        t.draw( false );

Answers

  • kthorngrenkthorngren Posts: 20,302Questions: 26Answers: 4,769

    Have you tried t.row.add( [s[i] ).node..... I think this should add the array without having to specify each one.

    Kevin

  • allanallan Posts: 61,722Questions: 1Answers: 10,108 Site admin

    Or even better, drop the for loop and use rows.add():

    t.clear().rows.add( s ).draw();
    

    And use createdRow to assign the generated id (although I'm not sure what a client-side generated id would be used for?).

    Allan

This discussion has been closed.