Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Archived » BIRT » Populating Arraylist from Dataset(Populating Arraylist from Dataset to be available from Text Item)
Populating Arraylist from Dataset [message #1062070] Wed, 05 June 2013 16:44 Go to next message
Jonathan Rodgers is currently offline Jonathan RodgersFriend
Messages: 11
Registered: May 2013
Junior Member
Im trying to populate an ArrayList from my one of my Data Sets and then access that Arraylist in my html TextItem.

I currently have in Script: initialize
var contactsaddresses = new java.util.ArrayList();
reportContext.setPersistentGlobalVariable("contactsaddresses", contactsaddresses);

and in my DataSet Script: onFetch
var tempArray = reportContext.getPersistentGlobalVariable("contactsaddresses");
tempArray.add(row["Address String"]);
reportContext.setPersistentGlobalVariable("contactsaddresses", tempArray);

in my html TextItem:
<!DOCTYPE html><html> <script>
var locationarray = <value-of> reportContext.getPersistentGlobalVariable("contactsaddresses")
</value-of>;

for(i = 0;i<locationarray.length;i++) {
alert(locationarray[i]);
}
</script><body></body></html>

For testing purposes I commented out all the onfetch script, and in the intialize script I used var contactsaddresses = "[['Address String 1'], ['Address String 2']];"; instead of var contactsaddresses = new java.util.ArrayList(); and it worked.

I think my issue could be with my onfetch not updating the array properly or the array is not coming in the html textitem correctly since in testing i had to encapsulate my array with quotes.

Any help or direction is greatly appreciated.
Re: Populating Arraylist from Dataset [message #1062110 is a reply to message #1062070] Thu, 06 June 2013 05:35 Go to previous messageGo to next message
Michael Williams is currently offline Michael WilliamsFriend
Messages: 1925
Registered: July 2009
Senior Member

Try this:

DataSet beforeOpen:
myArray = new java.util.ArrayList();

DataSet onFetch:
myArray.add("'" + row["myField"] + "'");

DataSet beforeClose:
reportContext.setPersistentGlobalVariable("myArray",myArray);

Text box:
<script>
var myArray = <value-of> reportContext.getPersistentGlobalVariable("myArray")</value-of>;

for(i = 0;i<myArray.length;i++) {
alert(myArray[i]);
} 
</script>


Be sure to bind a text box to your dataSet above the text box with the script if it's not used anywhere above this text box already so that it's forced to run to populate the arrayList.

Hope this helps!


Michael

Developer Evangelist, Silanis

[Updated on: Thu, 06 June 2013 05:36]

Report message to a moderator

Re: Populating Arraylist from Dataset [message #1062269 is a reply to message #1062110] Thu, 06 June 2013 21:11 Go to previous messageGo to next message
Jonathan Rodgers is currently offline Jonathan RodgersFriend
Messages: 11
Registered: May 2013
Junior Member
Michael,

You were not kidding about binding the dataset to another object above the desired textbox. Seems strange to me that you have to do that, would have thought that as long as the dataset is bound the textbox you would be good, guess not.

Quote:

Be sure to bind a text box to your dataSet above the text box with the script if it's not used anywhere above this text box already so that it's forced to run to populate the arrayList.


I had a feeling I needed to encapsulate the rowdata with quotes. Which actually simplified the code to allow for 2d array to be passed to my html textbox. (pretty sure not proper way to pass a 2d array but it works.)

Modified the DataSet onFetch:
myArray.add("['"+row["myField1"]+"','"+row["myField2"]+"']");


Modified the TextBox:
<script>
var myArray = <value-of> reportContext.getPersistentGlobalVariable("myArray")</value-of>;

for(i = 0;i<myArray.length;i++) {
  alert(myArray[i][0]+" | "+myArray[i][1]);
} 
</script>


Thanks again this really got me moving in the right direction. Now only to figure out how to get past my geocoding limitations.
Re: Populating Arraylist from Dataset [message #1062499 is a reply to message #1062269] Sat, 08 June 2013 05:56 Go to previous messageGo to next message
Michael Williams is currently offline Michael WilliamsFriend
Messages: 1925
Registered: July 2009
Senior Member

Great! Glad I could help! Let us know whenever you have questions! Smile

Michael

Developer Evangelist, Silanis
icon14.gif  Re: Populating Arraylist from Dataset [message #1268360 is a reply to message #1062070] Mon, 10 March 2014 11:56 Go to previous message
Thirumal m is currently offline Thirumal mFriend
Messages: 10
Registered: March 2014
Junior Member
follow the steps i given below:-

On dataSet event drop down choose beforeOpen and paste the following script

givenNames = new java.util.ArrayList();
familyName = new java.util.ArrayList();

And

choose OnFetch and paste the following script
givenNames.add(row["GivenNames"]);
familyName.add(row["FamilyName"]);
reportContext.setPersistentGlobalVariable("givenNames", givenNames);
reportContext.setPersistentGlobalVariable("familyName", familyName);


Go to layout -> drag and drop the data palette -> in expression paste this script
var str = "";
for (i = 0; i < givenNames.size(); i++) {
str = str + givenNames[i] +" " + familyName[i];
if ((i+1) < givenNames.size()) {
str = str + " and ";
}
}
str
----------------------------------------------------------------
Sorry for bad english but it will work 100%

http://www.eclipse.org/forums/images/message_icons/icon14.gif

Have a nice day.....................
Any help contact m.thirumal@hotmail.com
Previous Topic:Joining Data from Multiple tables
Next Topic:Hiding duplicate records in a group
Goto Forum:
  


Current Time: Tue Apr 23 13:27:41 GMT 2024

Powered by FUDForum. Page generated in 0.03372 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top