h:dataTable “var” attribute doesn't work inside facet EL

眉间皱痕 提交于 2019-12-13 20:42:08

问题


My problem is that I can't figure out how to make EL code inside <f:facet> work with <h:dataTable> var attribute.

JSF code:

<h:dataTable value="#{backingBean.someList}" var="absolutelyUniqueVar">
    <h:column>
       <f:facet name="header">#{backingBean.someEntity}</f:facet>
       <h:outputText value="#{backingBean.someEntity.someField}"/>
    </h:column>
    <h:column>
       <f:facet name="header">#{absolutelyUniqueVar.anotherField}</f:facet>
       <h:outputText value="#{absolutelyUniqueVar.anotherField}"/>
    </h:column>
</h:dataTable>

What it produces:

<table>
    <thead>
       <tr>
           <th>someEntityClass@f613189e</th>
           <th></th>
       </tr>
    </thead>
    <tbody>
       <tr>
           <td>someField</td>
           <td>anotherField</td>
       </tr>
    </tbody>
</table>

As you can see, EL in first <f:facet> works fine, but the second one produces nothing.

I'm not familiar with JSF, so I'll appreciate any help. Thanks.


回答1:


This is as to be expected. It makes no sense to use the value of an iterating variable in a common header. What would you show there? What should it do if it reaches the second row? Overwrite the value of the first element? Then in the end you'd only see the value of the last element. But since this also does not make sense, the thing you want is just not possible. It does not make sense in any way... with or without JSF.

Quote from a comment by @BalusC from the see also that also applies here:

.. but you're not terribly clear on which row exactly you want to show in the column header and why exactly. Usually, a table doesn't work like that. The column header should represent a description of the column, e.g. "Name", "Email", "Title", "Price", etc. In your specific case, "Key" and "Value" would be candidate headers for those two columns. Note that this is not a JSF problem, but just a general model/design problem as to tabular data. You'd have exactly the same problem when trying the same in any other presentation framework.

And if you want to use the value of a certain element (e.g. a common unit of measuer) then explicitly reference one (the first?) element in the header:

#{backingBean.someList[0].uom}

See also

  • h:dataTable header facet does not display value from row


来源:https://stackoverflow.com/questions/55118562/hdatatable-var-attribute-doesnt-work-inside-facet-el

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