简体   繁体   中英

How to pass Object from Birt Report to HTML dynamic text(Layout)

I'm new with BIRT in eclipse. I need to pass Object from BIRT side to HTML dynamic text(in Layout)

onFetch of dataSet

json = {Name: row["Name"],
        Lastname: row["Lastname"],
        Date: row["Date"]};

beforeClose of Dataset

reportContext.setPersistentGlobalVariable("json", json);

and by dynamic Text in Layout

var str = <value-of>reportContext.getPersistentGlobalVariable("json")</value-of>;

i have error Uncaught SyntaxError: Unexpected identifier

var str = [object Object];

Can someone point me in the right direction? Any idea how can i pass object between Birt Report and JavaScript(HTML)? Thanks in advance!

You should explain what your are trying to achieve. Why do you think you need to "pass Object to HTML dynamic text"?

There are two types of dynamic text items in BIRT, and it seems you are intermixing them:

Dynamic Text Item: You specify the value as a Javascript expression, eg "Hello world" or "Hello " + row["NAME"] if your dataset has a column NAME.

Text Item: You specify the value as HTML, eg Hello world, or Hello row["NAME"].

This is comparable to Java Servlets vs Java Server Pages, both can do the same things, but in different ways.

Both of this requires that your layout element can access the data from the dataset. This is usually accomplished by placing the layout element within a Table Item or a List Item which is bound to the data set (see the tab "Binding" in the properties).

For simplicity, I recommend to just forget about "Text Item" and always use "Dynamic Text Item".

Your syntax var str =...; is a non-working syntax mixture.

Inside a "Dynamic Text Item", you probably mean

reportContext.getPersistentGlobalVariable("json")

This won't give you a SyntaxError, because it is a valid JavaScript expression.

But it won't work as you think. It would try to render the JS object as text, probably with the result "[object Object]".

Note that the scripting in BIRT happens inside BIRT and not in the browser.

Do you actually want to generate (with BIRT) a HTML page containing Javascript, where this Javascript is executed in the browser?

Never tried this, but it should be possible:

Make sure to set the type of the "Dynamic Text Item" to HTML and use an expression like this:

("<script type=\"text/javascript\">\n" +
 "alert('Hello world');\n" +
 "</script>\n"
)

or with dynamic content:

("<script type=\"text/javascript\">\n" +
 "alert('Hello " + row["NAME"] + "');\n" +
 "</script>\n"
)

But this gets complicated:

To pass JSON objects this way (as opposed to simple strings like row["NAME"]), you need to write code that writes the JSON content as Javascript source text.

You can pass an object from dataset to Dynamic Text by following steps.

Step-1: Create a report level variable.
Step-2: Inside "Fetch" method just create a object. [As you have created json object]

vars["user"]={
    Name: row["Name"],
    Lastname: row["LastName"],
    Date: row["Date"]
 };

Step-3: on Close/beforeClose method assign the object to variable [As you have done]
Step-4: Create a HTML Dynamic text on your report
Step-5: Access the attributes as below-

vars["user"].Lastname

I have created a sample report. You can refer here:-
https://drive.google.com/file/d/1dlh4qmamGur7voaNnlgamFTOIpAMk3_f/view?usp=sharing

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM