您可以使用支持 bean 方法在运行时创建列
public void loadDynamicList() throws Exception {
// Set headers (optional).
//dynamicHeaders = new String[] {"ID", "Name", "Value","delete"};
// Set rows. This is a stub example, just do your dynamic thing.
dynamicList = new ArrayList<String>();
/*dynamicList.add("One");
dynamicList.add("Two");
dynamicList.add("Three");
dynamicList.add("Four");
dynamicList.add("Five");
dynamicList.add("Six");*/
/*dynamicList.add(Arrays.asList(new String[] { "ID1","ID2" }));
dynamicList.add(Arrays.asList(new String[] { "ID1","ID2" }));
dynamicList.add(Arrays.asList(new String[] { "ID1","ID2" }));
dynamicList.add(Arrays.asList(new String[] { "ID1","ID2" }));
dynamicList.add(Arrays.asList(new String[] { "ID1","ID2" }));*/
existingCountryList = new ArrayList<Country>();
String countryCode="SL";
existingCountryList.add(getCountryService().getCountryByCode(countryCode));
Country country=getCountryService().getCountryByCode(countryCode);
countryLanguageSet=country.getCountryLanguage();
int languageSize=country.getCountryLanguage().size();
dynamicHeaders = new String[languageSize+1] ;
int counter=0;
for (CountryLanguage count: countryLanguageSet) {
System.out.println(count.getLanguage().getLanguageName());
dynamicHeaders[counter]=count.getLanguage().getLanguageName();
counter++;
}
dynamicHeaders[counter]="Delete";
System.out.println("header list "+dynamicHeaders.toString());
System.out.println("size"+dynamicHeaders.length);
}
public void populateDynamicDataTable() {
debugLogger.debug("populateDynamicDataTable:Enter");
// Create <h:dataTable value="#{myBean.dynamicList}" var="dynamicItem">.
HtmlDataTable dynamicDataTable = new HtmlDataTable();
dynamicDataTable.setValueExpression("value", createValueExpression("# {relationBean.dynamicList}", List.class));
dynamicDataTable.setVar("dynamicItem");
for (int count = 0; count < dynamicHeaders.length; count++) {
HtmlColumn column = new HtmlColumn();
HtmlOutputText header = new HtmlOutputText();
header.setValue(dynamicHeaders[count]);
column.setHeader(header);
if(dynamicHeaders[count].equals("Delete")){
HtmlCommandButton commandButton=new HtmlCommandButton();
commandButton.setValue("Delete"+count);
commandButton.setActionExpression(createActionExpression("#{relationBean.deleteRow}", String.class));
column.getChildren().add(commandButton);
}else{
HtmlInputText input=new HtmlInputText();
//List<String[]> ls = new ArrayList<String[]>();
input.setValueExpression("value",createValueExpression("#{dynamicItem}", String.class));
column.getChildren().add(input);
}
dynamicDataTable.getChildren().add(column);
}
dynamicDataTableGroup = new HtmlPanelGroup();
dynamicDataTableGroup.getChildren().add(dynamicDataTable);
debugLogger.debug("populateDynamicDataTable:Exit");
}
public HtmlPanelGroup getDynamicDataTableGroup() throws Exception {
// This will be called once in the first RESTORE VIEW phase.
if (dynamicDataTableGroup == null) {
loadDynamicList(); // Preload dynamic list.
populateDynamicDataTable(); // Populate editable datatable.
}
return dynamicDataTableGroup;
}
public List<String> getDynamicList() {
return dynamicList;
}
public void setDynamicList(List<String> dynamicList) {
this.dynamicList = dynamicList;
}
public void setDynamicDataTableGroup(HtmlPanelGroup dynamicDataTableGroup) {
this.dynamicDataTableGroup = dynamicDataTableGroup;
}
public ValueExpression createValueExpression(String valueExpression, Class<?> valueType) {
FacesContext facesContext = FacesContext.getCurrentInstance();
return facesContext.getApplication().getExpressionFactory().createValueExpression(
facesContext.getELContext(), valueExpression, valueType);
}
public MethodExpression createActionExpression(String actionExpression, Class<?> returnType) {
FacesContext facesContext = FacesContext.getCurrentInstance();
return facesContext.getApplication().getExpressionFactory().createMethodExpression(
facesContext.getELContext(), actionExpression, returnType, new Class[0]);
}
private MethodExpression createMethodExpression(String valueExpression, Class<?> valueType, Class<?>[] expectedParamTypes) {
FacesContext facesContext = FacesContext.getCurrentInstance();
return facesContext.getApplication().getExpressionFactory().createMethodExpression(
facesContext.getELContext(), valueExpression, valueType, expectedParamTypes);
}