开发者

JSF unable to set dynamic id for input component

my form looks like this:

JSF unable to set dynamic id for input component

When the user clicks "+ Add" a new distance input shows up:

JSF unable to set dynamic id for input component

The user can repeat this operation as many times as desired, but all distances must be filled in, and individual messages must be served for missing data:

JSF unable to set dynamic id for input component

So i'm trying to stablish dynamic ids for the distance inputs. However, when i try to pass dynamic values everything fails. up to now i've tried two diff开发者_开发知识库erent techniques:

  • using the binding HtmlDataTable's rowIndex <- it always shows -1
  • setting the id as an attribute of the Distance class <- ugly exception

here's my code:

Backing bean:

private List<Distance> distances;
private HtmlDataTable distancesUI;

public void addDistance(ActionEvent e) {
    distances.add(new Distance());
}

JSPX:

<h:dataTable
binding="#{Bean.distancesUI}"   
value="#{Bean.distances}"
var="distance"> 
    <h:column> 
        <h:inputText 
        id ="distance_#{Bean.distancesUI.rowIndex}" // <- always renders "distance_-1"
        value="#{distance.value}" />
    </h:column>
</h:dataTable>

I need to be able to show individual "required" messages. Any ideas?

thanks in advance


I need to be able to show individual "required" messages. Any ideas?

For that you don't need to fiddle with IDs. JSF will do it for you. Just do

<h:dataTable id="table" value="#{bean.distances}" var="distance"> 
    <h:column> 
        <h:inputText id="distance" value="#{distance.value}" required="true" />
        <h:message for="distance" />
    </h:column>
</h:dataTable>

This will end up in generated HTML like follows:

<table id="form:table">
    <tr><td><input id="form:table:0:distance" /></td></tr>
    <tr><td><input id="form:table:1:distance" /></td></tr>
    <tr><td><input id="form:table:2:distance" /></td></tr>
    ...
</table>

Rightclick the page in browser and choose View Source to see it yourself.

Any validation error will just be shown in the same row as the input.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜