Tuesday, July 10, 2007


Selecting a particular row in Datatable – JSF

Selecting a particular row in Datatable in a web-app which is developed on JSF,If the requirements is to display data in the datatable with check boxes in every row and user will select a particular row and then clicks the respective buttons to do the operation.

Html Page for Displaying DataTable

Sample:




<table align="center">
<tr>
<td>
<h:dataTable border="1" cellpadding="2" cellspacing="0" headerClass="headerClass" footerClass="footerClass"
rowClasses="rowClass1" styleClass="dataTable" value="#{beanname.data}" var="pagination" binding="#{beanname.datatable}">
<h:column>
<f:facet name="header">
<h:outputText value="Select"/>
</f:facet>
<h:selectBooleanCheckbox id="selectid" styleClass="selectBooleanCheckbox" binding="#{beanname.checked}">
</h:selectBooleanCheckbox>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Header Variable"/>
</f:facet>
<h:outputText value="#{pagination.headerid}"/>
<h:inputHidden id="hidden" value="#{pagination.variable2}" binding="#{beanname.variable2}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Header Variable1"/>
</f:facet>
<h:outputText value="#{pagination.variable2}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Header Variable2"/>
</f:facet>
<h:outputText value="#{pagination.variable3}"/>
</h:column>
</h:dataTable><BR>
<h:commandButton value="Opertion1" action="actionparam" id="Opertion1ID" >
<f:actionListener type="ListenerClassName" /></h:commandButton>
<h:commandButton value="View Opertion1" action="actionparam1" id="Opertion2ID" >
<f:actionListener type="com.anz.cpr.listener.TransactionHeaderListener" /></h:commandButton>
<h:commandButton value="Opertion1 TxHeader" action="actionparam2" id="Opertion3ID">
<f:actionListener type="ListenerClassName" />
</h:commandButton>
</td></tr>
</table>


Java Bean which holds the data (Backing Bean)


This bean is used by the above page to display the data
beanname.methodname

getData() method returns a list which contains the DB data

In the Bean create a method which will return a list which contains bean Object

Sample :




Public class SampleBean
{
private String variable;
private String variable1;
private String variable2;
private UISelectBoolean checked;
private UIData datatable;
public List tbllist;
/* Getter Setter Methods for variable1,2,3 and for other variables*/
public List getData()
{
/* Query DB and get the Resultloop through the result setsample i am using hibernateget Session for hibernate*/
List hquery=session.createQuery("from XYZ").list();
if (hquery != null && hquery.size() > 0)
{
tbllist=new ArrayList(hquery.size());
for (int i=0; i< hquery.size(); i++)
{
Tblxyz xyz=(Tblxyz)hquery.get(i);
SampleBean bean=new SampleBean();
/* get value from the db and set in the bean */
tbllist.add(bean);
}
}
}
}


ListenerBean


Since we are Implementing ActionListener we need implement the methods




public BeanListener implements ActionListener
{
public void processAction(ActionEvent event) throws AbortProcessingException
{
/* This will give the id name of the particular h-link which
* generated this action
* /
String sCommandName=event.getComponent().getId();
/*Getting the faces context*/
FacesContext context=FacesContext.getCurrentInstance();

/* Since we have three H-links we are cheking which one got triggred
* i.e operation1ID got triggred now i need that particular row data
*/
if(sCommandName.equals("Opertion1ID"))
{
ValueBinding valueBinding=Util.getValueBinding("#{beanname}");
SampleBean samplbean=(SampleBean)valueBinding.getValue(context);
/* since we have used binding the jsf we can get the entire datatable from the JSF page*/
UIData datatable=samplbean.getDatatable();
/* getting the totalRowCount */
int rowCount=datatable.getRowCount();
/* Looping through the datatable to check which row got selected*/
for (int index = 0; index < hvalue="samplbean.getVariable2().getValue().toString();">

0 comments: