Assign dynamic ids to hidden fields when iterating

2019-07-16 21:45发布

Is there a way to assign dynamic ids to h:inputHidden components?

EDIT1

I am trying to assign the ids inside a ui:repeat tag when iterating over a collection of elements.

3条回答
来,给爷笑一个
2楼-- · 2019-07-16 21:54

You can dynamically add any random number also (id="#{}"),but

add functionally related ids to hidden components, that will be helpful

for example if it is employee form ,you may add empid to it.

查看更多
在下西门庆
3楼-- · 2019-07-16 22:01

It is not possible to set the ID based on the iteration value of an <ui:repeat>. But you don't need it anyway. They will by default already get dynamic and unique IDs based on the iteration index.

E.g.

<h:form id="form">
    <ui:repeat value="#{bean.list}" var="item">
        <h:inputHidden id="hidden" value="#{item.value}" />
    </ui:repeat>
</h:form>

will generate this HTML during view render time

<form id="form" name="form">
    <input type="hidden" id="form:0:hidden" name="form:0:hidden" value="item1value" />
    <input type="hidden" id="form:1:hidden" name="form:1:hidden" value="item2value" />
    <input type="hidden" id="form:2:hidden" name="form:2:hidden" value="item3value" />
</form>

If you want to manually control the ID, you'd need to use <c:forEach> instead, because <ui:repeat> doesn't generate multiple JSF components, but lets its children (which is a single <h:inputHidden> in the above example) generate HTML multiple times. The <c:forEach> will generate multiple JSF components which then each generate HTML only once (so you effectively end up with multiple <h:inputHidden> components in JSF component tree).

E.g.

<h:form id="form">
    <c:forEach items="#{bean.list}" var="item">
        <h:inputHidden id="#{item.id}" value="#{item.value}" />
    </c:forEach>
</h:form>

which will basically generate this JSF component tree during view build time

<h:form id="form">
    <h:inputHidden id="item1id" value="#{bean.list[0].value}" />
    <h:inputHidden id="item2id" value="#{bean.list[1].value}" />
    <h:inputHidden id="item3id" value="#{bean.list[2].value}" />
</h:form>

which in turn will generate this HTML during view render time

<form id="form" name="form">
    <input type="hidden" id="form:item1id" name="form:item1id" value="item1value" />
    <input type="hidden" id="form:item2id" name="form:item2id" value="item2value" />
    <input type="hidden" id="form:item3id" name="form:item3id" value="item3value" />
</form>

See also:

查看更多
叛逆
4楼-- · 2019-07-16 22:02

They get assigned a dynamic ID by default. You can also specify id="#{..} to customize it.

查看更多
登录 后发表回答