简体   繁体   English

具有动态列的DataTable

[英]DataTable with dynamic columns

I am completely new to JSF, and just attempting a proof of concept to decide whether it will be useful for a project. 我是JSF的新手,只是尝试一个概念证明来决定它是否对项目有用。 My POC simply consists of a single page, with a table, containing some data. 我的POC只包含一个页面,其中包含一些包含一些数据的表格。

The number of columns (as well as the number of rows) is dynamic, loaded from a database before the page is rendered. 列数(以及行数)是动态的,在呈现页面之前从数据库加载。

With the following, I get two static columns with the appropriate number of rows, as I'd expect: 通过以下内容,我得到两个具有适当行数的静态列,正如我所期望的那样:

<h:dataTable id="data" value="#{viewDataBean.dataRows}" var="row">
    <h:column>
        <f:facet name="header">
            <h:outputText value="Col 1"/>
        </f:facet>
        <h:outputText value="#{row.values[0].value}"/>
    </h:column>
    <h:column>
        <f:facet name="header">
            <h:outputText value="Col 2"/>
        </f:facet>
        <h:outputText value="#{row.values[1].value}"/>
    </h:column>
</h:dataTable>

What I wanted to do was to add a <c:forEach...> around a single set of <h:column>...</h:column> tags, to iterate over the number of columns, but that didn't work - I tried a variety of combinations, but I was expecting something like this to work: 我想要做的是在一组<h:column>...</h:column>标签周围添加一个<c:forEach...>来迭代列数,但是没有工作 - 我尝试了各种组合,但我期待这样的工作:

<h:dataTable id="data" value="#{viewDataBean.dataRows}" var="row">
    <c:forEach items="#{row.values}" var="val">
        <h:column>
            <f:facet name="header">
                <h:outputText value="Col 1"/>
            </f:facet>
            <h:outputText value="#{val.value}"/>
        </h:column>
    </c:forEach>
</h:dataTable>

From Googling, I've read various vague comments like 'that's not the way to do it', but I haven't been able to find any particularly compelling examples of the right way. 从谷歌搜索,我已经阅读了各种模糊的评论,比如'那不是那样做的',但我还没有找到任何特别引人注目的正确方法的例子。 Someone mentioned building the DataTable in the backing bean, but the only example of that I could find was http://forums.sun.com/thread.jspa?threadID=577589 . 有人提到在支持bean中构建DataTable,但我能找到的唯一例子是http://forums.sun.com/thread.jspa?threadID=577589 It worked, but felt kind of clumsy, especially since some of the methods used are deprecated. 它有效,但感觉有点笨拙,特别是因为使用的一些方法已被弃用。

At the moment, it's looking unlikely that I'll be able to use any libraries other than core JSF, but that might change if absolutely necessary. 目前,看起来我不太可能使用除核心JSF之外的任何库,但如果绝对必要,这可能会改变。 Can anyone shed any light on the right way to do this? 任何人都可以对正确的方法有所了解吗? It seems like it should be pretty simple, so I'm sure I'm just missing something obvious. 它看起来应该很简单,所以我肯定我只是遗漏了一些明显的东西。

JSTL and JSF doesn't run in sync as you'd expect from the coding. JSTL和JSF没有按照您对编码的期望同步运行。 It's more so that JSTL first processes the entire page from top to bottom first and then hands over the result to JSF for further processing. 更重要的是,JSTL首先从上到下处理整个页面,然后将结果交给JSF进行进一步处理。 The #{row} is unavailable at the time JSTL is busy, so your attempt indeed won't work. JSTL忙时#{row}不可用,因此您的尝试确实无效。

The linked topic indeed mentions deprecated methods, but the topic is also aged and discusses legacy JSF 1.0/1.1. 链接主题确实提到了已弃用的方法,但该主题也已老化并讨论了旧版JSF 1.0 / 1.1。 If you explore the current Javadocs of the mentioned methods, you'll see that -as usual- the replacement methods are mentioned. 如果你探索上述方法的当前Javadocs,你会看到 - 通常 - 提到了替换方法。 For example, Application#createValueBinding() mentions the following: 例如, Application#createValueBinding()提到以下内容:

Deprecated . 不推荐 This has been replaced by calling getExpressionFactory() then ExpressionFactory.createValueExpression(javax.el.ELContext, java.lang.String, java.lang.Class) . 这已经被调用getExpressionFactory()然后调用ExpressionFactory.createValueExpression(javax.el.ELContext, java.lang.String, java.lang.Class)取代。

You can find some concrete examples of dynamically populating a datatable this way in this article . 你可以找到的动态填充一个DataTable这种方式在一些具体的例子这篇文章

As to 3rd party component libraries, RichFaces has a rich:columns component which is designed for exactly this purpose. 对于第三方组件库,RichFaces有一个rich:columns组件,专门用于此目的。

第三方库PrimeFaces具有p:columns组件,用于从数据源动态生成列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM