How to pass parameter to valueChangeListener in p:dataTable?
我在数据表内的
例如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <h:panelGroup id="panelId"> <p:dataTable id="outerDatatable" var="supplier" value="bean.supplierList"> <p:column> <f:facet name="header"> <h:outputText value="Suppliers" /> </f:facet> <h:outputText value="#{supplier.name}" /> </p:column> <p:column> <p:dataTable id="innerDataTable" var="supplierAccount" value="supplier.supplierAccountList"> <p:column> <h:selectBooleanCheckbox id="booleanBoxId" value="#{supplierAccount.supported}" valueChangeListener="#bean.checkBoxListener}" immediate="true" onchange="this.form.submit();"/> </p:column> </p:dataTable> </p:column> </p:dataTable> </h:panelGroup> |
我找到了以下解决方案:我使用了
1 2 3 4 5 6 7 8 9 | <p:column> <h:selectBooleanCheckbox id="booleanBoxId" value="#{supplierAccount.supported}" immediate="true"> </h:selectBooleanCheckbox> <p:ajax listener="#{bean.myListenerMethod(supplier,supplierAccount)}" update=":formName:panelId"/> </p:column> |
在这种特殊情况下,您可以通过以编程方式评估
1 2 3 4 5 | public void checkBoxListener(ValueChangeEvent event) { FacesContext context = FacesContext.getCurrentInstance(); Supplier supplier = context.getApplication().evaluateExpressionGet(context,"#{supplier}", Supplier.class); // ... } |
然而,这实在是太丑了,您正在通过
1 2 3 | <h:selectBooleanCheckbox value="#{supplierAccount.supported}"> <f:ajax listener="#{bean.checkBoxListener}" render="???" /> </h:selectBooleanCheckbox> |
(
与
1 2 3 4 5 6 | public void checkBoxListener(AjaxBehavior event) { Boolean value = (Boolean) ((UIInput) event.getComponent()).getValue(); FacesContext context = FacesContext.getCurrentInstance(); Supplier supplier = context.getApplication().evaluateExpressionGet(context,"#{supplier}", Supplier.class); // ... } |
或者如果您的环境支持 EL 2.2 并因此在 EL 中指定方法参数:
1 2 3 | <h:selectBooleanCheckbox value="#{supplierAccount.supported}"> <f:ajax listener="#{bean.checkBoxListener(component, supplier)}" render="???" /> </h:selectBooleanCheckbox> |
1 2 3 4 | public void checkBoxListener(UISelectBoolean checkbox, Supplier supplier) { boolean selected = checkbox.isSelected(); // ... } |
也可以看看:
- 何时使用 valueChangeListener 或 f:ajax 侦听器?
与具体问题无关,至于使用
我看到你在
1 | valueChangeListener="#{bean.checkBoxListener}" immediate="true" |
此外,由于您使用的是 Primefaces,您可以使用它的组件(如果您使用版本 3):http://www.primefaces.org/showcase-labs/ui/selectBooleanCheckbox.jsf<铅>
如果你使用 jsf 2 就没有必要使用 outputText:
1 2 3 | <f:facet name="header"> Suppliers </f:facet> |
也没有必要使用
的属性
1 2 3 | <p:column headerText="Suppliers"> #{supplier.name}" </p:column> |
那样就简单多了,不是吗?
PS:这是什么?