【问题标题】:CellTable Issues with custom rows and ListDataProvider GWT自定义行和 ListDataProvider GWT 的 CellTable 问题
【发布时间】:2014-01-22 04:45:48
【问题描述】:

基本上,我想要做的是有一个包含多个部分的 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 一起工作。我似乎无法根据 ListDataProvider 中的内容确切地确定 CellTable 何时刷新,以及如何根据 ListDataProvider 中的内容将行添加到 CellTable。

【问题讨论】:

    标签: gwt celltable


    【解决方案1】:

    我想出了问题所在,以防将来有人遇到问题。当您单击一行时,它会根据您的选择模型重绘该行。所以它正在重绘行,但由于我已经覆盖了 buildRowImpl,它只是在调用我的自定义行创建,所以它会添加到表中而不是重绘单击的行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-19
      • 2014-02-19
      相关资源
      最近更新 更多