gpt4 book ai didi

javascript - JS代码未执行

转载 作者:行者123 更新时间:2023-11-29 15:35:31 26 4
gpt4 key购买 nike

我目前正在尝试调用 JS 脚本以便从 primefaces 图表组件导出图表。 问题是 base64str 变量似乎为空,并且由于某种原因没有调用负责填充该值的脚本:

xhtml代码:

<p:chart id="chart" type="line"  widgetVar="chart" model="#{cont.lineModel}"  style="height:550px;width:1800px">
<p:ajax event="itemSelect" listener="#{cont.itemSelect}" update="growl" />
</p:chart>
<p:commandButton id="exp" value="Export" icon="ui-icon-extlink"
onclick="exportChart();" actionListener="#{cont.submittedBase64Str}"
/>
<h:inputHidden id="b64" value="#{cont.base64Str}" />


<script type="text/javascript">
function exportChart() {
img = chart.exportAsImage();
document.getElementById('hform:b64').value = img.src;

}
</script>

Controller :

public void submittedBase64Str(ActionEvent event){
// You probably want to have a more comprehensive check here.
// In this example I only use a simple check
if(base64Str.split(",").length > 1){
String encoded = base64Str.split(",")[1];
byte[] decoded = org.apache.commons.codec.binary.Base64.decodeBase64(encoded);
// Write to a .png file
try {
RenderedImage renderedImage = ImageIO.read(new ByteArrayInputStream(decoded));
ImageIO.write(renderedImage, "png", new File("D:\\out.png"));
} catch (IOException e) {
e.printStackTrace();
}
}

谢谢

最佳答案

onclick 属性更改为 onstart

    <p:commandButton id="exp" value="Export" icon="ui-icon-extlink"
onstart="exportChart();" actionListener="#{cont.submittedBase64Str}" />

那应该调用JS函数。

编辑

此外,您需要在函数中定义imgchart

关于javascript - JS代码未执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29709815/

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