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