I have a simple data table which contains dynamic form text fields. Each field has an ID defined in the backing bean, so I wanted to use that ID to identify each of the h:inputText fields.
我有一个简单的数据表,其中包含动态表单文本字段。每个字段都有一个在后台bean中定义的ID,所以我想使用这个ID来标识每个h:inputText字段。
<h:dataTable id="fieldTable" value="#{bean.indexFields}" var="item">
<h:column id="column">
<h:inputText id="#{item.id}" value="#{bean.values[item.id]}" />
</h:column>
</h:dataTable>
When I try to view the page, JSF generates an error: The id attribute may not be empty
.
当我尝试查看页面时,JSF会产生一个错误:id属性可能不是空的。
If I add a constant to the input ID attribute, it works, but looking at the generated ID the item's ID is not included:
如果我向输入ID属性添加一个常量,它可以工作,但是查看生成的ID,则不包含项目的ID:
<h:inputText id="#{item.id}abc" value="#{bean.values[item.id]}" />
This generates the following output:
这将产生以下输出:
<table id="form:fieldTable">
<tbody>
<tr>
<td><input id="form:fieldTable:0:abc" type="text" name="form:fieldTable:0:abc" title="" /></td>
</tr>
<tr>
<td><input id="form:fieldTable:1:abc" type="text" name="form:fieldTable:1:abc" title="" /></td>
</tr>
<tr>
<td><input id="form:fieldTable:2:abc" type="text" name="form:fieldTable:2:abc" title="" /></td>
</tr>
</tbody>
</table>
Is there a way to include the id from the iterated item in the input id attribute? Why is the ID omitted?
是否有方法在输入id属性中包含来自迭代项的id ?为什么省略ID ?
1 个解决方案
#1
1
Why is the ID omitted?
为什么省略ID ?
Because there's only one component in the view tree. It's only its generated output which get repeated. The #{item}
is not available during view build time.
因为视图树中只有一个组件。它只产生重复的输出。在视图构建时,不能使用#{item}。
Why do you need a different ID? Just give it a fixed ID. JSF will take care about its uniqueness in the generated HTML output. The generated ID is very predictable and you can easily hook on that by JavaScript, if that is your intent.
为什么需要不同的ID?只要给它一个固定的ID。生成的ID是非常可预测的,如果您的目的是这样的话,您可以通过JavaScript轻松地挂起它。
#1
1
Why is the ID omitted?
为什么省略ID ?
Because there's only one component in the view tree. It's only its generated output which get repeated. The #{item}
is not available during view build time.
因为视图树中只有一个组件。它只产生重复的输出。在视图构建时,不能使用#{item}。
Why do you need a different ID? Just give it a fixed ID. JSF will take care about its uniqueness in the generated HTML output. The generated ID is very predictable and you can easily hook on that by JavaScript, if that is your intent.
为什么需要不同的ID?只要给它一个固定的ID。生成的ID是非常可预测的,如果您的目的是这样的话,您可以通过JavaScript轻松地挂起它。