I need to store table cell content into array using tr and td indexes like this:
myArray[tr_idx][td_idx]='value';
It is not required to store data of all of cells in array but only several with a special content, but if i use indexes as a keys of the array i’ll have many empty array elements.
e.g.
myArray[2][3]='data1';
myArray[4][3]='data2';
alert (myArray.toSource()) -> [, , [, , , "data1"], , [, , , "data2"]]
maybe there is another suitable way of storing such type of data?
Why not just store them as individual 2-item arrays
[row,col]? Since the table cells are already accessible viatableElement.rows[].cells[], you can use the 2 indices to access them from the table.Access with:
Or even cleaner, if you prefer to use objects rather than arrays:
Accessed with:
Finally, if you don’t actually need the row/column indexes, but rather the DOM nodes themselves, just push those onto your array.
They are then trivially modified:
Update after comment:
If you need to be able to attach the cell coordinates to a new value from the backend, a good solution would be to expand the object
{}example above to include avalueattribute. The server can pass back the new value in JSON.Your backend should send a JSON response back to the script containing the same type of array of objects, where
value:has been populated with the new cell value. You can then pass it back into the table with: