Tuesday, 20 December 2011

JSF with AJAX Example using slectonemenuItem


//following managed bean and xhtml file will give the some idea


com.example.Item
public class Item {

    private String value1;
    private String value2;

    // Generate public getters/setters.  
}
com.example.Bean
@ManagedBean
@ViewScoped
public class Bean {

    private List<Item> items;
    private DataModel<Item> model;
    private List<String> list;

    @PostConstruct
    public void init() {
        items = Arrays.asList(new Item(), new Item(), new Item());
        model = new ListDataModel<Item>(items);
        list = Arrays.asList("one", "two", "three");
    }

    public void change(AjaxBehaviorEvent e) {
        Item item = model.getRowData();
        item.setValue2(item.getValue1());
    }

    public DataModel<Item> getModel() {
        return model;
    }

    public List<String> getList() {
        return list;
    }

}
test.xhtml
<h:form>
    <h:dataTable value="#{bean.model}" var="item">
        <h:column>
            <h:selectOneMenu value="#{item.value1}">
                <f:selectItem itemLabel="select..." itemValue="#{null}" />
                <f:selectItems value="#{bean.list}" />
                <f:ajax execute="@this" listener="#{bean.change}" render="list2" />
            </h:selectOneMenu>
        </h:column>
        <h:column>
            <h:selectOneMenu id="list2" value="#{item.value2}">
                <f:selectItem itemLabel="select..." itemValue="#{null}" />
                <f:selectItems value="#{bean.list}" />
            </h:selectOneMenu>
        </h:column>
    </h:dataTable>
</h:form>

No comments:

Post a Comment