我尝试在primefaces中动态创建图表,它工作正常,但是当我想向组件添加ajax行为时,事件不会触发。我的代码有什么问题?
以下是我想在java中动态创建的内容:
<p:panel>
<p:pieChart id="chart_id" rendered="true" value="#{index.pieChart}" legendPosition="w" showDataLabels="true" dataFormat="value"/>
<p:resizable for="chart_id">
<p:ajax event="resize" listener="#{index.onResize}" />
</p:resizable>
</p:panel>
这是事件处理程序:
public void onResize(ResizeEvent event){
//Some code
}
我尝试了很多方法,但我认为这将是创建行为和组件的正确方法:
FacesContext fc = FacesContext.getCurrentInstance();
Application ap = FacesContext.getCurrentInstance().getApplication();
Panel panel = (Panel)ap.createComponent(fc, "org.primefaces.component.Panel","org.primefaces.component.PanelRenderer");
PieChart pieChart = (PieChart)ap.createComponent(fc, "org.primefaces.component.chart.PieChart", "org.primefaces.component.chart.PieChartRenderer");
pieChart.setId("chart_id");
pieChart.setRendered(true);
PieChartModel mod = new PieChartModel();
mod.set("category1", 32);
mod.set("category2", 46);
pieChart.setValue(mod);
pieChart.setLegendPosition("w");
pieChart.setShowDataLabels(true);
pieChart.setDataFormat("value");
panel.getChildren().add(pieChart);
Resizable resizable = (Resizable)ap.createComponent(fc, "org.primefaces.component.Resizable","org.primefaces.component.ResizableRenderer");
resizable.setFor("chart_id");
AjaxBehavior resizeBehavior = (AjaxBehavior)ap.createBehavior(AjaxBehavior.BEHAVIOR_ID);
MethodExpression listener = ap.getExpressionFactory().createMethodExpression(fc.getELContext(), "#{index.onResize}", void.class, new Class[0]);
MethodExpression listenerWithArgs = ap.getExpressionFactory().createMethodExpression(fc.getELContext(), "#{index.onResize}", void.class, new Class[]{ResizeEvent.class});
resizeBehavior.addAjaxBehaviorListener(new AjaxBehaviorListenerImpl(listener, listenerWithArgs));
resizeBehavior.setListener(listenerWithArgs);
resizable.addClientBehavior("resize", resizeBehavior);
panel.getChildren().add(resizable);
return panel;