简体   繁体   English

如何将 Object 从 Birt Report 传递到 HTML 动态文本(布局)

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

I'm new with BIRT in eclipse.我是 eclipse 中的 BIRT 新手。 I need to pass Object from BIRT side to HTML dynamic text(in Layout)我需要将 Object 从 BIRT 端传递到 HTML 动态文本(在布局中)

onFetch of dataSet数据集的onFetch

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我有错误 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)?知道如何在 Birt Report 和 JavaScript(HTML) 之间传递 object 吗? 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"?为什么您认为您需要“将 Object 传递给 HTML 动态文本”?

There are two types of dynamic text items in BIRT, and it seems you are intermixing them: BIRT 中有两种类型的动态文本项,您似乎在混合使用它们:

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.动态文本项:您将值指定为 Javascript 表达式,例如“Hello world”或“Hello”+row[“NAME”],如果您的数据集有列 NAME。

Text Item: You specify the value as HTML, eg Hello world, or Hello row["NAME"].文本项:您将值指定为 HTML,例如 Hello world 或 Hello row["NAME"]。

This is comparable to Java Servlets vs Java Server Pages, both can do the same things, but in different ways.这相当于 Java Servlets vs Java 服务器页面,两者都可以做同样的事情,但方式不同。

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 =...;你的语法 var str =...; is a non-working syntax mixture.是一种无效的语法混合。

Inside a "Dynamic Text Item", you probably mean在“动态文本项”中,您可能是指

reportContext.getPersistentGlobalVariable("json") reportContext.getPersistentGlobalVariable("json")

This won't give you a SyntaxError, because it is a valid JavaScript expression.这不会给你一个 SyntaxError,因为它是一个有效的 JavaScript 表达式。

But it won't work as you think.但它不会像你想象的那样工作。 It would try to render the JS object as text, probably with the result "[object Object]".它会尝试将 JS object 呈现为文本,结果可能是“[object Object]”。

Note that the scripting in BIRT happens inside BIRT and not in the browser.请注意,BIRT 中的脚本编写发生在 BIRT 内部,而不是在浏览器中。

Do you actually want to generate (with BIRT) a HTML page containing Javascript, where this Javascript is executed in the browser?您是否真的想生成(使用 BIRT)一个包含 Javascript 的 HTML 页面,这个 Javascript 在浏览器中执行?

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:确保将“动态文本项”的类型设置为 HTML 并使用如下表达式:

("<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.要以这种方式传递 JSON 对象(而不是像 row["NAME"] 这样的简单字符串),您需要编写将 JSON 内容写入 Javascript 源文本的代码。

You can pass an object from dataset to Dynamic Text by following steps.您可以通过以下步骤将 object 从数据集传递到动态文本。

Step-1: Create a report level variable.步骤 1:创建报告级别变量。
Step-2: Inside "Fetch" method just create a object.第 2 步:在“Fetch”方法中创建一个 object。 [As you have created json object] [因为您已经创建了 json 对象]

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]步骤 3:在 Close/beforeClose 方法中,将 object 分配给变量 [正如您所做的那样]
Step-4: Create a HTML Dynamic text on your report第 4 步:在报表上创建 HTML 动态文本
Step-5: Access the attributes as below-第 5 步:访问属性如下 -

vars["user"].Lastname

I have created a sample report.我创建了一个示例报告。 You can refer here:-你可以参考这里:-
https://drive.google.com/file/d/1dlh4qmamGur7voaNnlgamFTOIpAMk3_f/view?usp=sharing https://drive.google.com/file/d/1dlh4qmamGur7voaNnlgamFTOIpAMk3_f/view?usp=sharing

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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