我试图打印<p:repeat></p:repeat>
标记内生成的一些图表对象。
我知道由于使用了HTML5 Canvas,常规<p:printer>
标记不适用于primefaces图表? Canvii?
无论哪种方式,我从另一个SO帖子中抓取this JS function,但我无法按照我想要的方式工作。
这是我的图表面板:
<script type="text/javascript">
function exportChart(component) {
//export image
$('#output').empty().append(PF(component).exportAsImage());
//show the dialog
PF('dlg').show();
}
</script>
<p:dialog widgetVar="dlg" showEffect="fade" modal="false" header="Druckbares Diagramm als Bild" resizable="true">
<p:outputPanel id="output" layout="block" style="width:1000px;height:300px"/>
<p:commandButton onClick="javascript:PF('dlg').hide()" value = "Schließen"/>
</p:dialog>
<p:panel id="charts">
<p:repeat value="#{AttributeChartsController.kacModel.chartWrappers}" var="wrapper" style="height: #{wrapper.drawHeight}" >
<p:chart type="bar" model="#{wrapper.chart}" style="height: #{wrapper.drawHeight}" widgetVar="#{wrapper.widgetVarIdStr}"/>
<p:commandLink id="lnk#{wrapper.widgetVarIdStr}" onclick="print(#{wrapper.widgetVarIdStr})">Print</p:commandLink>
</p:repeat>
</p:panel>
我将widgetVar
设置为widgetVarIdStr
,这是为每个图表生成的,如下所示:
//these are values retrieved from a database
long widgetVarId = 0;
for(Entry<String, HashMap<String, String>> entry : keyAttribs.entrySet()) {
this.chartWrappers.add(createChartWrapper(entry.getKey(), entry.getValue()));
widgetVarId++;
}
private ChartWrapper createChartWrapper(String attribName, HashMap<String, String> keyValue) {
//... create Chart, ChartSeries, enter values, etc
wrapper.setWidgetVarId(widgetVarId);
return wrapper;
}
结果就是这样:
图表工作,打印按钮在那里并且具有正确的值,但是widgetVar似乎没有在<p:repeat>
元素内正确设置。 widgetVar
属性也无法在最终生成的页面源中找到,这是正常的吗?
这是我的chartWrapper,这可能是一种更优雅的方式来做我想做的事情,但我会坚持下去:
public class ChartWrapper {
private HorizontalBarChartModel chart;
private long drawHeight;
private long widgetVarId;
public HorizontalBarChartModel getChart() {
return chart;
}
public void setChart(HorizontalBarChartModel chart) {
this.chart = chart;
}
public long getDrawHeight() {
return this.drawHeight();
}
public void setDrawHeight(long drawHeight) {
this.drawHeight = drawHeight;
}
public String getDrawHeightStr() {
return drawHeight+"px";
}
public long getWidgetVarId() {
return widgetVarId;
}
public void setWidgetVarId(long widgetVarId) {
this.widgetVarId = widgetVarId;
}
public String getWidgetVarIdStr() {
return "attrChart"+widgetVarId;
}
}
答案 0 :(得分:1)
我在评论部分的帮助下找到了我的问题的解决方案,它归结为我的jscript调用中的一些缺失的引号
onclick="exportChart(#{wrapper.widgetVarIdStr})"
无效,因为它试图将对象传递给函数。我按照惯例改变了它:
onclick="exportChart('#{wrapper.widgetVarIdStr}')"
所以现在实际的widgetVar作为字符串传递给函数,它按预期工作。