我使用Java和icefaces,我需要构建一个表,每次将有不同数量的列。
到目前为止,我找到的最好的例子是在icefaces网站上我试图让列的数量是动态的,但我不知道如何在html页面中获得单元格值。下面是我的代码:
My bean:
public class DynamicColumnsBean implements Serializable {
int randomRows ;
int randomCol ;
List<Task> data; // rows data
List<ColumnModel> columns ;
public DynamicColumnsBean() {
super();
Random rn = new Random();
randomRows = rn.nextInt() % 40;
randomCol = rn.nextInt() % 30;
if(randomRows==0) randomRows=10;
if(randomCol==0) randomCol=7;
if(randomRows<0) randomRows*=-1;
if(randomCol<0) randomCol*=-1;
columns = new ArrayList<ColumnModel>();
for (int i=0 ; i< randomCol ; i++ )
{
columns.add(new ColumnModel(i, "ID "+ i +" "));
}
data = new ArrayList<Task>() ; //row objects
for (int i=0 ; i< randomRows ; i++ )
{
List<String> obj = new ArrayList<String>() ;
for (int j=0 ; j< randomCol ; j++ )
{
obj.add("row "+i +" col "+j);
}
Task temp = new Task(obj);
data.add(temp);
}
}
public List<Task> getData()
{ return data; }
public void setData(ArrayList<Task> data)
{ this.data = data; }
public List<ColumnModel> getColumns() {
return columns; }
public void setColumns(ArrayList<ColumnModel> columns) {
this.columns = columns; }
}
public class Task {
List<String> obj ;
public Task() {
super();
}
public Task(List<String> obj) {
super();
obj = new ArrayList<String>();
this.obj = obj;
}
public List<String> getObj() {
return obj; }
public void setObj(List<String> obj) {
this.obj = obj; }
}
public class ColumnModel {
int value; // represents sortBy / filterBy as one field
String headerText;
public ColumnModel(int i, String headerText) {
this.value = i;
this.headerText = headerText;
}
public int getValue() {
return value; }
public void setValue(int value) {
this.value = value; }
public String getHeaderText() {
return headerText; }
public void setHeaderText(String headerText) {
this.headerText = headerText; }
}
<ace:dataTable value="#{dynBean2.data}" var="row" scrollable="true"
height="200" paginator="true" rows="5" >
<c:forEach items="#{dynBean2.columns}" var="col">
<ace:column headerText="#{col.headerText}" style="width: 40px" >
<ice:outputText value=" #{row['obj'[col.value]]}"></ice:outputText>
</ace:column>
</c:forEach>
</ace:dataTable>
我的问题是:
ice:outputText value=" #{row['obj'[col.value]]}"
我需要的值是:获取索引I中的数据->获取索引[col.value]中的obj中的值
您可以尝试使用varStatus:
<c:forEach items="#{dynBean2.columns}" var="col" varStatus="colStatus">
<ace:column headerText="#{col.headerText}" style="width: 40px" >
<ice:outputText value=" #{row.obj[colStatus.index]}"></ice:outputText>
</ace:column>
</c:forEach>