Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

building a dataTable from java code

 
Camilo Diaz
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello:

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
Pie
Posts: 18277
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joe ONeil
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Solution
Create a binded panel grid

JSP/Facelet Page


<h:form id="dynTable" >
<t:panelGrid binding="#{multiPartSearch.panelGrid}" id="pgdt" />
</h:form>
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
.getCurrentInstance().getViewRoot().findComponent("dynTable:pgdt");

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
panelGrid.getChildren().clear();
.....

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.setBorder(0);
dataTable1.setCellpadding("1");
dataTable1.setVar(rowVar);
dataTable1.setHeaderClass("headerText");
dataTable1.setFooterClass("footerText");
dataTable1.setRowClasses("row1, row2");
HtmlColumn col1 = new HtmlColumn();
col1.setId("demCol1"+idcount++);
HtmlOutputText header1 = new HtmlOutputText();
header1.setValue("MPN");
header1.setId("demdtMpn" + rowVar);
HtmlCommandLink idLink = new HtmlCommandLink();
idLink.setStyleClass("highLightLink");
idLink.setId("demandlink" + rowVar); // Custom ID is required in
// dynamic
// UIInput
// and UICommand.
idLink.setValueExpression("value", createValueExpression("#{" + rowVar
+ ".part}", String.class));
idLink.setActionExpression(createActionExpression(dtValueExpression
+ ".selectDataModel}", String.class));
col1.getChildren().add(idLink);
col1.setHeader(header1);

dataTable1.getChildren().add(col1);

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"));
dataTable1.getChildren().add(
addColumn2(rowVar, "Demand Type", "demandTypeCode","dem5"));
dataTable1.getChildren().add(
addColumn2(rowVar, "Account Code", "accountCode","dem6"));
dataTable1.getChildren().add(
addColumn2(rowVar, "Account Classification",
"accountClassificationDesc","dem7a"));
dataTable1.getChildren().add(
addColumn2(rowVar, "Business Type", "businessTypeCode","dem7"));
dataTable1.getChildren().add(
addARColumn2(rowVar, "Target Price ($)", "targetPrice","dem8"));
dataTable1.getChildren().add(
addDateColumn2(rowVar, "Target Date", "strTargetDate","dem10"));
dataTable1.getChildren().add(
addDateColumn2(rowVar, "Update Date", "strUpdateDate","dem11"));
dataTable1.getChildren().add(
addColumn2(rowVar, "Trader", "salespersonName","dem12"));
// dataTable1.setRows(10);
Div div = new Div();
div.setId("demdiv"+idcount++);
// createScrollerDiv(div, "dem", rowVar);

dataTable1.setFooter(div);
HtmlOutputText blank = new HtmlOutputText();
blank.setValue(" ");
blank.setId("demhblnk"+rowVar);
dtPanelGrid.getChildren().add(blank);
dtPanelGrid.getChildren().add(dataTable1);
}



When dtPanelGrid is full you add it to the panelGrid on the JSP/facelet page
panelGrid.getChildren().add(dtPanelGrid);

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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic