Page 1 of 1

### Variable Grid Code

Posted: Thu Sep 29, 2011 11:57 pm
Now that i have the basics of my button Grid Macro worked out, I need to take it the next step and create the number of cells / buttons based on the count of a list.

I am unsure how I go about coding the loop to handle the variance of the number of buttons.

For example...

If the list count = 32 I would end up with a 6x6 grid. (evenly distributed)
If the list count = 25 i would end up with a 5x5 grid.
If the list count = 24 I would end up with a 5 X 5 grid with the last row only having 4.

Any pointers how I achive this type of dynamic grid building?

### Re: Variable Grid Code

Posted: Fri Sep 30, 2011 3:08 am

Code: Select all

``[r: gridSize = 1+floor(squareroot(listCount))]``

### Re: Variable Grid Code

Posted: Fri Sep 30, 2011 7:25 am
CoveredInFish wrote:

Code: Select all

``[r: gridSize = 1+floor(squareroot(listCount))]``

Also, you may wish to know how many cells are in the final row, which you can do the following the calculate. Depends on how you're implementing the table, but for me I'd need to know this number.

Code: Select all

``[r: lastRowCellsNumber = noOfCells - ((gridSize - 1) * gridSize)]``

### Re: Variable Grid Code

Posted: Fri Sep 30, 2011 10:00 am
So using the following:

Code: Select all

``[r: TTCount = listCount(ttList)]<br>[r: gridSize = 1+floor(squareroot(TTCount))][r,count(gridSize,""),code:{  <tr>    [r, for(i,1,gridSize,1,""), code:{       [h: tokenName = "image:Treasure_Token_1"]      [r: tv = listGet(ttList, num)]      [r: button = strformat('<td><input type="image" src="%s" name="%s" value= "%s"></td>',getImage(tokenName),"btnChoice" + num, tv)]      [h: num = num +1]       } ]  </tr>}]``

The list size at the start is 32

Im only getting 30 buttons. 5(cols) x 6(rows) grid.

I am trying to get 32 buttons. 5 (cols) x 7(rows)-Last row would only have 2 buttons.

### Re: Variable Grid Code

Posted: Fri Sep 30, 2011 12:21 pm
You have to start the for-loop at 0 or walk to gridsize+1

### Re: Variable Grid Code

Posted: Fri Sep 30, 2011 2:36 pm
CoveredInFish wrote:You have to start the for-loop at 0 or walk to gridsize+1

You often encounter this when for looping through arrays or objects, because the first index starts at 0, and the final entry is (length - 1).