← servoy magazine

[Tip] Looping through rows

  • Tips

by Marcel Trapman
www.it2be.com

Everybody needs to, from time to time, loop through a set of rows in order to search for a particular value or a row. This is very easy but can have, when not handled the right way, great impact on the performance of your solution.

Quite often the looping is done via the following method:

Var size = controller.getMaxRowIndex();

For ( var i = 1 ; i <= size ; i++ ) {
Controller.setRowIndex(i);

//do your thing
}

Using this creates at least one but likely 2 performance issues:

1. When the form is shown the screen needs to be repainted every single step because you ‘fysically walk’ through rows on the screen...
2. If you attach a method to the recordSelection event, stepping through the rows will cause the method to be fired every step...

So, knowing that, you will ALL, of that I am sure, use the below method from now on:

Var set = foundset;
Var size = set.getSize();

For ( var i = 1 ; i <= size ; i++ ) {
Var row = set.selectRecord(i);

//do your thing
}

Notice that not only did I copy the foundset to a variable called ‘set’ but I also copied the contents of the row to the variable ‘row’. This way you do everything on the foundset without touching the controller. Please make sure, when referencing to a column you now reference to row.column instead!! Otherwise your result won’t be what you expected it to be ☺

Hope this helps a little in solving potential (or actual) performance issues,

Marcel

Comments (4)

Harjo Kompagnie
Just a little adjustment: For ( var i = 1 ; i <= foundset.getSize() ; i++ ) { var row = foundset.selectRecord(i); //do your thing } or else the method will stop after 200 records!
Harjo Kompagnie
btw: this is allready tip here: http://www.kabootit.com/home/2005/02/tip_looping_thr.html. From myself. haha :-)
Hameed
var row = foundset.selectRecord(i); OR var row = foundset.getRecord(i); ?? Thanks
Marcel Trapman
??