洗洗就睡了
催眠曲进行中
农妇,山泉,有点田
我的首页
文章
相册
圈子
留言
管理
 
    当前所在页面:首页>>文章>>让复杂的html表格在Apache Velocity中变得简单[译](2)
让复杂的html表格在Apache Velocity中变得简单[译](2)
    作者: 来源:http://www.javaworld.com/javaworld/jw-01-2008/jw-01-htmltables.html?page=2 发表时间:2008-01-09

 
 

坦白的说,相对于我处理过的项目需求,table1只能算是比较简单的实例。我们来看看另一个例子(Table 2).

Box 1                     Box 5  
            Box 2              
                         
                         
  Box 3                        
                                 
          Box 4       Box 6    
                   
                   
                             

表2. 复杂表格

我将演示怎样使用简单的几行java和velocity模版代码来实现table2的效果

这个解决方案主要在java模型层中使用table和cell类来生成html表格信息。

首先创建一个table实例,比如

int   rowNumber = 10;
int   colNumber = 5;
Table table     = new Table(rowNumber, colNumber);

然后,你需要创建单元表格对象,并加入到表格中。setcell方法是增加单元格的核心方法。这行代码增加一个宽1高1的单元格到表格中的第三行第二列。 

Cell cell = new Cell("A first cell");
table.setCell(cell, 3, 2);

当你创建一个多行列的单元格的时候,你会觉得非常有趣。下面代码增加了一个3行4列的单元格。  

Cell cell = new Cell("A more complex cell", 3, 4);
table.setCell(cell, 3, 2);

为了查看单元格怎样反映到表格类的内部结构中,下面我们进入表格的内部来查看其中的数据。                  

private Cell[][]    cells   = null;
private boolean[][] visible = null;
private boolean[][] def     = null;

一旦行和列数被确定,上面的二维数据将使用一个包含表格中所有单元格的元素创建,布尔型对象visible、def将被初使化为true。而每一个单元格数组初始化为默认单元格                  

private static Cell defaultCell = new Cell("", 1, 1);

如果一个单元格实例生成多行列到表格中,它将保存在单元格数组的相应位置。这个Cell实例覆盖在其他临近的其他单元格数组元素之上,所以那些单元格的visible值需要设置为false,从而实现隐藏。一个新增加的单元实例引用也将被存储在隐藏的单元格数组合元素中。你能使用这些信息去检测这样的冲突:更多新增加的单元格会覆盖老格之上。(html不能很好的显示重叠的表格单元,所以我们必须在表类中检测到这样的冲突)

The initial setup for a table with four rows and six columns is:

初始设置4行6列的表格

Table table = new Table(4, 6);

表三列出了表格的内部数据结构

cells   = default
def     = true
visible = true
cells   = default
def     = true
visible = true
cells   = default
def     = true
visible = true
cells   = default
def     = true
visible = true
cells   = default
def     = true
visible = true
cells   = default
def     = true
visible = true
cells   = default
def     = true
visible = true
cells   = default
def     = true
visible = true
cells   = default
def     = true
visible = true
cells   = default
def     = true
visible = true
cells   = default
def     = true
visible = true
cells   = default
def     = true
visible = true
cells   = default
def     = true
visible = true
cells   = default
def     = true
visible = true
cells   = default
def     = true
visible = true
cells   = default
def     = true
visible = true
cells   = default
def     = true
visible = true
cells   = default
def     = true
visible = true
cells   = default
def     = true
visible = true
cells   = default
def     = true
visible = true
cells   = default
def     = true
visible = true
cells   = default
def     = true
visible = true
cells   = default
def     = true
visible = true
cells   = default
def     = true
visible = true

表3 四行6列的表格数据结构

 
 

(阅读 )   评论数(:1)
评论】 【收藏】
评论:共1条
这样就方便多了,呵呵! 你要放到一篇文章里撒,看完第一篇我还以为嫩个麻烦!
评论人: 天一     评论时间: 2008-01-15 13:53:05

发表评论:
发表人:
评论: 
    
 
关于我们 | 诚聘英才 | 联系我们 | 广告业务 | 网站地图 | 法律声明

EasyJF开源团队版权所有  建议使用1024*768分辨率