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來實現。