I. Overview
1.1 Requirement
Usually, we use setCellValue("cell", null, value) to set a cell value and this method applies only to fixed cells.
To set the value of an expanded cell, e.g., the vertically expanded cell A1: setCellValue("A1", null, value), you will find that only the cell A1 has a value, but the expanded cells do not, so how to set the value of an expanded cell?
Enter a number in the edit cell in each row, then the controlled cells will display 1 or 0 according to whether or not the number is greater than 10, as follows:
1.2 Solution
In the data entry, expanded cells can be divided into two categories by source: one is cells expanded directly from data columns and the other is cells expanded by inserting rows. The way of setting the value varies with the mode of expansion, which will be detailed below.
II. Sample
2.1 Directly expanded cells
In an edit end event of a widget, dynamically pass the row number and column number to a JS function as parameters, use contentPane.setCellValue(col, row, value); to set a cell value.
Both col and row begin from 0.
Here, if the value of the edit cell is greater than 10, the controlled cell has a value of 1; otherwise, it has a value of 0.
1) The template design interface corresponding to the figure above is as follows:
2) Add an Edit End event to the widget of the textbox B2, input the row number row and the column number col, with the value of =row()-1 and =col() respectively:
Input the JS code:
if(this.getValue()>10){
contentPane.setCellValue(col,row,1);
}else{
contentPane.setCellValue(col,row,0);
}
2.2 Cells expanded by inserting rows
Add a Load End event in the Web Attributes of data entry, get the row number and column number of the cell being edited by using an event listener of FineReport, save them in a global variable and then get the row and column numbers from the Edit End event of the cell to set the cell value. See Get the Row Number of Cell Currently being Edited for how to get the row number and column number of a cell using an event listener of FineReport.
1) Create a new template and add two text widgets in cells A2 and B2:
2) Click Template>Web Attributes>Data Entry Settings to add a Load End event and the JS code is as follows:
Note
a global variable must be preceded by Window.
Input the JS code:
contentPane.on("cellselect", function(td) {
var row =contentPane.curLGP.getTDRow(td);
var col =contentPane.curLGP.getTDCol(td);
window.row=row;
window.col=col;
});
3) Add an Edit End event to the widget of the cell A2 and the JS code is as follows:
Note
The row and column numbers obtained from the event listener begin from 1 and their values begin from 0.
var value=this.getValue();
contentPane.setCellValue(window.col,window.row-1,value);
2.3 Preview
Save the template and select Data Entry Preview. The preview effect on a PC is shown as below:
1) Directly expanded cells
2) Cells expanded by inserting rows