Adding rows from json but with dynamic columns

Adding rows from json but with dynamic columns

Keith_HKeith_H Posts: 54Questions: 19Answers: 1
edited April 2018 in Free community support

I have a table which will have varying columns based on the data (it's the number of periods the task is valid for).

So I am dynamically creating the table and that's all fine.

I want to use the add.rows.

If i do this, it works fine (note: this was to prove the add.rows was OK).:

            for (var i = 0; i < data.length; i++) {
                $('#tblTaskWbsBudgetSumm').DataTable().row.add([
                     trim(data[i].WSWBS)
                    ,trim(data[i].WSWBSDESC)
                    ,fncFormatNumber(data[i].WSTOTALHOURS,0,"Y","Y")
                    ,fncFormatNumber(data[i].WSTOTALCOSTS,0,"Y","Y")
                    ,null
                    ,null
                ]);

What I want to do is (loop through the json data and add all the relevant columns):

            for (var i = 0; i < data.length; i++) {
                locRow=[];
                locRow[0]   =   '<a id="aTaskWbsBudgetEdit'+trim(data[i].WSWBS)+'" href="#" >'+trim(data[i].WSWBS)+'</a>';
                locRow[1]   =   trim(data[i].WSWBSDESC);
                locRow[2]   =   fncFormatNumber(data[i].WSTOTALHOURS,0,"Y","Y");
                locRow[3] = fncFormatNumber(data[i].WSTOTALCOSTS,0,"Y","Y");
                for (var j = 0; j < glbTaskPeriodArray.length; j++) {
                    var locPd = glbTaskPeriodArray[j];
                    locRow[4+(j*2)] =   fncFormatNumber(data[i][locPd+'_HOURS'],0,"Y","Y");
                    locRow[5+(j*2)] =   fncFormatNumber(data[i][locPd+'_COSTS'],0,"Y","Y");
                }

                $('#tblTaskWbsBudgetSumm').DataTable().row.add([locRow]);

This results in the data all going into the first cell in each row. I assumed that by creating an array it would work - I'm obviously missing something simple here.

Answers

  • Keith_HKeith_H Posts: 54Questions: 19Answers: 1

    The issues was with the $('#tblTaskWbsBudgetSumm').DataTable().row.add([locRow]); line.

    Needs to be $('#tblTaskWbsBudgetSumm').DataTable().row.add(locRow);.

    :#

This discussion has been closed.