Discussions
Categories
Groups
Community Home
Categories
INTERNAL ENABLEMENT
POPULAR
PUBLIC CLOUD
PRIVATE CLOUD
Quick Links
MY LINKS
HELPFUL TIPS
Back to website
Home
Intelligence (Analytics)
Iterate over table columns in script
matthjes
Hi,
I'd like to hide rows in my table which are entirely empty. As there seems to be no standard solution for this (like an option in the property editor) I thought of doing this via a script. So what I want to do is:
For each row:
1. Iterate over all columns of a table
2. Test if current cell is empty
3. If all cells are empty, hide the row
Is there a better way to do this? I already tried adding code to the onCreate method of a table row (get the row data via this.getRowData(), then the column count and column values via this.getRowData().getColumnCount(), however, this only let me iterate over all bindings and not over table columns) but this did not work.
EDIT: Btw, I expected that when I add this.getRowData().getColumnValue(this.getColumn()) to the onCreate method of a cell I would retrieve the current value of this cell...but this does not seem to work. How do I get the value of a specific table cell using indices?
Find more posts tagged with
Comments
kclark
I was able to hide an empty row in a table from the onCreate() using the following script<br />
<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>var columnCount = this.getRowData().getColumnCount()
isNull = false;
for(i=1;i<columnCount;i++) {
var temp = this.getRowData().getColumnValue(i)
if(this.getRowData().getColumnValue(i) == "") {
isNull = true;
}else{
isNull = false;
i = columnCount+1;
}
}
if(isNull == true) {
this.getStyle().display = "none"
}</pre>
<br />
Let me know if this works for you
matthjes
Hi,<br />
<br />
Thanks for your help
I already tried this but getColumnCount() seems to provide the number of bindings for the table but not the number of columns (for example, I have seven columns in my table, but getColumnCount() gives 49 columns, the number of bindings in the property editor -> binding tab). Similarly, getColumnValue(i) seems to give the value of the i-th binding, not the i-th column.<br />
<br />
As an example: if I have the following table bindings:<br />
Name, ****, Height, Profession, Date of Birth, Age, Address, Phone<br />
and the following table:<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
| Name | Age | Height | Profession |
| John | 25 | 1.78m | Tailor |
| Tom | | | |
| Paul | 34 | 1.60m | Fisher |
</pre>
then the row with Tom should be hidden as there are no value entries for him in the table. So I expected that getColumnValue(1) gives the name (Tom), getColumnValue(2) the Age (null or "") and so on and I just have to test if columns 2, 3, and 4 are empty. But getColumnValue(1) could give, for example, the ****, value 2 the Profession, value 3 the date of birth and so on. So it iterates through the list of bindings, not the list of table columns.<br />
<br />
Maybe I got something wrong here...<br />
<br />
EDIT: Just tested it with an example report using the classic models database (unfortunately I cannot upload files to anything from work). When I add, e.g., aggregations to a table at different group levels then these are counted as well when I call getColumnCount() in the onCreate() method of a row. When I iterate over all columns via getRowData().getColumnValue(i) then the order of values returned for increasing indices corresponds to the order in which the table bindings have been defined (so, e.g., added aggregations have the highest indices).
kclark
I had placed that script in the onCreate() of the detail row. I have an example but I made it on another PC last night so I'll need to recreate it and upload that one. I created a scripted data set with some test data that looks like this.<br />
<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
Column 1 | Column 2 | Column 3
test 1 test 1 test 1
test 2 test 2 test 2
test 4 test 4 test 4
test 5 test 5 test 5</pre>
<br />
Using the script from either I was able to hide that 3rd row.
matthjes
Yes, this should work with a simple table without, e.g., any aggregations. The number and order of columns in the table is equal to the number and order of column bindings, so getColumnValue(1) returns the data for the first column in the visual table and the first defined column binding.<br />
<br />
Could you please add, e.g., a grouping and an aggregation on, maybe, column 3? So that the table looks something like this:<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
| Column 1 | Column 2 | Column 3 |
| GroupCol1 | | AggCol3 |
| | DetailCol2 | DetailCol3 |
| | DetailCol2 | DetailCol3 |
| GroupCol2 | | AggCol3 |
| | DetailCol2 | DetailCol3 |
</pre>
Now the Aggregation is a new column binding and getColumnCount() will return 4 and getColumnValue(4) will return the respective value for AggCol3.<br />
<br />
The problem I have is that the index used in getColumnValue has nothing to do with the visual table but with the column binding. You can see this if you rearrange your table from:<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
Column 1 | Column 2 | Column 3
test1Row1 test2Row1 test3Row1
test1Row2 test2Row2 test3Row2
test1Row3 test2Row3 test3Row3
</pre>
to<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
Column 2 | Column 1 | Column 3
test2Row1 test1Row1 test3Row1
test2Row2 test1Row2 test3Row2
test2Row3 test1Row3 test3Row3
</pre>
(just drag the data elements around, but do not change the binding). Now for the first row getColumnValue(1) will still return the value "test1Row1", although I would expect it to return "test2Row1".<br />
<br />
EDIT: Interestingly, calling getColumn() in the onCreate() method of a cell returns the correct column integer of the cell (starting with zero). However, this information cannot be used to get the value from the row data as the index returned by getColumn() is specific to the visual table but the index expected by getColumnValue() is specific to the column binding. It's a strange API, at least the method names are very confusing...