CellTable自定义行和ListDataProvider GWT的问题

时间:2014-01-22 04:45:48

标签: gwt celltable

基本上我要做的是拥有一个包含多个部分的CellTable,由一个跨越所有列的自定义行分隔以描绘这些部分。到目前为止,我使用ListDataProvider作为表数据,我正在扩展AbstractCellTableProvider并重写buildRowImpl以确定何时需要添加自定义行。它有点工作,但并不是所有的行都显示出来,并且由于某些奇怪的原因,当我单击选择CellTable中的一行时,它会随机地向CellTable添加重复的行。我不确定我使用的SelectionModel是否会发生奇怪的事情,但如果有人能够帮助解决这个问题,我们将不胜感激。

这是我创建的类,它扩展了AbstractCellTableBuilder。默认行构建器基本上是从buildRowImpl通常复制的。如果ListDataProvider rowValue使用if语句匹配某个条件,那么它将被发送到我的buildExtraRow,其中创建了单个单元格行。

    public class CustomCellTableBuilder extends AbstractCellTableBuilder<SearchColumn>{
    public CustomCellTableBuilder() {

        super(cellTable_2);
        System.out.println("Getting into CustomCellTableBuilder super");
    }

    @Override
    protected void buildRowImpl(SearchColumn rowValue, int absRowIndex){
       //building main rows logic
        System.out.println("Getting into custom buildRowImpl, labelrow = " + labelrow);

        if (rowValue.quantity.equals("test")){
            System.out.println("Going to build extra row if");
            buildExtraRow(absRowIndex, rowValue);
        }
        else {
            System.out.println("rowValue: " + rowValue);
             final String evenRowStyle;
               final String oddRowStyle;
               final String selectedRowStyle;
               final String cellStyle;
               final String evenCellStyle;
               final String oddCellStyle;
               final String firstColumnStyle;
               final String lastColumnStyle;
               final String selectedCellStyle;

               Style style = cellTable_2.getResources().style();
                evenRowStyle = style.evenRow();
                oddRowStyle = style.oddRow();
                selectedRowStyle = " " + style.selectedRow();
                cellStyle = style.cell();
                evenCellStyle = " " + style.evenRowCell();
                oddCellStyle = " " + style.oddRowCell();
                firstColumnStyle = " " + style.firstColumn();
                lastColumnStyle = " " + style.lastColumn();
                selectedCellStyle = " " + style.selectedRowCell();

            // Calculate the row styles.
            SelectionModel<? super SearchColumn> selectionModel = cellTable_2.getSelectionModel();
            boolean isSelected =
                (selectionModel == null || rowValue == null) ? false : selectionModel.isSelected(rowValue);
            boolean isEven = absRowIndex % 2 == 0;
            StringBuilder trClasses = new StringBuilder(isEven ? evenRowStyle : oddRowStyle);
            if (isSelected) {
              trClasses.append(selectedRowStyle);
            }

            // Add custom row styles.
            RowStyles<SearchColumn> rowStyles = cellTable_2.getRowStyles();
            if (rowStyles != null) {
              String extraRowStyles = rowStyles.getStyleNames(rowValue, absRowIndex);
              if (extraRowStyles != null) {
                trClasses.append(" ").append(extraRowStyles);
              }
            }

            // Build the row.
            TableRowBuilder tr = startRow();
            tr.className(trClasses.toString());

            // Build the columns.
            int columnCount = cellTable_2.getColumnCount();
            System.out.println("column count " + columnCount);
            for (int curColumn = 0; curColumn < columnCount; curColumn++) {
              Column<SearchColumn, ?> column = cellTable_2.getColumn(curColumn);
              // Create the cell styles.
              StringBuilder tdClasses = new StringBuilder(cellStyle);
              tdClasses.append(isEven ? evenCellStyle : oddCellStyle);
              if (curColumn == 0) {
                tdClasses.append(firstColumnStyle);
              }
              if (isSelected) {
                tdClasses.append(selectedCellStyle);
              }
              // The first and last column could be the same column.
              if (curColumn == columnCount - 1) {
                tdClasses.append(lastColumnStyle);
              }

              // Add class names specific to the cell.
              Cell.Context context = new Cell.Context(absRowIndex, curColumn, cellTable_2.getValueKey(rowValue));
              String cellStyles = column.getCellStyleNames(context, rowValue);
              if (cellStyles != null) {
                tdClasses.append(" " + cellStyles);
              }

              // Build the cell.
              HorizontalAlignmentConstant hAlign = column.getHorizontalAlignment();
              VerticalAlignmentConstant vAlign = column.getVerticalAlignment();
              TableCellBuilder td = tr.startTD();
              td.className(tdClasses.toString());
              if (hAlign != null) {
                td.align(hAlign.getTextAlignString());
              }
              if (vAlign != null) {
                td.vAlign(vAlign.getVerticalAlignString());
              }
              // Add the inner div.
              DivBuilder div = td.startDiv();
              div.style().outlineStyle(OutlineStyle.NONE).endStyle();

              // Render the cell into the div.
              renderCell(div, context, column, rowValue);

              // End the cell.
              div.endDiv();
              td.endTD();
            }

            // End the row.
            tr.endTR();
        }
    }

    private void buildExtraRow(int absRowIndex, SearchColumn rowValue){
        System.out.println("In buildExtraRow");
        start(true);
        TableRowBuilder row = startRow();
        TableCellBuilder td = row.startTD().colSpan(getColumns().size());
        td.text(label).endTD();
        row.endTR();

        //cellTable_2.redrawRow(absRowIndex);
    }}

基本上我将如何向表中添加一行:

searchProvider.getList().add(new SearchColumn("test","","","","","","","",""));

我对此感到困惑的其他事情是ListDataProvider如何与CellTable一起使用。我似乎无法弄清楚CellTable何时根据ListDataProvider中的内容进行刷新,以及如何根据ListDataProvider中的内容将行添加到CellTable。

1 个答案:

答案 0 :(得分:0)

我想出了问题是什么,如果有人在将来遇到问题。单击某行时,它会根据您的选择模型重绘该行。所以它重新绘制了行,但是因为我已经覆盖了buildRowImpl,所以它只是调用我的自定义行创建,因此它会添加到表中而不是重新绘制所单击的行。