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

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

问题描述

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

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?

如何动态添加 JSF 组件

推荐答案

基于 other question 你链接了,你需要做如下修改:

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中实例化这个)

    改为在数据表中使用它.

    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());
    }
    

  • 另见:

    • @ViewScoped 的好处和缺陷 - 包含 JSF 2.0 CRUD 表示例
    • 这篇关于JSF 2.0 动态删除组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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