在没有< h:dataTable/>的情况下渲染2D数组 [英] Rendering 2D array without <h:dataTable/>

查看:79
本文介绍了在没有< h:dataTable/>的情况下渲染2D数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将2D数组显示为页面中的表格.我知道这听起来像是我在重新发明轮子,但我必须处理的对象是自定义类型的2D数组,还必须以特定方式(始终相同)进行渲染.

I'm trying to show a 2D array as a table in a page. I know this sounds as if I was reinventing the wheel but the object I have to handle is a 2D array of a custom type that must also be rendered in a particular way (always the same).

棘手的部分是,此2D数组的某些索引中可以包含空值.在那些情况下,必须呈现特定的不可用"单元格.表结构(是... <table> <tr><td>)已经由设计团队定义并为客户所接受.

The tricky part is that this 2D array can have null values in some of its indexes. In those cases, a particular "unavailable" cell must be rendered. The table structure (yes... <table> <tr> and <td>) was already defined by the Design Team and accepted by the client.

我尝试使用<c:forEach/>,但是由于管理JSTL和JSF标记的顺序而使它无法正常工作时遇到了麻烦.处理JSF标记时,会出现一些问题,包括过时的值和缺少的组件.

I've tried to use <c:forEach/> but got in trouble trying to make it work, because of the order in which JSTL and JSF tags are managed. When the JSF tags are handled there are some issues that include outdated values and missing components.

调用<c:forEach/>时,数组(@ViewScoped bean的属性)始终为null,即使我强制创建数组:

The array (attribute of a @ViewScoped bean) is always null when <c:forEach/> is invoked, even when I force the creation of the array:

public MyObject[][] getMatrix() {
    if(loadedMatrix == null)
        initializeMatrix();
    return loadedMatrix.getTable();
}

initializeMatrix()方法从数据库中获取相应的数据,并调用创建2D数组的逻辑,使loadedMatrix在创建后引用它(所有这些工作,没有异常或错误). initializeMatrix完成后,loadedMatrix仍为null.

The initializeMatrix() method obtains the corresponding data from the database and invokes the logic that creates the 2D array, making loadedMatrix reference it once it's created (all of this works, no exceptions or errors). When initializeMatrix finishes, loadedMatrix is still null.

我选择了嵌套的<c:forEach/>选项,因为我需要管理表的索引以了解要呈现的内容(如果对象为null,将可用性标志设置为false或可以正常呈现),但就目前而言,我认为最安全的解决方案是创建自定义组件.

I went for the nested <c:forEach/> option because I need to manage the indexes of the table to know what to render (if the object is null, has an availability flag set to false or if it can be rendered normally), but as for now I think the safest solution is to create a custom component.

我的问题是:在知道要渲染的索引的同时,我还需要哪些其他方法将2D数组的内容渲染为表?

My question is: What alternatives do I have to render the content of a 2D array as a table while being aware of the indexes I'm rendering?.

预先感谢

推荐答案

您可以使用ui:repeat代替c:forEach,我尝试了一个示例,它很适合我. c:foreach是一个标记处理程序,请查看@BalusC的以下帖子,以了解有关为什么不应该将标记处理程序用于视野范围的bean的更多信息.

You can use ui:repeat instead of c:forEach, I tried myself with a sample and it worked for me. c:foreach is a tag-handler and please take a look at the following post of @BalusC to learn more about why you should not use tag handlers with view-scoped beans.

JSF2 Facelets中的JSTL ...有意义吗?

<table>
<ui:repeat value="#{sampleBean.twodarray}" var="firstLevel" varStatus=#{vs}>
    <tr>
    <ui:repeat value="#{firstLevel}" var="secondLevel" rendered="#{!empty  firstLevel}">
        <td>#{vs.index} - #{secondLevel}</td>
    </ui:repeat>
    <h:panelGroup rendered="#{empty firstLevel}">
        <td colspan="3">empty</td>
    </h:panelGroup>
    </tr>
</ui:repeat>
</table>

这篇关于在没有&lt; h:dataTable/&gt;的情况下渲染2D数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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