2014-01-13 62 views
1

我有一個CellTables的列表。我怎樣才能合併這兩個表的行並返回結果。 例如如何合併GWT中的兩個單元格表的內容

List<CellTable> cellTables = new ArrayList<CellTable>(); 
celltables.add(table1); 
celltables.add(table2); 
celltables.add(table3); 

我使用以下方法

private CellTable fetchAllCellTables() { 
      CellTable table=new CellTable(); 
      for(CellTable tempTable:cellTables){ 
        int numRows = tempTable.getRowCount(); 
        table.setRowCount(numRows+1); 
        table.setRowData((List) tempTable.getLayoutData()); 
      } 
      return table; 

    } 

,但我無法看到的總含量。

回答

1

什麼,我認爲這裏將是最好的辦法是用每個CellTable S以及最終CellTableDataProvider

示例代碼:

// Create a CellList. 
CellList<String> cellList = new CellList<String>(new TextCell()); 

// Create a data provider. 
MyDataProvider dataProvider = new MyDataProvider(); 

// Add the cellList to the dataProvider. 
dataProvider.addDataDisplay(cellList); 

// Get the underlying list from data dataProvider. 
List<String> list = dataProvider.getList(); 

// Add the value to the list. The dataProvider will update the cellList. 
list.add(newValue); // you can do this in a loop so that you merge all values 

對於您的情況爲您正在使用一個ListCellTable,你將不得不同樣保持各自的 s

2

我假設您要製作一張顯示您的小桌子行的大桌子:

table1      table2 
col1 | col2 | col3   col1 | col2 | col3 | col4 
------------------   ------------------------- 
a | b | c    1 | 2 | 3 | 4 

big table 
col1 | col2 | col3 | col1 | col2 | col3 | col4 
---------------------------------------------- 
a | b | c | 1 | 2 | 3 | 4 

與例如

CellTable<String[]> table1 = new CellTable<String[]>(); 
table1.addColumn(new Column<String[], String>(new TextCell()){ 

    public String getValue(String[] object){ 
     return object[0]; 
    } 

}, "col1"); 

此解決方案僅適用,如果你可以編輯源代碼構建小桌子!

我首先定義一個行對象類,它包含大表中單個行的全部信息,例如,

public class RowObject{ 

    public String[] table1RowObject; // the class of the field should be the generic 
            // type of the table1 CellTable 

    public MyObject table2RowObject; // the class of the field should 
            // be the generic type of table2 

    // ... other tables 

} 

現在改變泛型類型的小表來RowObject

CellTable<RowObject> table1 = new CellTable<RowObject>(); 
table1.addColumn (new Column<RowObject, String>(new TextCell()){ 

    public String getValue(RowObject object){ 
     // The data of table1 has been moved into the table1RowObject 
     // old: String[] object; return object[0]; 
     return object.table1RowObject[0]; 
    } 

}, "col1"); 

那麼大的表可以很容易地構造這樣的:

CellTable<RowObject> bigTable = new CellTable<RowObject>(); 
for (CellTable<RowObject> ct : tablesList){ 
    for (int i = 0; i < ct.getColumnCount(); i++) 
     bigTable.addColumn(ct.getColumn(i)); 
} 

加載數據的所有表同時在數據提供者的幫助下,例如,

ListDataProvider<RowObject> dataSource = new ListDataProvider<RowObject>(); 
dataSource.addDataDisplay(table1); 
dataSource.addDataDisplay(table2); 
dataSource.addDataDisplay(bigTable); 

,然後只要你更新dataSource所有的小表,可能得到在同一時間爲大表進行更新。

相關問題