gpt4 book ai didi

javascript - 将 java 变量传递给 javascript 函数

转载 作者:行者123 更新时间:2023-11-28 01:53:37 25 4
gpt4 key购买 nike

我需要将 java 变量传递给 javascript 函数。我的代码如下。想知道这是正确的方法吗?

因为当页面首次加载并单击按钮时,我遇到了一些问题,图表数据和类别为空。

我期待以下内容:

chartData = [{"name":"Anne","data":[1.0,0.0,4.0]},
{"name":"Billy","data":[5.0,7.0,10000.0]}]
categories = ["APPLES","BANANAS","ORANGES"]

public String doLoadChartDataAction () {

String _cat = "[{\"name\":\"Anne\",\"data\":[1.0,0.0,4.0]},{\"name\":\"Billy\",\"data\":[5.0,7.0,10000.0]}]";
String _data = "[\"APPLES\",\"BANANAS\",\"ORANGES\"]";

System.out.println("1.0 " + " _cat:" + _cat);
System.out.println("1.0 " + " _data:" + _data);

setCategories(_cat);
setChartData(_data);

return "";
}


<p:commandButton styleClass="commandButton" value="This" id="btnThis" action="#{pc_Test.doLoadChartDataAction}"
oncomplete="renderChart('container','line','Sample Chart','${pc_Test.chartData}', '${pc_Test.categories}');">
</p:commandButton>

最佳答案

public String doLoadChartDataAction () {

String _cat = "[{\"name\":\"Anne\",\"data\":[1.0,0.0,4.0]},{\"name\":\"Billy\",\"data\":[5.0,7.0,10000.0]}]";
String _data = "[\"APPLES\",\"BANANAS\",\"ORANGES\"]";

System.out.println("1.0 " + " _cat:" + _cat);
System.out.println("1.0 " + " _data:" + _data);

//setCategories(_cat);
//setChartData(_data);

RequestContext context = RequestContext.getCurrentInstance();
context.addCallbackParam("chartData", _data);
context.addCallbackParam("categories", _cat);

return "";
}

<p:commandButton styleClass="commandButton" value="This" id="btnThis" action="#{pc_Test.doLoadChartDataAction}"
oncomplete="renderChart('container','line','Sample Chart', args.chartData, args.categories);">

关于javascript - 将 java 变量传递给 javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19420471/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com