So I'm building dynamic tables that will be populated from my DB, but everything about these tables need to be dynamically assigned. So coming into it the function which creates the table will not know how many cols there are, or what the data type is in each col, but I've got that all worked out.
Now populating the data I'm using objects.
When creating the rows I call populate the object with the name of the row as the property name. This is important to stay the same because the tables will be using these property names to associate to which column the data belongs.
row.id = dataObj[i].id;
row.timeType = dataObj[i].timeType;
row.existing = dataObj[i].existing;
row.upgrade = dataObj[i].upgrade;
row.childNode = dataObj[i].childNode;
row.budget = dataObj[i].budget;
So 'row' is my object, and as you can see I'm using 'i' as an iterator in the loop to populate all the data before passing it to the table.
Here's the key... Because this table function should be able to accept any dataset, the program will never know what the property names are. I do have an array already stored with all the property names retrieved from the DB, but JS won't let me do something like this:
rows = [];
nameOfCols = ['id','timeType','existing','upgrade','childNode','budget']; //Populated from DB
for (i=0;i<numRows;i++) { //Iterate over each row of data returned
for (j=0;j<numCols;j++) { //Iterate over each Col for curr data row
row.nameOfCols[j] = dataObj[i].nameOfCols[j];
}
rows.push(row);
}
Using a variable property name to assign data to an object. How do you get around this?
The row.nameOfCols[j], because the property name can't be a variable.
And so as you can see in the end 'rows' is an array of objects.
Any help appreciated!