Thursday, February 08, 2007

JSF: Binding dataTable from managed bean

This is a very easy method to retrieve row data of the data table in jsp page, from managed bean.
<h:dataTable var="item" value="#{MyBean.items}"
binding="#{MyBean.dataTable}" >
<h:column>
<h:outputText styleClass="output" value="#{item.productName}"/>
</h:column>

<h:column>
<h:commandButton value="remove" action="#{MyBean.remove}" />
</h:column>
</h:dataTable>
.
public class MyBean {
   private ArrayList items = new ArrayList();
   private HtmlDataTable dataTable;

   public ArrayList getItems() {
      return items;
   }

   public void setItems(List items) {
      this.items = new ArrayList(items);
   }

   public void remove(){
      ItemBean item = (ItemBean) getDataTable().getRowData();
      items.remove(item);
   }

   public HtmlDataTable getDataTable() {
      return dataTable;
   }

   public void setDataTable(HtmlDataTable dataTable){
      this.dataTable = dataTable;
   }

}
here, when user clicks the remove button, the related item is removed
from the data table.

7 comments:

  1. I got stuck with such scenario.
    Can u please provide explanation for this example.

    Thank You.

    ReplyDelete
  2. Excellent post! Thank you very much!

    ReplyDelete
  3. sure, nice post and nice lookin!

    ReplyDelete
  4. Excellent...!!
    helped me a lot... thanks...

    ReplyDelete
  5. Can't you people for once make a step by step example for us beginners?!!! That would also be comprehesive?!!! arggg

    ReplyDelete
  6. i would like to know about the ItemBean class used in the remove methode in this line
    ItemBean item = (ItemBean) getDataTable().getRowData();

    i didnt find any doc about that class i would like to know more about it !

    ReplyDelete
  7. ItemBean is the referenced type in dataTable, since getRowData() return Object, you need cast that guy :-)

    ReplyDelete