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

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

问题描述

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

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?

JSF2,我可以动态添加JSF组件吗? / a>

JSF2, can I add JSF components dynamically?

推荐答案

根据其他问题,您需要进行以下更改:

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


  1. 在列表中添加一个带有删除按钮的列。

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

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


  • 添加 DataModel< Item> 属性到bean并包装列表的项目,以便您可以获取点击按钮的表格行。

  • 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);
    

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

    在datatable中使用。

    Use this in the datatable instead.

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


  • 向bean添加删除方法。

  • Add a delete method to the bean.

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




  • 另请参见: / h3>


    • @ViewScoped 的优点和缺陷 - 包含JSF 2.0 CRUD表示例

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

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