h:dataTable标签用来生成表格,感觉就和struts的logic:iterator差不多,下面是一般的用法:
1
<
h:dataTable
value
='#{items}'
var
='item'>
2
3
<h:column
>
4
5
<
%--
left column components --%
>
6
7
<
h:outputText
value
='#{item.propertyName}'/>
8
9
</h:column
>
10
11
12
13
<
h:column
>
14
15
<
%--
next column components --%
>
16
17
<
h:outputText
value
='#{item.anotherPropertyName}'/>
18
19
</h:column
>
20
21
22
23
<
%--
add more columns, as desired --%
>
24
25
</
h:dataTable
>
26
27
其中这个items可以是一个集合(其类型可以是array、java.util.List、java.sql.ResultSet、javax.servlet.jsp.jstl.sql.Result、javax.faces.model.DataModel,循环遍历所有的item),或者是其他任何非集合的元素(循环一次)。
注意1)h:dataTable里面只能包含h:column。2)在h:dataTable这种能render子元素的标签中,要显示原始文本的话,要用<f:verbatim></f:verbatim>将文本包含起来(或者用h:outputText)。3) <f:facet name="header">和<f:facet name="footer">可以制定表头和表尾。
h:dataTable有以下这些属性:
bgcolor
Background color for the table
border
Width of the table's border
cellpadding
Padding around table cells
cellspacing
Spacing between table cells
columnClasses
Comma-separated list of CSS classes for columns
first
Index of the first row shown in the table
footerClass
CSS class for the table footer
frame
Specification for sides of the frame surrounding the table should be drawn; valid values: none, above, below, hsides, vsides, lhs, rhs, box, border
headerClass
CSS class for the table header
rowClasses
Comma-separated list of CSS classes for columns
rules
Specification for lines drawn between cells; valid values: groups, rows, columns, all
summary
Summary of the table's purpose and structure used for non-visual feedback such as speech
var
The name of the variable created by the data table that represents the current item in the value
binding, id, rendered, styleClass, value
Basic attributes
dir, lang, style, title, width
HTML 4.0
onclick, ondblclick, onkeydown, onkeypress, onkeyup, onmousedown, onmousemove, onmouseout, onmouseover, onmouseup
DHTML events
有一个很有意思的例子,它可以生成一个动态可编辑的表格:
1
<
h:dataTable
value
="#{tableData.names}"
var
="name"
>
2
3
<
%--
checkbox column --%
>
4
5
<
h:column
>
6
7
<
f:facet
name
="header"
>
8
9
<
h:outputText
value
="#{msgs.editColumn}"
10
11
style
="font-weight: bold"
/>
12
13
</
f:facet
>
14
15
16
17
<
h:selectBooleanCheckbox
value
="#{name.editable}"
18
19
onclick
="submit()"
/>
20
21
</
h:column
>
22
23
24
25
<
%--
last name column --%
>
26
27
<
h:column
>
28
29
30
31
<
h:inputText
value
='#{name.last}'
32
33
rendered
='#{name.editable}'
34
35
size
='10'/>
36
37
38
39
<h:outputText value
='#{name.last}'
40
41
rendered
='#{not
name.editable}'
/>
42
43
</
h:column
>
44
45
46
47
</
h:dataTable
>
48
49
<
p
>
50
51
<
h:commandButton
value
="#{msgs.saveChangesButtonText}"
/>
52
53
事实上,h:dataTable不是直接操作集合对象的,而是通过Table Models来实现的。所以我们可以通过getWrappedData()和setWrappedData()来实现对modle中对象的操作,比如下面的一个方法:
1
public
String deleteNames()
{
2
3
if
(
!
getAnyNamesMarkedForDeletion())
4
5
return
null
;
6
7
8
9
Name[] currentNames
=
(Name[]) model.getWrappedData();
10
11
Name[] newNames
=
new
Name[currentNames.length
-
12
13
getNumberOfNamesMarkedForDeletion()];
14
15
16
17
for
(
int
i
=
0
, j
=
0
; i
<
currentNames.length;
++
i)
{
18
19
Name name
=
(Name) currentNames[i];
20
21
if
(
!
name.isMarkedForDeletion())
{
22
23
newNames[j
++
]
=
name;
24
25
}
26
27
}
28
29
model.setWrappedData(newNames);
30
31
return
null
;
32
33
}
34
35
}
36
37
同时如果我们要对集合数据进行排序和过滤的话,我们必须通过继承一种table model来实现。