DataTable与动态列 [英] DataTable with dynamic columns

查看:260
本文介绍了DataTable与动态列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对JSF是全新的,只是尝试一个概念证明来决定它对项目是否有用。我的POC只包含一个页面,其中包含一些表格,包含一些数据。



列数(以及行数)是动态的,从在页面呈现之前的数据库。



如下所示,我会得到两个静态列,其中包含适当的行数,如我所料:

 < h:dataTable id =datavalue =#{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>

我想做的是添加一个< c:forEach。 ..> 围绕一组< h:column> ...< / h:column> 标签,迭代列数,但没有工作 - 我尝试了各种组合,但我期待着这样的工作:

 < h:dataTable id =datavalue =#{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>

从Google搜索,我读过各种含糊不清的评论,如这不是这样做但我没有找到任何特别引人注目的正确方法的例子。有人提到在支持bean中构建DataTable,但是我可以找到的唯一的例子是 http ://forums.sun.com/thread.jspa?threadID = 577589 。它的工作,但感到有点笨拙,特别是因为一些使用的方法已被弃用。



目前看来,我看不太可能使用任何除核心JSF之外的库,但是如果绝对必要的话可能会改变。任何人都可以通过正确的方法来解决这个问题吗?看起来应该很简单,所以我确定我只是缺少一些明显的东西。

解决方案

JSTL和JSF不像您期望的编码一样运行。更重要的是,JSTL首先从上到下处理整个页面,然后将结果交给JSF进行进一步处理。在JSTL忙时,#{row} 不可用,所以您的尝试确实不会奏效。



链接的主题确实提到了不赞成的方法,但主题也是老旧的,并讨论了传统的JSF 1.0 / 1.1。如果您探索上述方法的当前Javadoc,您将看到 - 通常 - 提及替换方法。例如, 应用程序#createValueBinding() 提到以下内容:


已弃用。这已被替换为调用 getExpressionFactory() 然后 ExpressionFactory.createValueExpression(javax.el.E​​LContext,java.lang。 String,java.lang.Class)


你可以找到在这篇文章中动态填充数据表的一些具体示例。



对于第三方组件库,RichFaces有一个 rich:columns 组件,专为此设计目的。


I am completely new to JSF, and just attempting a proof of concept to decide whether it will be useful for a project. My POC simply consists of a single page, with a table, containing some data.

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: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. 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. 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. 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. The #{row} is unavailable at the time JSTL is busy, so your attempt indeed won't work.

The linked topic indeed mentions deprecated methods, but the topic is also aged and discusses legacy 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. For example, Application#createValueBinding() mentions the following:

Deprecated. This has been replaced by calling getExpressionFactory() then 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.

As to 3rd party component libraries, RichFaces has a rich:columns component which is designed for exactly this purpose.

这篇关于DataTable与动态列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆