JSF 2.0动态移除组件 [英] JSF 2.0 Dynamically Remove Components

查看:168
本文介绍了JSF 2.0动态移除组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为关于在JSF 2.0中动态添加组件的回答问题(请参阅下面的链接),我喜欢使用dataTable的方法,但是如何删除其中一个添加的组件?



如何动态添加JSF组件根据其他问题,您需要进行以下更改:


     < h:column>< h :commandButton value =deleteaction =#{bean.delete}/>< / h:column> 


  1. 添加 DataModel< Item> 属性到bean并包装

      private DataModel< Item> ; model = new ListDataModel< Item>(items); 

    (不要忘记getter,请注意你也可以在bean构造函数中实例化或postconstruct)


  2. 在datatable中使用。

     < h:dataTable value =#{bean.model}var =item> 


  3. 向bean添加删除方法。

      public void delete(){
    items.remove(model.getRowData());
    }




另请参见: / h3>


As a follow on to an answered question on Adding Components Dynamically in JSF 2.0 (see link below), I like the approach of using a dataTable, but what about Removing one of the added components?

How to dynamically add JSF components

解决方案

Based on the code snippet in the other question you linked, you need to do the following changes:

  1. Add a column with a delete button to the table.

    <h:column><h:commandButton value="delete" action="#{bean.delete}" /></h:column>
    

  2. Add a DataModel<Item> property to the bean and wrap the list of items in it so that you will be able to obtain the table row where the button was clicked.

    private DataModel<Item> model = new ListDataModel<Item>(items);
    

    (don't forget the getter, note that you can also instantiate this in bean constructor or postconstruct)

  3. Use this in the datatable instead.

    <h:dataTable value="#{bean.model}" var="item">
    

  4. Add a delete method to the bean.

    public void delete() {
        items.remove(model.getRowData());
    }
    

See also:

这篇关于JSF 2.0动态移除组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆