JSF 2.0 Dynamically Remove Components

不打扰是莪最后的温柔 提交于 2019-12-09 01:22:53

问题


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


回答1:


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:

  • Benefits and pitfalls of @ViewScoped - contains JSF 2.0 CRUD table example


来源:https://stackoverflow.com/questions/4973384/jsf-2-0-dynamically-remove-components

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!