2

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

Community
  • 1
  • 1
monkey-wrench
  • 1,556
  • 2
  • 14
  • 16

1 Answers1

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:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555