使用下列代码创建二行三列的单元格到表格中
Cell cell = new Cell("A cell", 2, 3);
table.setCell(cell, 1, 2); // Rows and columns start at 0
这时,表格的内部数据就如表4所示,对应的html效果见表5
t
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 = cell
def = false
visible = true |
cells = cell
def = false
visible = false |
cells = cell
def = false
visible = false |
cells = default
def = true
visible = true |
cells = default
def = true
visible = true |
cells = default
def = true
visible = true |
cells = cell
def = false
visible = false |
cells = cell
def = false
visible = false |
cells = cell
def = false
visible = false |
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 |
表 4. 四行六列表格的内部数据结构
表 5. 具有2行3列单元格的四行六列表格
合并表实例到velocity模版中
通过以上的代码,我们已经创建了一个简单数据结构的表格,表格中我们也定义了所需要的一些单元格元素。下面所需要做的就是合并这个表实例到标准的velocity模板中,这时候把table元素增加到VelocityContex元素中。
velocityContext.put("table", table);
一个处理所定义表格结构的简单模板如列表2所示
列 2. 处理表5结构的模版
<html>
<body>
<table border="1" cellspacing="0" cellpadding="0">
#foreach ( $row in [$table.row0..$table.rowEnd])
<tr>
#foreach ( $col in [$table.col0..$table.colEnd])
#if ( $table.isVisible($row, $col) )
#if ( $table.isDefaultCell($row, $col) )
<td> </td>
#else
#set ( $cell = $table.getCell($row, $col) )
<td rowspan="$cell.rowSpan"
colspan="$cell.colSpan"
align="center"> $cell.name
</td>
#end
#end
#end
#end
</table>
</body>
</html>
在列2中,值得注意的是 :
$table.isVisible()确认所有被其他单元格覆盖的格子将不会被显示
- The
$table.isDefaultCell() 检查处理默认的单元格(比如一些没有被显式被其他格子覆盖或被定义的单元格)。为了准确的显示表格,即使标签里没有包含任何内容,在这里仍然需要使用td。
- 使用预定义的rowspan和colspan去处理那些不是默认被添加的显式单元格。而单元格里的内容呢,我们简单的设置为单元格名字。当然,cell类也提供更复杂的显示数据功能去。(你会逐渐了解到)
让我们再次回到表2
列3显示了所有定义表2结构所需要的Java代码
Table demoTable = new Table(10, 20);
Cell box1 = new Cell("Box 1", 1, 5);
Cell box2 = new Cell("Box 2", 3, 3);
Cell box3 = new Cell("Box 3", 6, 3);
Cell box4 = new Cell("Box 4", 3, 5);
Cell box5 = new Cell("Box 5", 5, 4);
Cell box6 = new Cell("Box 6", 4, 2);
box1.setProperty("backgroundColor", "#33FF33");
box2.setProperty("backgroundColor", "#FF6666");
box3.setProperty("backgroundColor", "#FFFF00");
box4.setProperty("backgroundColor", "#6600CC");
box5.setProperty("backgroundColor", "#C0C0C0");
box6.setProperty("backgroundColor", "#FFCC33");
demoTable.setCell(box1, 0, 0);
demoTable.setCell(box2, 1, 6);
demoTable.setCell(box3, 4, 1);
demoTable.setCell(box4, 6, 8);
demoTable.setCell(box5, 0, 15);
demoTable.setCell(box6, 6, 16);
一旦我们定义好表结构,下面所需要做的当然是用模板来表现出我们所需要的效果。详细代码见列表4
列4. 表2对应的Velocity模板
<table width="50%" valign="middle" border="1"
cellpadding="2" cellspacing="2" style="empty-cells:show">
#foreach ( $row in [$demoTable.row0..$demoTable.rowEnd])
<tr>
#foreach ( $col in [$demoTable.col0..$demoTable.colEnd])
#if ( $demoTable.isVisible($row, $col) )
#if ( $demoTable.isDefaultCell($row, $col) )
<td> <br> </td>
#else
#set ( $cell = $demoTable.getCell($row, $col) )
<td rowspan="$cell.rowSpan"
colspan="$cell.colSpan"
align="center"
bgcolor="$cell.getProperty("backgroundColor")">
$cell.name
</td>
#end
#end
#end
#end
</table>
值得注意的是,我们在列3中显式的为每1个单元格实例定义了1个backgroundColor属性。在列4中,我们需要用到这个属性来设定单元格背景颜色。在这部分中,我们演示了怎样实现一些基本效果:怎样在模板中控制表格和单元格显示,怎样使用Velocity模版来控制Java代码中的单元格数据。
未完待续