jsfrichfacesvaluechangelistener

ValueChangeListener for a particular item in a Collection in a <a4j:repeat> tag


How could I set fields of particular Driver in a List<Driver> by standart decision? without my own attribute:

<f:attribute name="hashCode" value="#{driver.hashCode()}"/>

Java BackBean:

public class BackBean {
    List<Driver> drivers;

    public List<Driver> getDrivers() {
        List<Driver> drivers = new ArrayList<Driver>();
        ...
        return drivers;
    }

    public void licenceReplacedCheckBoxChanged(ValueChangeEvent e) {
        Boolean driverDocChanged = (Boolean) e.getNewValue();

        int hashCode = (Integer) e.getComponent().getAttributes().get("hashCode");

        for (Driver buff : getDrivers()) {
            boolean findDriver = ( buff.hashCode() == hashCode );
            if (findDriver) {
                buff.setDocumentReplaced(driverDocChanged);
                buff.makeChangeDriver();
            }
        }
    }
}

xhtml:

<a4j:repeat value="#{backBean.drivers}" var="driver" rowKeyVar="rowDriver" id="drivers">
...
    <div class="controls one-control-group">
        <h:selectBooleanCheckbox value="#{driver.documentReplaced}"                 
            valueChangeListener="#{backBean.licenceReplacedCheckBoxChanged}">
            <f:attribute name="hashCode" value="#{driver.hashCode()}"/>
            <a4j:ajax execute="@this" render="..." />
        </h:selectBooleanCheckbox>
    </div>
...
</a4j:repeat>

Solution

  • Simply call the desired method in <a4j:ajax listener>.

    <h:selectBooleanCheckbox value="#{driver.documentReplaced}">                 
        <a4j:ajax execute="@this" listener="#{driver.makeChangeDriver}" render="..." />
    </h:selectBooleanCheckbox>
    

    See also:


    Nonetheless, you could in your attempt just have passed the #{driver} itself instead of its hashcode.

    <f:attribute name="driver" value="#{driver}" />
    

    It's only an unnecessary detour.