考虑一个虚拟案例:
<h:form id="wrapperForm">
<h:panelGroup id="rowsContainer">
<h:dataTable id="rowsTable" value="#{bean.rows}" var="row" >
<h:column>
<h:commandButton value="Click me to update (#{component.parent.parent.parent.clientId})">
<f:ajax render=":#{component.parent.parent.parent.clientId}" />
</h:commandButton>
</h:column>
</h:dataTable>
</h:panelGroup>
</h:form>
单击按钮时,id=rowsContainer
会按预期成功更新。
但是,如果我在那里添加 ui:repeat
,它就不再起作用了:
<h:form id="wrapperForm">
<ui:repeat id="blocksRepeat" var="block" value="#{bean.blocks}">
<h:panelGroup id="rowsWrapper">
<h:dataTable id="rowsTable" value="#{block.rows}" var="row" >
<h:column>
<h:commandButton value="Click me 2 update (#{component.parent.parent.parent.clientId})">
<f:ajax render=":#{component.parent.parent.parent.clientId}" />
</h:commandButton>
</h:column>
</h:dataTable>
</h:panelGroup>
</ui:repeat>
</h:form>
取而代之的是:
<f:ajax> contains an unknown id ':wrapperForm:blocksRepeat:0:rowsWrapper' - cannot locate it in the context of the component j_idt363
但是,该组件确实具有该 ID,因此 EL 应该没问题。 ui:repeat
以某种方式打破了这种情况。是否可能在实际循环之前尝试评估 EL?
如何从数据表中引用 rowsWrapper
元素?
注意:我最近问了关于 odd dataTable naming within ui:repeat 的问题,这turned out to be a bug .然而,这个问题不应该与此相关,因为我按照建议将 dataTable 包装在 panelGroup 中 here .
最佳答案
实际上有两件事出了问题:
1) ui:repeat 被破坏
正如 BalusC 在问题的评论中所回答的那样,第一个问题(再次)出现是由于 a bug in Mojarra .好像ui:repeat
连装有h:dataTable
的包装容器都被打破了没有帮助。更多详细信息,请参见问题“Why doesn't h:dataTable inside ui:repeat get correct ID?”和该问题的评论。
正如 BalusC 所建议的,解决方法是使用 h:dataTable 而不是 ui:repeat
.它将提供不方便的 ( <table>
) HTML 但有效。这消除了在向内部迭代添加行和从内部迭代中删除行时出现的一些奇怪问题。
注意:关于 ui:repeat
的一些问题似乎在 Mojarra 2.0.3 中已修复,但并非全部。
2) 引用失败
即使是 h:dataTable
解决方法,引用内部h:dataTable
从里面的按钮失败。因为没有 ui:repeat
在使用中,这一定是datatable的内部问题。我目前看不到任何解决方案,所以我 filed a ticket for this behavior as well .
关于java - 如何在数据表中引用数据表父级?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3882147/