This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JSF and the fly likes building a dataTable from java code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "building a dataTable from java code" Watch "building a dataTable from java code" New topic

building a dataTable from java code

Camilo Diaz

Joined: Sep 03, 2009
Posts: 19

i was wondering how to building a dataTable from my bean ?

i know i should bind the tag h:datatable in my jsf file to a HtmlDataTable instance in my backing bean, but, in not sure how to populate it from the java code,

I'll appreciate the help
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15950

Take a look at the HtmlDataTable javadocs. You should find a fairly straightforward mapping from the tag properties we all know and love to properties in the HtmlDataTable object.

It's often easiest to bind a dataTable tag on the view definition to a method on a backing bean and let the backing bean customize the dataTable rather than building up the whole thing from scratch.

Customer surveys are for companies who didn't pay proper attention to begin with.
Joe ONeil

Joined: Jan 16, 2009
Posts: 12
Building Dynamic Data table
The problem I had to solve was to be able to search multiple parts
over multiple sources one method of doing it was to create a static number of tables but that would be limiting
Create a binded panel grid

JSP/Facelet Page

<h:form id="dynTable" >
<t:panelGrid binding="#{multiPartSearch.panelGrid}" id="pgdt" />
Java Code
public class MultiPartSearch extends ConObject {
private HtmlPanelGrid panelGrid;


public HtmlPanelGrid getPanelGrid() {
return panelGrid;

public void setPanelGrid(HtmlPanelGrid panelGrid) {
this.panelGrid = panelGrid;
Binding the grid alone is not enough you also have to Initialize it in the class constructor

panelGrid=(HtmlPanelGrid) fc

I have a search method that creates the Datamodels After getthing the datamodels
You clear the panelGrid otherwise each time you hit search you will append to the panelGrid

public void searchRecords() {
//Clear children on each search

For Part number A create a panelGrid

HtmlPanelGrid dtPanelGrid = new HtmlPanelGrid();

Pass the dtPanelGrid into the createDemandTable method
After the table is created add it to dtPanelGrid
You can add as many tables as memory will allow to the dtPanelgrid

/*This Method creates a Data table inside of a panel grid

public void createDemandTable(HtmlPanelGrid dtPanelGrid,
String dtValueExpression, DataModel datamodel, String rowVar) {

HtmlDataTable dataTable1 = new HtmlDataTable();
dataTable1.setValueExpression("value", createValueExpression(
dtValueExpression + ".dataModel}", DataModel.class));
dataTable1.setId("demdt" + rowVar);
// dataTable1.setRenderedIfEmpty(false);
dataTable1.setRowClasses("row1, row2");
HtmlColumn col1 = new HtmlColumn();
HtmlOutputText header1 = new HtmlOutputText();
header1.setId("demdtMpn" + rowVar);
HtmlCommandLink idLink = new HtmlCommandLink();
idLink.setId("demandlink" + rowVar); // Custom ID is required in
// dynamic
// UIInput
// and UICommand.
idLink.setValueExpression("value", createValueExpression("#{" + rowVar
+ ".part}", String.class));
+ ".selectDataModel}", String.class));


dataTable1.getChildren().add(addColumn2(rowVar, "IPN", "internalPart","dem1"));

dataTable1.getChildren().add(addColumn2(rowVar, "MFR", "mfgCode","dem2"));
dataTable1.getChildren().add(addColumn2(rowVar, "D/C", "dateCode","dem3"));
dataTable1.getChildren().add(addARColumn2(rowVar, "Qty", "requestQty","dem4"));
addColumn2(rowVar, "Demand Type", "demandTypeCode","dem5"));
addColumn2(rowVar, "Account Code", "accountCode","dem6"));
addColumn2(rowVar, "Account Classification",
addColumn2(rowVar, "Business Type", "businessTypeCode","dem7"));
addARColumn2(rowVar, "Target Price ($)", "targetPrice","dem8"));
addDateColumn2(rowVar, "Target Date", "strTargetDate","dem10"));
addDateColumn2(rowVar, "Update Date", "strUpdateDate","dem11"));
addColumn2(rowVar, "Trader", "salespersonName","dem12"));
// dataTable1.setRows(10);
Div div = new Div();
// createScrollerDiv(div, "dem", rowVar);

HtmlOutputText blank = new HtmlOutputText();
blank.setValue(" ");

When dtPanelGrid is full you add it to the panelGrid on the JSP/facelet page

This can be done in a while loop and repeated over and over as memory allows

Problem with these data tables is duplicate IDS so it is best to assign a id to each component
Originally I put each set of parts in a collapsible panel but going to a detail page and back to the search page resulted in duplicate IDs
putting it in a Panelgrid and assigning ids seems to have eliminated the problem
subject: building a dataTable from java code
Similar Threads
selectmanycheckbox with selectitem
Create Datatable inside ManageBean
how to retrieve a particular value from datatable
Iteration in JSF, how ?
Which Package to Import to Use FacesContext's getRequestMap()?