Home
Analytics
Read data form dataset with JavaScript
ShlomiW
Hi all,<br />
<br />
I would like to know what is the easiest way to read data from a data-set by using JavaScript?<br />
<br />
What I'm trying to do is:<br />
I have a query that return a list of ages<br />
<strong class='bbc'>Query Text:</strong><br />
<pre class='_prettyXprint _lang-auto _linenums:0'>SELECT age as ages, age_code AS code
FROM table1
</pre>
<br />
<strong class='bbc'>Query return value:</strong><br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
age | code
0 - 18 | 1
19 - 30 | 2
31 - 50 | 3
51+ | 4
</pre>
<br />
What I would like to do is to create a drop-down list that will have all those values.<br />
Meaning that the value will be equal to the code and the text will be equal to the age.<br />
<br />
For example, if I where to write the drop down manually the code will be like that:<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
<select id="list1">
<option value="default"></option>
<option value="1">0 - 18</option>
<option value="2">19 - 30</option>
<option value="3">31 - 50</option>
<option value="4">51+</option>
</select>
</pre>
<br />
<strong class='bbc'>The main issue is to create a scenario that is completely automatically, because the ages values or code can change and I do not want to have to make change is the HTML code as well every time</strong><br />
<br />
I have tried many ways to do that but without much success, <br />
Please help me<br />
Thanks a lot<br />
Shlomi
Find more posts tagged with
Comments
kclark
<a class='bbc_url' href='
http://www.birt-exchange.org/org/devshare/designing-birt-reports/1342-populating-a-drop-down-list-in-a-birt-report/'>Take
a look at this devshare</a> it should be exactly what you're looking for. The example uses a query like this<br />
<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
select distinct country
from customers
order by 1
</pre>
<br />
And the resulting dataset is populated automatically in a drop down box on the report.
ShlomiW
Hi,
First thanks for the quick replay.
Second, I have seen this solution, but it involves using a hidden table and 2 parts of Java code.
And I'm trying to find a way that is more simple Or at least uses only 1 type of code, meaning only JavaScript OR only Java.
Hope that maybe you will have a different idea.
Have a geate day
Shlomi
kclark
I've attached an example that I created for you to look at. The easiest way I can think to do this with the least amount of moving parts is to create a CONCATENATE computed column for the data you're trying to use. Separating it with some character not used in that data. In my case I used ~. Then add that element onto your table making it invisible.<br />
<br />
Then you want to set this data in a PGV from onCreate()<br />
<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>var temp = new String(this.getValue());
reportContext.setPersistentGlobalVariable("values", temp);</pre>
<br />
Now you can add a text item to your report splitting the PGV into an array. Once it's in an array you can add the items to the list box.<br />
<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>var list = reportContext.getPersistentGlobalVariable("values");
var array = list.split('~');
var total = (list.split('~').length - 1)
var listbox = "<select name=\"sometext\">";
for(i=0;i<=total;i++) {
listbox = listbox + "<option>" + array[i] + "</option>";
}
listbox = listbox + "</select>";
this.text = listbox;</pre>
<br />
It's very similar to the devshare I linked but it might work better for you. Let me know
ShlomiW
Hi,<br />
<br />
Again thanks for the reply.<br />
<br />
But this still leaves me with having to write 3 pieces of code and a "data" field.<br />
<br />
Now I'm thinking pf maybe using Parameters, meaning to populate a parameter with all values from a query and then to use this parameter to create my Drop-Down list.<br />
I have found <a class='bbc_url' href='
http://www.birt-exchange.org/org/forum/index.php/topic/23082-parameter-with-dynamic-multi-default-value/'>this</a>
; topic that talks on something like what I want to do, but as you can see there is no finale answer.<br />
<br />
So if there is any way to reply to that topic or if you have a remarks on why this is not an efficient way I would like that.<br />
<br />
Waiting to hear form you again<br />
As always have a great day<br />
Shlomi
Tubal
Maybe I don't understand what the problem is with the other solutions, but could you not load the query results into a global variable in an onFetch, and then use javascript in an HTML text element to split the global variable and make your dropdown?
i.e.:
- create a global variable in the report's initialize or beforeFactory event
- create a hidden table at the top of your report that's bound to your dataset, so your dataset will load
- in the onFetch event of the dataset, load your global variable with an array or comma delimited list
- generate your drop down from your global variable
You could theoretically even generate your entire dropdown code in the global variable and then just insert it into your html text element.
I'm not sure what you consider java code, but I don't think there would be any java coding in this solution?
ShlomiW
I have solve this issue in a different way, this is what I did.<br />
I have added the following code in to my report<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
var runnable = reportContext.getReportRunnable();
var engine = runnable.getReportEngine();
var tsk = engine.createGetParameterDefinitionTask( runnable );
agesList = new java.util.ArrayList();
selectionList = tsk.getSelectionList("agesList");
if (selectionList != null)
{
for ( var sliter = selectionList.iterator( ); sliter.hasNext( ); )
{
var selectionItem = sliter.next( );
agesList.add("\"" + selectionItem.getLabel() + "\"");
}
</pre>
<br />
I have placed this code in the "beforeFactory" section of the data-set.<br />
<br />
It allows me to read data form a parameter named "agesList".<br />
For this parameter I set the "Display text" field to be "age" + "age_code".<br />
<br />
So in the "agesList" variable I will have all the values of the data-set, and then I can call this VAR<br />
form the JS code (by using <pre class='_prettyXprint _lang-auto _linenums:0'>var array = <VALUE-OF> agesList </VALUE-OF>;</pre>) and then use the NEW JS var ("array")to create my Drop-Down list.<br />
<br />
I think that for me this is a better solution because I did not need to use any hidden tables in the report design.<br />
<br />
I would like to hear your thoughts on this solution.<br />
Have a great week <br />
Shlomi