本文介绍了如何将参数传递给p:dataTable中的valueChangeListener?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在dataTable内的<h:selectBooleanCheckbox>上调用valueChangeListener.该数据表又位于另一个(外部)数据表中.在valueChangeListener方法中,我需要外部dataTable的实例对象.有什么方法可以获取外部dataTable实例的对象?

I am calling valueChangeListener on a <h:selectBooleanCheckbox> which is inside a dataTable. and that dataTable is again inside another(outer) dataTable. In the valueChangeListener method I want the instance object of outer dataTable. Is there any way to get the object of outer dataTable instance?

EX:

<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>

我找到了以下解决方案:我使用了<p:ajax>侦听器而不是valueChangeListener,并且我可以将'supplier'对象和supplierAccount对象传递给此侦听器方法.我们可以将任意数量的自定义对象传递给<p:ajax>侦听器.

I found the following solution : I used <p:ajax> listener instead of valueChangeListener, and I could pass 'supplier' object as well as supplierAccount object to this listener method. We can pass any number of custom objects to <p:ajax> listener.

<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>

推荐答案

在这种特殊情况下,您可以通过以编程方式评估#{supplier}来获得它:

In this particular case, you could get it by evaluating the #{supplier} programmatically:

public void checkBoxListener(ValueChangeEvent event) {
    FacesContext context = FacesContext.getCurrentInstance();
    Supplier supplier = context.getApplication().evaluateExpressionGet(context, "#{supplier}", Supplier.class);
    // ...
}

但是,这很丑陋,您正在通过onchange="submit()"同步提交整个表单.我建议为此添加一些ajax.

However, this is plain ugly, you're synchronously submitting the entire form by onchange="submit()". I recommend to throw in some ajax for that.

<h:selectBooleanCheckbox value="#{supplierAccount.supported}">
    <f:ajax listener="#{bean.checkBoxListener}" render="???" />
</h:selectBooleanCheckbox>

(render属性由您决定)

(the render attribute is up to you)

使用

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中指定方法参数:

Or if your environment supports EL 2.2 and thus specifying method arguments in EL:

<h:selectBooleanCheckbox value="#{supplierAccount.supported}">
    <f:ajax listener="#{bean.checkBoxListener(component, supplier)}" render="???" />
</h:selectBooleanCheckbox>
public void checkBoxListener(UISelectBoolean checkbox, Supplier supplier) {
    boolean selected = checkbox.isSelected();
    // ...
}

另请参见:

  • 何时使用valueChangeListener或f:ajax监听器?
  • See also:

    • When to use valueChangeListener or f:ajax listener?
    • 无关与具体问题有关,对于使用onchange="submit()"而言,了解onchange不适用于IE6/7中的复选框可能会很有用.仅在第二次点击时触发.您宁愿使用onclick="submit()"代替.

      Unrelated to the concrete problem, as to using onchange="submit()", it may be useful to know that onchange doesn't work as expected for checkboxes in IE6/7. It get only fired on every 2nd click. You rather want to use onclick="submit()" instead.

      这篇关于如何将参数传递给p:dataTable中的valueChangeListener?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 10:36