开发者

JSF dataTable with selectOneListbox

I have a dataTable that lists some objects and I want to set a property for those objects using a selectOneListbox. This is my dataTable

<h:dataTable value="#{someHandler.entities}"
                binding="#{someHandler.dataTable}" var="entitiy">
               <h:column>
                    <f:facet name="header">
                        <t:outputText value="Level" />
                    </f:facet>
                    <h:selectOneListbox id="level" value="#{entitiy.level}" size="1"
                        valueChangeListener="#{someHandler.changeLevel}"
                        onchange="submit()">
                        <f:selectItem itemValue="-" itemLabel="-" />
                        <f:selectItem itemValue="ALL" itemLabel="ALL" />
                        (and so on)
                    </h:selectOneListbox>
                </h:column>
                <h:column>
                    <f:facet name="header">
                        <t:outputText value="Name" />
                    </f:facet>
                    <h:outputText value="#{entitiy.name}" />
                </h:column>
</h:dataTable>

The valueChangeListener looks like this:

public void changeLevel(ValueChangeEvent event) {
    String newLevel = (String) event.getNewValue();
    Logger logger = (Logger) dataTable.getRowData();        
    logger.setLevel(Level.toLevel(newLevel));
}

(dataTable is an HtmlDataTable object.)

However, the event object is always the same - no matter which row the selectOneListbox was in. (It seems always the logger in the first row). The Logger object I get is also not the one I want.

Any ideas? Thank you!

开发者_如何学JAVA

And anothers questions? Is the entitiy.setLevel() method called even though I have a valueChangeListener? I use entitiy.level because I want to show the chosen level as a default for those entity.

Thank you!


There are two potential problems here:


First, the onchange="submit()" submits the entire form. The valueChangeListener will be invoked on all input elements of which the submitted value differs from the initial value.

You need to preset the value behind #{entitiy.level} with the same value as the default menu option. E.g. in the constructor.

public Entity() {
    level = "-";
}

Or, better, make the default value null.

<f:selectItem itemValue="#{null}" itemLabel="-" />

so that the valueChangeListener won't be invoked when the particular menu is not changed.

Or, when you are already on JSF 2.x (please always mention exact JSF impl/version in your JSF questions), you can use <f:ajax> tag for this without the need for a valueChangeListener with a hacky onchange="submit()".


Second, you need to ensure that the datatable value #{someHandler.entities} returns exactly the same list during the submit as it was during the initial request. So, do the data loading in the bean (post)constructor. In JSF 2.x you'd like to put the bean in the view scope as well.


Unrelated to the concrete problem, you can also just use <h:selectOneMenu> instead of a <h:selectOneListbox size="1">.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜