Home
Analytics
reuse javascript - how to put in library
holger_p
Hello,
I am using birt 2.3.2.I am already using a birt library to reuse datasets / parameters and so on.
Now i want to reuse the javascript code too because i now have to develop more complex code.
In the outline i see a "scrips" tab but i cant insert anything there - like i could do with "Report Items".
To me it seems like its possible to put javascript files/functions there and make them available to all reports.
I just dont find an option in the ui how to do this - i am searching now for 1 hour in the documentation / web but i cant find anything.
Can you provide me a small example / explanation how to do this? I would be very grateful.
Thank you very much + Greetings,
Holger
Find more posts tagged with
Comments
pricher
In your project folder, you can create a .js file that will contain all your JavaScript code. Then, in the report, go to Properties --> Resources, and add the reference to that .js file.
The Script tab in the report outline is where you can override the behavior of your report items by calling functions you have created in your .js file. Or, you can call directly the JS code from a data item, as shown in the attached example.
Hope this helps,
P.
holger_p
Hi- just came to work.
First of all, thank you very much for you help, it looks very promising- ill check if its works for me.
Ill make a short post here if i succeed or have any further questions.
Thx again + Greetings,
Holger
holger_p
Hey it works :-)
Thank you very much for you time and sharing your knowledge.
Bye,
Holger
GoranG
<p>Hello.</p><p>
I don't want to open a new topic, since this one seems more than appropriate for what I need.
In our project, we have a common "rptlibrary" that we use in all of our reports. After reading articles about including the .js files in library this seemed to be a nice way to solve our code duplication. (I added a small .js file as the resource in the library) But...
First, I have noticed that our BIRT engine throws an error "could not load a resource file ...", which I solved by calling the "setResourcePath" at starting the platform. After that, this error didn't log anymore, so I assume the file is found and loaded.</p><p> </p><p>As a proof of concept, the file contained only the following code:</p><pre class="_prettyXprint _lang-">function printDeliveryMark(deliveryMark, languageCode) { var deliveryMarkString = "I am here"; return deliveryMarkString;}</pre><p>Using this code in the "Dynamic Text" field by calling </p><pre class="_prettyXprint _lang-">printDeliveryMark(row["DeliveryMark"], row["LanguageCode"])</pre><p>prints out <em>"I am here"</em> text, as expected.</p><p> </p><p>By extending the functionality with something like this:</p><pre class="_prettyXprint _lang-">function printDeliveryMark(deliveryMark, languageCode) { var deliveryMarkString = ""; if (BirtComp.equalTo("de", BirtStr.toLower(languageCode))) { deliveryMarkString = Packages.FULL_CLASS_NAME_TO_ENUM.callToOurMethod(deliveryMark, "de", null); } else { if(deliveryMark == Packages.FULL_CLASS_NAME_TO_ENUM.ENUM_NAME) deliveryMarkString = "another expression"; } return deliveryMarkString;}</pre><p>and this prints out nothing. No string at all. And no error is found in the protocol, no nothing.</p><p>What part of the code is not permitted to be used in javascript files?</p><p> </p><p>Just as a side note: if I copy-paste the same code to the report itself, and define this method in the report-initialize section, everything works. </p><p> </p><p>Please, let me know what part of the code is not permitted to be used in javascript files. Thanks in advance.</p>
GoranG
<p>If you're reading this...I believe I have found the problem.</p><p> </p><p>The problems doesn't lie (totally) in BIRT - with sole exception there is no error printed - but in our code. We have ambiguous methods and looks like it wasn't clear which method to call. so it didn't do anything. </p>
igillen
<p>Thank you for this! Huge help for me to have the freedom to reuse my own functions</p>