I have seen this question more than once now, how do you add/delete a row in a table? Turns out it’s pretty simple. Although you can’t do partial table update which RichFaces supports, when adding or deleting a row, jut render the entire table. This will also work with a standard JSF table. Also, a related post is How to delete a row in JSF. RichFaces 3.3.3 is used.
The page looks like this:
JSF page:
Managed bean:
@KeepAlive public class Bean { private List list; @PostConstruct public void init() { list = new ArrayList(); list.add(1); list.add(2); list.add(3); } public void remove() { // remove last if (!list.isEmpty()) { list.remove(list.size() - 1); } } public List getList() { return list; } public void add() { list.add(list.size()+1); } }
The only thing to mention is that @KeepAlive gives us view scope. This way we can keep the same bean between requests. More on view scope in RichFaces.
JSF configuration file:
bean org.richfaces.example.Bean request
That’s it.
Leave a Reply