Home
Analytics
How to get the row number in a cell script
germobirt
Hi,
I'm using Birt 2.6.2 with Javascript.
I've a fixed grid with e.g. 40 rows & 3 cols.
In every onCreate()/onPrepare() script of a cell I want to know in which row the script is running.
How can I get the row number in a grid, within a cells onCreate()/onPrepare() script.
regards
germobirt
Find more posts tagged with
Comments
kclark
Since the grid is fixed you could create a variable in each cell so you know what row it's in.
bricombs
Good Day
One easy way would be to create a global variable and then increment it in the first cell of each row.
I have attached small sample that does this.
germobirt
Hi,
thanks for the file but with 2.6.2 I can't open a 3.7.2 report.
But I know what you mean.
As I said 3 cols and 40 rows. That results in 120 Cells=Labels=Scripts! :wacko:
That's why I hoped to access all labels from the grids onCreate() to set the labels style and text.
I tried to avoid to edit 120 scripts.
But it is as it is. Nevertheless, now I try use at least generic script code.
Means not to set in each of the scripts individual vars just to see which cell script is running.
Now I have to set the cell sytle, means another 40 onCreate scripts.
If I could get the current row, I would have a kind of index to access some global array vars.
Hope dies last, endless coding follows? Please not.
regards
bricombs
Good Day
Ok this is different than just getting the row number that you are on in a grid. It sounds like what you want to do is set the text value and style for each label that is in the grid. And that you will have a label in each cell. If so one way to do this is to use a naming convention so you know where each label is in the grid. For example:
r1c1 r1c2 r1c3
r2c1 r2c2 r2c3
You can then create a loop in the Grid onPrepare that sets the value of each label. example code that does this for a 3x3 grid.
var rowcount = 1;
var columncount = 1;
do
{
columncount = 1; //reset the column count to one as we are on a new row.
do
{
var s = "r" + rowcount + "c" + columncount;
lbl =reportContext.getDesignHandle().findElement(s);
lbl.setText(s + "this works");
lblStyle = lbl.getPrivateStyle();
var bgColor = lblStyle.getBackgroundColor();
bgColor.setRGB(0xA9A9A9 ); //set the background color
columncount +=1
} while (columncount < 4);
rowcount +=1
} while (rowcount < 4);
germobirt
Hi ,
in fact I did already started with that approach using smart label names as coordinates.
But for cell & row scripts that doesn't work because cells and rows can't have a name!
Any idea here?
Because I need to set border styles in the onCreate() of cells or better in onCreate() of the rows for all cells in that row.
regards
germobirt
bricombs
Good Day
Here is some sample code that you can put in your Grid's onPrepare. It will create a label to place in each cell and then set the background color for that cell.
var rowcount = 0;
var columncount = 0;
var mygrid = reportContext.getDesignHandle().findElement("TestGrid");
//var factory = design.getElementFactory();
var factory = reportContext.getDesignHandle().getElementFactory();
do
{
columncount = 0; //reset the column count to one as we are on a new row.
var row = mygrid.getRows().get(rowcount );
do
{
var s = "r" + rowcount + "c" + columncount;
var lbl = factory.newLabel(s);
lbl.setText(s + " Place your Data here");
var cell = row.getCells().get(columncount);
//add the label
cell.getContent().add(lbl);
// set the background color of each cell.
var cellStyle = cell.getPrivateStyle();
var bgColor = cellStyle.getBackgroundColor();
bgColor.setRGB(0xA9A9A9 );
//add the label
//cell.getContent().add(lbl);
columncount += 1;
} while (columncount < 3);
rowcount += 1;
} while (rowcount < 3);
germobirt
Hi,<br />
<br />
and thanks for the code.<br />
<br />
But the origin problem is, that I've the data for the styling decisions <span class='bbc_underline'>not before</span> when <strong class='bbc'>onCreate()</strong> is fired!<br />
<br />
Because the script order is:<br />
<br />
<ul class='bbcol decimal'><li>onPrepare()</li><li>onFetch()</li><li>onCreate()</li></ul>
<br />
<br />
From <span class='bbc_underline'>onCreate() of the grid</span>, <strong class='bbc'>I can't change data or styling</strong> anymore of embedded labels in the cells. <br />
I can change data or styling only in <span class='bbc_underline'>onCreate() of the labels</span>.<br />
<br />
<br />
So:<br />
<br />
In onCreate() of a <strong class='bbc'>row </strong>I need to know which <span class='bbc_underline'>row </span>am I, to check the data and set the styling of the row.<br />
<br />
In onCreate() of a <strong class='bbc'>cell </strong>I need to know which <span class='bbc_underline'>row </span>am I, to check the data and set the styling of the cell.<br />
<br />
In onCreate() of a <strong class='bbc'>label</strong> I need to know which <span class='bbc_underline'>row </span>am I, to check the data and set the styling of the label. <br />
> This is solved by smart label names and a lot of code!
<br />
<br />
<br />
I still have the hope to do this without setting the row number in each onCreate().<br />
<br />
Question remains:<br />
Can a cell or row onCreate() script determines their row number in a grid?<br />
<br />
regards <br />
germobirt
Hans_vd
Hi Germobirt,
Not sure if I entirely understand what it is that you want, but how about this:
instead of a grid with labels, you build a scripted dataset with 3 columns and 40 rows and bind it to a table. You will then only need to write four scripts to set the styling: 3 for each column, 1 for the row.
You can also add an extra column in the scripted data set to hold the rownumber.
You will not have a grid in the report, but a table, but if the result looks the same, I don't see how that could be a problem.
Regards
Hans