jsf - 迭代 h :datatable 内的嵌套列表属性

标签 jsf datatable nested

    <h:dataTable value="#{SearchingBeans.list}" var="entry">
        <h:column>
            <f:facet name="header">
                <h:outputLabel>Photo</h:outputLabel>
            </f:facet>
        </h:column>
        <h:column>
            <f:facet name="header">
                <h:outputLabel>Pseudo</h:outputLabel>
            </f:facet>
            <h:outputLabel value="#{entry.pseudo}"></h:outputLabel>
        </h:column>
        <h:column>
            <f:facet name="header">
                <h:outputLabel>Description</h:outputLabel>
            </f:facet>
            <h:outputLabel value="#{entry.description}"></h:outputLabel>
        </h:column>
        <h:column>
            <f:facet name="header">
                <h:outputLabel>Photo</h:outputLabel>
            </f:facet>
            <h:outputLabel value="#{entry.photo[0].path}"></h:outputLabel> <-- this a List
        </h:column>
    </h:dataTable>

我有一个实体成员,他的一个属性是带有 get/set 的列表照片 该属性已填充 我不知道如何在 jsf 中获取该值我只想要每个成员的第一张照片,因为他们有 2-3 张照片。这可能吗??任何其他解决方案将不胜感激。

最佳答案

只需使用 <ui:repeat> 对其进行迭代即可或 <h:dataTable>通常的方式。将多个迭代组件相互嵌套是完全有效的。如果是<h:dataTable> ,您只需要确保将嵌套的迭代组件放在 <h:column> 中.

例如

<h:dataTable value="#{bean.entities}" var="entity">
    <h:column>
        #{entity.property}
    </h:column>
    <h:column>
        <ui:repeat value="#{entity.subentities}" var="subentity">
            #{subentity.property}
        </ui:repeat>
    </h:column>
</h:dataTable>

<h:dataTable value="#{bean.entities}" var="entity">
    <h:column>
        #{entity.property}
    </h:column>
    <h:column>
        <h:dataTable value="#{entity.subentities}" var="subentity">
            <h:column>
                #{subentity.property}
            </h:column>
        </h:dataTable>
    </h:column>
</h:dataTable>

您可能只会在嵌套多个 <ui:repeat> 时遇到问题成分及用途<f:ajax>在使用旧版本的 Mojarra 时。

仅JSTL <c:forEach>由于此处解释的原因,嵌套在 JSF 迭代组件中时不起作用 JSTL in JSF2 Facelets... makes sense?


具体问题无关,请勿滥用<h:outputLabel>用于纯文本演示。它生成一个 HTML <label>用于 label an input element 的元素通过 for属性。但是,您在代码中的任何地方都没有这样做。你应该使用 <h:outputText>反而。顺便说一句,我最近在初学者代码中更频繁地看到这种情况。一定有某个地方滥用了 <h:outputLabel> 的不良教程或资源这样而不是<h:outputText>甚至模板文本中的普通 EL。您使用的是哪个教程/资源?然后我可以就这个严重的误导联系作者。另见 Purpose of the h:outputLabel and its "for" attribute

关于jsf - 迭代 h :datatable 内的嵌套列表属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16590976/

相关文章:

JSF 身份验证 : cannot intercept error messages

java - 获取输入文本以将其焦点设置在一个特定的命令按钮上

ajax - a4j :commandButton and h:commandButton with a4j:ajax in it 之间的区别

jquery - 根据屏幕宽度显示或隐藏DataTables列

javascript - 如何使用 Vuetify 2 和 Vue JS 2 在 v-data-table 中使用 vue-draggable(或 Sortable JS)?

mysql - 在一个 mysql 表中关联用户列表的最佳方法

css - 工具提示背景颜色变化和工具提示文本在 primefaces 中的字体样式

ios - 如何从另一个 uitableviewcell 中的 tableview 导航?

python - Pyspark 从结构化流中的映射数组中提取值

javascript - 如何提取 json 对象数组中的深层和浅层嵌套字段