本实验来自一个客户的真实需求。
由于种种原因,Table的列在设计时无法确定,因此只能在运行时通过程序动态创建Column。
案例1:Table的前两列固定不变, 其后的列不确定。
(1)页面代码
<af:table var="row" rowBandingInterval="0" id="t1" value="#{viewScope.useCase1MB.rowsData}" varStatus="vs" binding="#{viewScope.useCase1MB.useCase1Table}"> <af:column sortable="true" sortProperty="#{row.id}" headerText="id" id="c1"> <af:outputText value="#{row.id}" id="ot1"/> </af:column> <af:column sortable="true" sortProperty="#{row.name}" headerText="Name" id="c2"> <af:outputText value="#{row.name}" id="ot2"/> </af:column> <af:forEach begin="0" end="#{viewScope.useCase1MB.numberOfColsToAdd}" varStatus="index"> <af:column sortable="false" headerText="#{viewScope.useCase1MB.columnsHeader[index.current]}" id="c4"> <af:outputText value="#{row.columnsData[index.current].cellValue}" id="ot4"/> <f:facet name="header"> <af:outputText value="#{viewScope.useCase1MB.columnsHeader[index.current]}" id="ot3"/> </f:facet> </af:column> </af:forEach> </af:table>
(2)Managed Bean代码
package view; import java.util.ArrayList; import java.util.List; import model.type.usecase1.ColumnData; import model.type.usecase1.RowData; import oracle.adf.model.BindingContext; import oracle.adf.model.OperationBinding; import oracle.adf.model.binding.DCBindingContainer; import oracle.adf.view.rich.component.rich.data.RichTable; import oracle.binding.BindingContainer; public class UseCase1ManagedBean { private RichTable useCase1Table; private ListrowsData = new ArrayList(); private int numberOfColsToAdd = 0; private List columnsHeader = new ArrayList(); public UseCase1ManagedBean() { super(); initData(); } public BindingContainer getBindings() { return BindingContext.getCurrent().getCurrentBindingsEntry(); } public void initData() { BindingContainer bindings = getBindings(); oracle.binding.OperationBinding operationBinding = bindings.getOperationBinding("populateTableData"); rowsData = (List )operationBinding.execute(); for (int i = 0; i < rowsData.get(0).getColumnsData().size(); i++) { columnsHeader.add(rowsData.get(0).getColumnsData().get(i).getHeaderName()); } if (rowsData.get(0).getColumnsData().size() > 0) { setNumberOfColsToAdd(rowsData.get(0).getColumnsData().size() - 1); } // useCase1Table.setColumnResizing("disabled"); } public void setRowsData(List rowsData) { this.rowsData = rowsData; } public List getRowsData() { return rowsData; } public void setNumberOfColsToAdd(int numberOfColsToAdd) { this.numberOfColsToAdd = numberOfColsToAdd; } public int getNumberOfColsToAdd() { return numberOfColsToAdd; } public void setColumnsHeader(List columnsName) { this.columnsHeader = columnsName; } public List getColumnsHeader() { return columnsHeader; } public void setUseCase1Table(RichTable useCase1Table) { this.useCase1Table = useCase1Table; } public RichTable getUseCase1Table() { return useCase1Table; } }
(3)运行效果:
初始化Steve的数据,显示Steve's View:
初始化Mark的数据,显示Mark's View:
初始化Kim的数据,显示Kim's View:
案例2:每个Tab中有不确定的Table,每个Table中的列不确定。
(1)页面代码
<af:panelTabbed id="pt1" binding="#{viewScope.useCase2MB.panelTabbed}"/>
(2)Managed Bean代码
package view; import java.util.ArrayList; import java.util.List; import javax.el.ELContext; import javax.el.ExpressionFactory; import javax.el.ValueExpression; import javax.faces.application.Application; import javax.faces.component.UIComponent; import javax.faces.component.UIViewRoot; import javax.faces.context.FacesContext; import javax.faces.event.PhaseEvent; import javax.faces.event.PhaseId; import model.type.usecase2.ColumnData; import model.type.usecase2.RowData; import model.type.usecase2.TableData; import model.type.usecase2.TabData; import oracle.adf.model.BindingContext; import oracle.adf.view.rich.component.rich.RichForm; import oracle.adf.view.rich.component.rich.data.RichColumn; import oracle.adf.view.rich.component.rich.data.RichTable; import oracle.adf.view.rich.component.rich.input.RichChooseDate; import oracle.adf.view.rich.component.rich.input.RichInputDate; import oracle.adf.view.rich.component.rich.input.RichInputListOfValues; import oracle.adf.view.rich.component.rich.input.RichInputText; import oracle.adf.view.rich.component.rich.layout.RichPanelTabbed; import oracle.adf.view.rich.component.rich.layout.RichShowDetailItem; import oracle.adf.view.rich.component.rich.output.RichOutputText; import oracle.adf.view.rich.context.AdfFacesContext; import oracle.binding.BindingContainer; import view.util.JSFUtils; public class UseCase2ManagedBean { private RichTable useCase3Table; private ListtabsData = new ArrayList(); private RichPanelTabbed panelTabbed; public UseCase2ManagedBean() { super(); } public BindingContainer getBindings() { return BindingContext.getCurrent().getCurrentBindingsEntry(); } public void beforeRenderResponse(PhaseEvent phaseEvent) { if (phaseEvent.getPhaseId() == PhaseId.RENDER_RESPONSE) { FacesContext fctx = FacesContext.getCurrentInstance(); AdfFacesContext adfFacesContext = AdfFacesContext.getCurrentInstance(); boolean isInitialRender = adfFacesContext.isInitialRender(); if (isInitialRender) { initTabsData(); initTabsUI(); } } } public void initTabsUI() { List panelTabbedChildren = panelTabbed.getChildren(); for (int i = 0; i < tabsData.size(); i++) { RichShowDetailItem showDetailItem = new RichShowDetailItem(); showDetailItem.setId("sdi" + (i + 1)); showDetailItem.setText(tabsData.get(i).getTabName()); List tablesData = tabsData.get(i).getTablesData(); for (int j = 0; j < tablesData.size(); j++) { RichTable table = new RichTable(); table.setId("t_" + (i + 1) + "_" + (j + 1)); table.setVar("row"); List rowsData = tablesData.get(j).getRowsData(); table.setValue(rowsData); for (int k = 0; k < rowsData.get(0).getColumnsData().size(); k++) { RichColumn column = new RichColumn(); column.setId("col_" + (i + 1) + "_" + (j + 1) + "_" + (k + 1)); column.setSortable(false); column.setHeaderText(rowsData.get(0).getColumnsData().get(k).getHeaderName()); RichOutputText outputText = new RichOutputText(); outputText.setId("ot" + (i + 1) + "_" + (j + 1) + "_" + (k + 1)); FacesContext fctx = FacesContext.getCurrentInstance(); ELContext elctx = fctx.getELContext(); Application jsfApp = fctx.getApplication(); ExpressionFactory exprFactory = jsfApp.getExpressionFactory(); ValueExpression valueExpr = exprFactory.createValueExpression(elctx, "#{row.columnsData[" + k + "].cellValue}", Object.class); outputText.setValueExpression("value", valueExpr); column.getChildren().add(outputText); table.getChildren().add(column); } RichOutputText outputText = new RichOutputText(); outputText.setId("ot" + (i + 1) + "_" + (j + 1) ); outputText.setValue(tablesData.get(j).getTableName()); showDetailItem.getChildren().add(outputText); showDetailItem.getChildren().add(table); } panelTabbedChildren.add(showDetailItem); } } public void initTabsData() { BindingContainer bindings = getBindings(); oracle.binding.OperationBinding operationBinding = bindings.getOperationBinding("populateTabsData"); tabsData = (List )operationBinding.execute(); } public void setUseCase3Table(RichTable useCase3Table) { this.useCase3Table = useCase3Table; } public RichTable getUseCase3Table() { return useCase3Table; } public void setPanelTabbed(RichPanelTabbed panelTabbed) { this.panelTabbed = panelTabbed; } public RichPanelTabbed getPanelTabbed() { return panelTabbed; } }
(3)运行效果
案例3:Table的列不确定,且每个列的显示组件不同。
(1)页面代码
<af:table var="row" rowBandingInterval="0" id="t1" value="#{viewScope.useCase3MB.rowsData}" varStatus="vs"
binding="#{viewScope.useCase3MB.useCase3Table}"/>
(2)Managed Bean代码
package view; import java.util.ArrayList; import java.util.List; import javax.el.ELContext; import javax.el.ExpressionFactory; import javax.el.ValueExpression; import javax.faces.application.Application; import javax.faces.component.UIComponent; import javax.faces.component.UIViewRoot; import javax.faces.context.FacesContext; import javax.faces.event.PhaseEvent; import javax.faces.event.PhaseId; import model.type.usecase3.ColumnData; import model.type.usecase3.RowData; import oracle.adf.model.BindingContext; import oracle.adf.view.rich.component.rich.RichForm; import oracle.adf.view.rich.component.rich.data.RichColumn; import oracle.adf.view.rich.component.rich.data.RichTable; import oracle.adf.view.rich.component.rich.input.RichChooseDate; import oracle.adf.view.rich.component.rich.input.RichInputDate; import oracle.adf.view.rich.component.rich.input.RichInputListOfValues; import oracle.adf.view.rich.component.rich.input.RichInputText; import oracle.adf.view.rich.component.rich.output.RichOutputText; import oracle.adf.view.rich.context.AdfFacesContext; import oracle.binding.BindingContainer; import view.util.JSFUtils; public class UseCase3ManagedBean { private RichTable useCase3Table; private ListrowsData = new ArrayList(); public UseCase3ManagedBean() { super(); } public BindingContainer getBindings() { return BindingContext.getCurrent().getCurrentBindingsEntry(); } public void beforeRenderResponse(PhaseEvent phaseEvent) { if (phaseEvent.getPhaseId() == PhaseId.RENDER_RESPONSE) { FacesContext fctx = FacesContext.getCurrentInstance(); AdfFacesContext adfFacesContext = AdfFacesContext.getCurrentInstance(); boolean isInitialRender = adfFacesContext.isInitialRender(); if (isInitialRender) { initTableData(); initTableUI(); } } } public void initTableUI() { List tableChildren = useCase3Table.getChildren(); for (int i = 0; i < rowsData.get(0).getColumnsData().size(); i++) { RichColumn column = new RichColumn(); column.setId("col" + (i + 1)); column.setSortable(false); column.setHeaderText(rowsData.get(0).getColumnsData().get(i).getHeaderName()); FacesContext fctx = FacesContext.getCurrentInstance(); ELContext elctx = fctx.getELContext(); Application jsfApp = fctx.getApplication(); ExpressionFactory exprFactory = jsfApp.getExpressionFactory(); ValueExpression valueExpr = exprFactory.createValueExpression(elctx, "#{row.columnsData[" + i + "].cellValue}", Object.class); String columnType = rowsData.get(0).getColumnsData().get(i).getColumnType(); if (columnType.equals("DATEBOX")) { RichInputDate inputDate = new RichInputDate(); inputDate.setId("cd" + (i + 1)); inputDate.setValueExpression("value", valueExpr); column.getChildren().add(inputDate); } else if (columnType.equals("INPUT_POPUP")) { RichInputListOfValues inputLOV = new RichInputListOfValues(); inputLOV.setId("ilov" + (i + 1)); inputLOV.setValueExpression("value", valueExpr); column.getChildren().add(inputLOV); column.setWidth("120"); } else if (columnType.equals("HIDDEN")) { RichOutputText outputText = new RichOutputText(); outputText.setId("ot" + (i + 1)); outputText.setValueExpression("value", valueExpr); column.getChildren().add(outputText); column.setVisible(false); } else if (columnType.equals("INPUT_TEXT")) { RichInputText inputText = new RichInputText(); inputText.setId("it" + (i + 1)); inputText.setValueExpression("value", valueExpr); column.getChildren().add(inputText); } else { RichOutputText outputText = new RichOutputText(); outputText.setId("ot" + (i + 1)); outputText.setValueExpression("value", valueExpr); column.getChildren().add(outputText); } tableChildren.add(column); } } public void initTableData() { BindingContainer bindings = getBindings(); oracle.binding.OperationBinding operationBinding = bindings.getOperationBinding("populateTableData"); rowsData = (List )operationBinding.execute(); } public void setUseCase3Table(RichTable useCase3Table) { this.useCase3Table = useCase3Table; } public RichTable getUseCase3Table() { return useCase3Table; } public void setRowsData(List rowsData) { this.rowsData = rowsData; } public List getRowsData() { return rowsData; } }
(3)运行效果
Project 下载:ADF_Table_Dynamic_Column.7z
没有评论:
发表评论