Assign dynamic ids to hidden fields when iterating over a collection

匿名 (未验证) 提交于 2019-12-03 00:59:01

问题:

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.

回答1:

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:



回答2:

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



回答3:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!