Skip to main content



      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 12:44 Go to next message
Eclipse UserFriend
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 01:35 Go to previous messageGo to next message
Eclipse UserFriend
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!

[Updated on: Thu, 06 June 2013 01:36] by Moderator

Re: Populating Arraylist from Dataset [message #1062269 is a reply to message #1062110] Thu, 06 June 2013 17:11 Go to previous messageGo to next message
Eclipse UserFriend
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 01:56 Go to previous messageGo to next message
Eclipse UserFriend
Great! Glad I could help! Let us know whenever you have questions! Smile
icon14.gif  Re: Populating Arraylist from Dataset [message #1268360 is a reply to message #1062070] Mon, 10 March 2014 07:56 Go to previous message
Eclipse UserFriend
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: Fri Jul 04 05:12:29 EDT 2025

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

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

Back to the top