Start Building Professional
Web Apps Today


 
Categories Question details Back To List
Question  posted by Andrea Benini on Nov 20, 2007 02:21
open dhtmlx forum
Cycle through cells with Javascript

Hello Support,
i've a grid where user can insert, update, delete rows . After his operation i'd like to post grid data to a server with AJAX, i only need a simple code to cycle through each cell of the grid.
This is not so difficult but please let me explain how to solve this issue when deleting, inserting rows, let's take for example a loaded grid from xml data with these information:
column1 column2
id1 row1cell1 row1cell2
id2 row2cell1 row2cell2
id3 row3cell1 row3cell2
id4 row4cell1 row4cell2

if I delete (ID2) i cannot do something like:
for (row=0; row<mygrid.getRowsNum(); row++) {
alert(mygrid.cells(""+(row+1), 0).getValue());
}
because when ID2 is not available it still tries to access it

and even when you follow these steps you run into troubles:
- add a row (new ID="id5")
- delete a row (delete row 1, ID="id1")
- add another row (new ID="id5")
if you calculate new IDs on getRowsNum you're not sure about unique keys on grid

How can i safely cycle in a table ?

Kind Regards
Answer posted on Nov 20, 2007 05:23
The construction
    for (row=0; row<mygrid.getRowsNum(); row++) {
    alert(mygrid.cells(""+(row+1), 0).getValue());
is incorrect, you are mixing two different row parameters ID and Index

Iteration by row index will look as

for (var i=0; i < grid.getRowsNum(); i++) {
    alert(mygrid.cells2(i, 0).getValue());
};

Iteration by row ID will look as

grid.forEachRow(function(id) {
    alert(mygrid.cells(id, 0).getValue());
});
Answer posted on Nov 23, 2007 03:00
Thanks, it worked

Ben