Home
Analytics
Iterate Rows in a Report's Dataset in Javascript
dcoombs
I've been searching for javascript code I can incorporate into my report to iterate the rows and then the columns of a dataset in the onRender method of my report detail. I have found code for the columns but no code that allows me to fetch the next row until a certain condition occurs.
Basically I need to:
-run a report contains a dataset/recordset against an SQL query consisting of a left join on main data to optional child records
for each PrimaryKey from the rows in the joined set
- read a row and add the new data from each read to a master store of global variables
- After reading all rows (1..n) for the PK send the custom-formatted global variables to the detail section.
More Detail:
My dataset has one or more rows from various joins for each primary key (PK) row in a parent table. The additional child data is 0..n and is unique subscript numbers that belong to one or more columns from the parent. Due to the nature of joins, in this case a left join on the parent table, the data for the parent table repeats when there are two or more rows of joined child (subscript data). The subscript data varies for each row and can be null if there are no subscripts for the parent row. I need to read through the dataset and as appropriate format and concatenate row values together in global variables to create the subscripts. Once I have processed all rows for the current PK I need to send the formatted data to the detail section.
Any help or pointers to links that show javascript code that can iterate rows of a dataset defined in a report is greatly appreciated.
Thanks,
Doug
Find more posts tagged with
Comments
mwilliams
Hi Doug,
I'm not sure if I'm totally understanding the issue or not, and you've probably already found a solution, but I'll respond just in case you haven't or someone else has the same issue. If you set your PGV's in the onFetch of the dataSet, it will iterate through every row of the dataSet as the dataSet is filled. You should be able to do everything you need here, I would think.
wrobin1
I'm having a variation of this same issue and I'm ready to rip my hair out, so I could use some suggestions, if you have any.
In our report, I have a text box in which I have embedded some HTML and Javascript. (It's a Google Maps implementation). I have bound a data set to that text box, and inside the Javascript I can see the values of the first row of that data set (using '<VALUE-OF>row["platform"]</VALUE-OF>' etc), but I can't seem to figure out how to iterate through the data set to get the rest of the rows.
I have tried adding a persistent global variable to the report, which I then fill in the onFetch() of the data set, but I can't seem to access the "global" variable inside the Javascript, either directly or though use of getPersistentGlobalVariable().
I have tried adding a non-visible table to the report and attaching the same data set to that, and using document.getElementById() I seem to be able to find that table but can't seem to get data out of it.
What I'm trying to get out of this is a list of names, latitudes, and longitudes so that I can add a list of pushpins to the map. I'm probably missing something pretty basic here, but any help would be greatly appreciated!
Robin
mwilliams
Robin,
In your onFetch(), you should be able to create an array of your values and store it in a persistentGlobalVariable. You would just need to have the dataSet attached to a report item at the top of your report or at least before you're wanting to use this array so that you can be sure that it gets run. You should be able to use the <value-of> tag to grab the persistentGlobalVariable() with reportContext.getPersistentGlobalVariable("variableName");. Are you not able to do this from within the value-of tag.
wrobin1
Hi Michael, thanks for the response.
Ah-ha, you mean literally something like '<VALUE-OF>reportContext.getPersistentGlobalVariable("variableName")</VALUE-OF>'. The one thing I haven't tried
I set a test global variable to a nonsense string in the initialize() of the report, and I can indeed grab it from inside the text box as long as I enclose it in <VALUE-OF> tags, so I'll put the onFetch() stuff back into the data set and (hopefully) run from there.
Thank you!
mwilliams
You are very welcome. Let me know how it works out for you!
wrobin1
Ok, I finally had time to get back to this, and I'm having some unexpected results.<br />
<br />
In the initialize() of the report, I have this:<br />
<br />
var badguys = [];<br />
reportContext.setPersistentGlobalVariable("badguys", badguys);<br />
<br />
Then in the onFetch() of the associated data set, I have this:<br />
<br />
var tmpArray = reportContext.getPersistentGlobalVariable("badguys");<br />
tmpArray[tmpArray.length] = row.time + ":" + row.platform + ":" + <br />
row.tail + ":" + row.latitude + ":" + row.longitude;<br />
reportContext.setPersistentGlobalVariable("badguys", tmpArray);<br />
<br />
(Basically just jamming the values together there separated by colons so they can be broken out later) Inside the Javascript inside the text box where I want to pull out the values and use them, I have this:<br />
<br />
function addAllToMap() {<br />
var badGuyList = '<VALUE-OF>reportContext.getPersistentGlobalVariable("badguys")</VALUE-OF>';<br />
var len = badGuyList.length;<br />
var i = 0;<br />
for(i;i<len;i++) {<br />
var str = [];<br />
str = badGuyList
.split(":", 5);<br />
alert(str[0] + "-" + str[1]);<br />
}<br />
}<br />
<br />
I had an alert show the length of the badGuyList, and it was the expected number, but rather than getting an array of strings like I expected to, here's what I see in the HTML:<br />
<br />
var badGuyList = '[Ljava.lang.Object;
@1f88f0f'
;;<br />
<br />
I tried taking the single quotes off the <VALUE-OF> line, thinking that maybe that was forcing a toString() on the results or something weird, but that didn't do anything (except break it worse!)<br />
<br />
What am I missing?<br />
<br />
Robin
wrobin1
Another update...
I changed the global variable declaration to this:
var badGuys = new java.util.ArrayList();
The onFetch() where the values get added to this:
tmpArray.add(row.time + ":" + row.platform + ":" + row.tail + ":" + row.latitude
+ ":" + row.longitude);
But now, at least, in the HTML source when I run the report, I see this:
var badGuyList = '[1961.38378906:F-35C:2:25.9732132:119.62733459, 2751.00488281:F-16:62:25.13703918:120.09384155, 2995.1081543:F-16C:1:28.50241852:121.41327667, ... ];'
So now my issue is just that I'm not splitting it out correctly. Getting closer!
wrobin1
Final answer, in case anyone else runs into this...
in the onFetch(), when I crammed the column values together into a colon-delimited string, I had to encompass the whole thing in double quotes, like this:
tmpArray.add('"' + row.time + ":" + row.platform + ":" + row.tail + ":" + row.latitude
+ ":" + row.longitude + '"');
Once I did that, when I pull the global variable out, Javascript treats it as an array of strings, which is what it should be.
MadhuriKankurte
<p>Hi wrobin1 & Williams</p>
<p> </p>
<p>Thanks a lot . With above instructions I'm able to successfully implement dynamically forming a dropdown in html.</p>
<p> </p>
<p>Have used , onFetch and initialize events and same set/<span style="color:rgb(40,40,40);font-family:'Source Sans Pro', sans-serif;">getPersistentGlobalVariable !</span></p>
<p>The output ( in my case , using the dataset and forming a dropdown box) is perfect too .</p>
<p> </p>
<p>However , when i try to execute the same in Webviewer ..I get the following error .</p>
<p> </p>
<div><span style="font-size:10px;"><em>ReportDesign (id = 1):</em></span></div>
<div><span style="font-size:10px;"><em><span>+ </span>There are errors evaluating script "//var tempArray=reportContext.getPersistentGlobalVariable('dataSetCurrencyArray');<br>
//tempArray.add("\""+row.CCY+"\""); <br>
//reportContext.setPersistantGlobalVariable("dataSetCurrencyArray",tempArray);<br><br>
//var tempArray=reportContext.getPersistentGlobalVariable('dataSetCurrencyArray');<br>
reportContext.getPersistentGlobalVariable('dataSetCurrencyArray').add(row.CCY); <br>
reportContext.setPersistantGlobalVariable("dataSetCurrencyArray",reportContext.getPersistentGlobalVariable('dataSetCurrencyArray'));<br><br>
":<br>
Fail to execute script in function __bm_onFetch(). Source:<br>
<br>
" + //var tempArray=reportContext.getPersistentGlobalVariable('dataSetCurrencyArray');<br>
//tempArray.add("\""+row.CCY+"\""); <br>
//reportContext.setPersistantGlobalVariable("dataSetCurrencyArray",tempArray);<br><br>
//var tempArray=reportContext.getPersistentGlobalVariable('dataSetCurrencyArray');<br>
reportContext.getPersistentGlobalVariable('dataSetCurrencyArray').add(row.CCY); <br>
reportContext.setPersistantGlobalVariable("dataSetCurrencyArray",reportContext.getPersistentGlobalVariable('dataSetCurrencyArray'));<br><br>
+ "<br>
--<span style="font-size:12px;"><strong><span style="color:#ff0000;">---<br>
A BIRT exception occurred. See next exception for more information.<br>
TypeError: Cannot find function setPersistantGlobalVariable in object org.eclipse.birt.report.engine.script.internal.ReportContextImpl@e7dc5b. (/report/data-sets/oda-data-set[
@id="
;70"]/method[
@name="
;onFetch"]#7). </span></strong></span></em></span></div>
<p><span style="font-size:12px;"><strong><span style="color:#ff0000;"> </span></strong></span></p>
<p> </p>
<p>Please suggest a solution here </p>