关于h:dataTable的使用

关于h:dataTable的使用


基本用法



后台处理



可以在table的每一行增加操作用于处理当前行(比如删除当前行),也可以在table外增加操作处理整个table(更新整个table),页面代码如下:
我们可以有多种方式来处理
1】 传统的jsp方式,也是很多初学者首先想到的方式,这种方式本质上和以前的方式没有区别,只是披上了jsf的外衣而已,强烈建议不要使用。并且这样没有办法处理整个table,按以前的方式处理是相当的麻烦。
给每一行的操作增加参数,然后在后台提取该参数
<t:dataTable var="emp" .... >
<h:commandLink id="editLink" action="#{employeeAction.prepareEdit}">
<h:outputText value="#{msg.edit}"/>
<f:param name="id" value="#{emp.id}"/>
</h:commandLink>

FacesContext context = FacesContext.getCurrentInstance();
Map map = context.getExternalContext().getRequestParameterMap();
String employeeID = (String) map.get("id");
2】 使用actionListener,来取得当前行的数据。整个表的处理很简单,直接获取list即可(参加方法3)
<h:commandLink>
<f:actionListener type="net.java.OrderActionListener" />
<h:outputText value="Order" />
</h:commandLink>
public class orderActionListener implements ActionListener {

public void processAction(ActionEvent anEvent) throws AbortProcessingException {

YourBeanClass tmpBean = null;

UIComponent tmpComponent = anEvent.getComponent();

while (null != tmpComponent && !(tmpComponent instanceof UIData)) {
tmpComponent = tmpComponent.getParent();
}

if (tmpComponent != null && (tmpComponent instanceof UIData)) {
Object tmpRowData = ((UIData) tmpComponent).getRowData();
if (tmpRowData instanceof YourBeanClass) {
tmpBean = (YourBeanClass) tmpRowData;

//TODO Implementation of your method

}
}
3】 通过绑定tale,这样就可以在后台直接通过table来获取当前行了,而不像方法2那样通过事件来获得table
<h:dataTable binding="#{testSelectItems.table}" id="hotels" value="#{testSelectItems.list}" var="hot">
<h:column id="c1">
<f:facet name="header" id="f1">label</f:facet>
<h:inputText id="bbb" value="#{hot.label}" required="true"/>
</h:column>
<h:column id="c2">
<f:facet name="header" id="f2">value</f:facet>

<h:inputText id="aaa" value="#{hot.value}" required="true"/>
</h:column>
<h:column id="c3">
<f:facet name="header" id="f3">link</f:facet>
<!- 处理当前行
<h:commandLink id="ccc" action="#{testSelectItems.actionTest1}" value="#{hot.value}">
xxx#{hot.label}
</h:commandLink>
</h:column>
</h:dataTable>
<!- 处理全部行
<h:commandButton action="#{testSelectItems.actionTest}"/>
</h:form>
public class TestSelectItems {
private UIData table=new UIData();
private List list;

public List getList(){
System.out.println("---TestSelectItems-getList----1---");
if(list==null){
System.out.println("--TestSelectItems-getList-----2---");
list=new ArrayList();
SelectItem it=new SelectItem();
it.setLabel("lable1");
it.setValue("1");
list.add(it);
it=new SelectItem();
it.setLabel("lable2");
it.setValue("2");
list.add(it);
}
return list;
}
//处理全部行
public void actionTest(){
System.out.println("----list.size():"+list.size());
System.out.println("--actionTest--");
}
//处理单行
public void actionTest1(){
SelectItem dd=(SelectItem)table.getRowData();
System.out.println("--actionTest1--:"+dd.getLabel());
}
public UIData getTable() {
return table;
}
public void setTable(UIData table) {
this.table = table;
}


增加多选操作


参见http://yourenyouyu2008.iteye.com/blog/201138


实现原理

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值