Saturday, March 31, 2012

How to programmatically populate af:table

Hi ,

It is same as mine last post.it is very easy to create the af:table through  ADF-BC.If you want to learn more about af:table then see the link http://jdevadf.oracle.com/adf-richclient-demo/docs/tagdoc/af_table.html for more information.

af:table has one  attribute value which  will accept the List or Collection Model value  to render the table.so i have just create a backing bean and where i have created the List of Example object which will render in UI as table.whatever attribute is present in  Example class it will render as a individual column.


package com.prateek.oracle.adf;

import java.util.ArrayList;
import java.util.List;


public class BackingBean {
private List exmapleObjectList;

{
exmapleObjectList = new ArrayList();
ExmapleObject ex1 = new ExmapleObject();
ex1.setId("1");
ex1.setEFirstName("prateek1");
ex1.setELastName("Shaw1");
ex1.setEAddress("hyderabad1");
ExmapleObject ex2 = new ExmapleObject();
ex2.setId("2");
ex2.setEFirstName("prateek2");
ex2.setELastName("Shaw2");
ex2.setEAddress("hyderabad2");
ExmapleObject ex3 = new ExmapleObject();
ex3.setId("3");
ex3.setEFirstName("prateek3");
ex3.setELastName("Shaw3");
ex3.setEAddress("hyderabad3");
ExmapleObject ex4 = new ExmapleObject();
ex4.setId("4");
ex4.setEFirstName("prateek4");
ex4.setELastName("Shaw4");
ex4.setEAddress("hyderabad4");
exmapleObjectList.add(ex1);
exmapleObjectList.add(ex2);
exmapleObjectList.add(ex3);
exmapleObjectList.add(ex4);

}


public void setExmapleObjectList(List exmapleObjectList) {
this.exmapleObjectList = exmapleObjectList;
}

public List getExmapleObjectList() {
return exmapleObjectList;
}
}


open the following the link to download sample project

http://www.4shared.com/rar/WVdPSfSh/AdfTable.html

Thanks
Prateek

4 comments:

  1. Hi Prateek,
    how the row selection can be achieved and how can I get the selected row in the backing bean?

    Thanks,
    Priya.

    ReplyDelete
    Replies
    1. You can get the selected row several way ...
      First way you can get the selected row in listener method of af:table.
      First add these two property on the table.
      1-rowSelection="single"
      2-selectionListener="#{pageFlowScope.backingBean.tableSelection}"

      Selection listener (tableSelection )method should be present in the backing bean.

      Method should have following code :

      public void tableSelection(SelectionEvent selectionEvent) {
      RichTable richTable = (RichTable)selectionEvent.getSource();
      ExmapleObject exmapleObject = (ExmapleObject)richTable.getSelectedRowData();
      System.out.println(exmapleObject.getId()+exmapleObject.getEAddress()+exmapleObject.getEFirstName()+exmapleObject.getELastName());
      }

      Second way in any command action method:
      First bind af:table in the backing like

      binding="#{pageFlowScope.backingBean.tableBinding}"

      And in the command button action you can use following code to get the selected row

      public void button(ActionEvent actionEvent) {
      System.out.println(((ExmapleObject)tableBinding.getSelectedRowData()).getEFirstName());
      }

      *where tableBinding is binding name of the table.

      I hope it would be help full.

      Delete
  2. But tables created by arraylist could not be filtered in my case.

    ReplyDelete