CustomdataGrid未在浏览器中显示

时间:2014-01-21 09:58:18

标签: gwt datagrid

我有一个自定义数据网格,定义如下

public class CustomDataGrid<T> extends DataGrid<T> {

    private static final int PAGE_SIZE = 10;

    public CustomDataGrid(ProvidesKey<T> keysProvider) {
        super(PAGE_SIZE, keysProvider);
    }

    public CustomDataGrid() {
        super(PAGE_SIZE);
    }

    public void redrawRow(int absRowIndex) {
        int relRowIndex = absRowIndex - getPageStart();
        checkRowBounds(relRowIndex);
        setRowData(absRowIndex, Collections.singletonList(getVisibleItem(relRowIndex)));
    }
}

我正在使用ui binder,在我的xml文件中,我已经定义了如下元素

  <com:CustomDataGrid ui:field="commissionListDataGrid"></com:CustomDataGrid>

现在,自定义数据网格初始化如下

@UiField     CustomDataGrid commissionListDataGrid;

private final Set<Long> showingFriends = new HashSet<Long>();

private Column<ServiceCategorywiseCommissionDetails, String> viewFriendsColumn;

private Column<ServiceCategorywiseCommissionDetails, String> serviceType;

public ZoneCommissionListView() {

    commissionListDataGrid = new CustomDataGrid<ServiceCategorywiseCommissionDetails>(new ProvidesKey<ServiceCategorywiseCommissionDetails>() {

        @Override
        public Object getKey(ServiceCategorywiseCommissionDetails item) {
            return item == null ? null : item.getId();
        }
    });
    commissionListDataGrid.setWidth("100%");
    commissionListDataGrid.setEmptyTableWidget(new Label("Empty data"));
    commissionListDataGrid.setHeight("100%");

    //      commissionListLayoutPanel = new SimpleLayoutPanel();

    initCommissionListDataGrid();
    //      commissionListLayoutPanel.add(commissionListDataGrid);

    //RootLayoutPanel.get().add(commissionListLayoutPanel);

}

@Override
public Widget asWidget() {
    return this.widget;
}

@Override
public void setUiHandlers(ZoneCommissionListUiHandlers uiHandlers) {
    this.uiHandlers = uiHandlers;
}

public void initCommissionListDataGrid() {

    // View friends.
    SafeHtmlRenderer<String> anchorRenderer = new AbstractSafeHtmlRenderer<String>() {
        @Override
        public SafeHtml render(String object) {
            SafeHtmlBuilder sb = new SafeHtmlBuilder();
            sb.appendHtmlConstant("(<a href=\"javascript:;\">").appendEscaped(object).appendHtmlConstant("</a>)");
            return sb.toSafeHtml();
        }
    };

    viewFriendsColumn = new Column<ServiceCategorywiseCommissionDetails, String>(new ClickableTextCell(anchorRenderer)) {
        @Override
        public String getValue(ServiceCategorywiseCommissionDetails object) {
            if (showingFriends.contains(object.getId())) {
                return "-";
            } else {
                return "+";
            }
        }
    };
    viewFriendsColumn.setFieldUpdater(new FieldUpdater<ServiceCategorywiseCommissionDetails, String>() {
        @Override
        public void update(int index, ServiceCategorywiseCommissionDetails object, String value) {
            if (showingFriends.contains(object.getId())) {

                showingFriends.remove(object.getId());
            } else {
                showingFriends.add(object.getId());
            }

            // Redraw the modified row.
            commissionListDataGrid.redrawRow(index);
        }
    });

    // First name.
    serviceType = new Column<ServiceCategorywiseCommissionDetails, String>(new TextCell()) {
        @Override
        public String getValue(ServiceCategorywiseCommissionDetails object) {
            return object.getServiceType();
        }
    };

    commissionListDataGrid.setTableBuilder(new CustomTableBuilder());
    commissionListDataGrid.setHeaderBuilder(new CustomHeaderBuilder());
    //      commissionListDataGrid.setFooterBuilder(new CustomFooterBuilder());
    //      GWT.log("list size is " + ContactDatabase.get().getDataProvider().getList().size());
    //      commissionListDataGrid.setRowData(ContactDatabase.get().getDataProvider().getList());
    //      Button button = new Button();
    //      button.setText("hello");

    //      commissionListLayoutPanel.add(commissionListDataGrid);

    this.widget = uiBinder.createAndBindUi(this);

}

private class CustomTableBuilder extends AbstractCellTableBuilder<ServiceCategorywiseCommissionDetails> {

    private final String childCell = " ";
    private final String rowStyle;
    private final String selectedRowStyle;
    private final String cellStyle;
    private final String selectedCellStyle;

    @SuppressWarnings("deprecation")
    public CustomTableBuilder() {
        super(commissionListDataGrid);

        // Cache styles for faster access.
        Style style = commissionListDataGrid.getResources().style();
        rowStyle = style.evenRow();
        selectedRowStyle = " " + style.selectedRow();
        cellStyle = style.cell() + " " + style.evenRowCell();
        selectedCellStyle = " " + style.selectedRowCell();

    }

    public void buildRowImpl(ServiceCategorywiseCommissionDetails rowValue, int absRowIndex) {
        buildServiceTypeRow(rowValue, absRowIndex, false);
        GWT.log("Inside build row impl");
        // Display list of friends.
        if (showingFriends.contains(rowValue.getId())) {
            TableRowBuilder row = startRow();

            TableCellBuilder th = row.startTH();
            th.text("").endTH();
            TableCellBuilder th2 = row.startTH();
            th2.text("Service Name").endTH();
            TableCellBuilder th3 = row.startTH();
            th3.text("SuperZone Commission").endTH();
            TableCellBuilder th4 = row.startTH();
            th4.text("Zone Commission").endTH();
            row.endTR();

            List<ServiceCommissionDetails> friends = rowValue.getServiceCommissionDetails();
            for (ServiceCommissionDetails friend : friends) {
                buildServiceCommissionDetailRow(friend, absRowIndex, true);
            }
        }
    }

    @SuppressWarnings("deprecation")
    private void buildServiceTypeRow(ServiceCategorywiseCommissionDetails rowValue, int absRowIndex, boolean isFriend) {
        GWT.log("inside build service Type row");
        SelectionModel<? super ServiceCategorywiseCommissionDetails> selectionModel = commissionListDataGrid.getSelectionModel();
        boolean isSelected = (selectionModel == null || rowValue == null) ? false : selectionModel.isSelected(rowValue);
        boolean isEven = absRowIndex % 2 == 0;
        StringBuilder trClasses = new StringBuilder(rowStyle);
        if (isSelected) {
            trClasses.append(selectedRowStyle);
        }

        // Calculate the cell styles.
        String cellStyles = cellStyle;
        if (isSelected) {
            cellStyles += selectedCellStyle;
        }
        if (isFriend) {
            cellStyles += childCell;
        }

        TableRowBuilder row = startRow();
        row.className(trClasses.toString());

        /*
         * Checkbox column.
         * 
         * This table will uses a checkbox column for selection. Alternatively, you can call dataGrid.setSelectionEnabled(true) to
         * enable mouse selection.
         */
        TableCellBuilder td = row.startTD();
        td.className(cellStyles);
        td.style().outlineStyle(OutlineStyle.NONE).endStyle();

        td.endTD();

        /*
         * View friends column.
         * 
         * Displays a link to "show friends". When clicked, the list of friends is displayed below the contact.
         */
        td = row.startTD();
        td.className(cellStyles);
        if (!isFriend) {
            td.style().outlineStyle(OutlineStyle.NONE).endStyle();
            renderCell(td, createContext(1), viewFriendsColumn, rowValue);
        }
        td.endTD();

        // First name column.
        td = row.startTD();
        td.className(cellStyles);
        td.style().outlineStyle(OutlineStyle.NONE).endStyle();
        if (isFriend) {
            td.text(rowValue.getServiceType());
        } else {
            renderCell(td, createContext(2), serviceType, rowValue);
        }
        td.endTD();

        // Last name column.
        row.endTR();
    }

    @SuppressWarnings("deprecation")
    private void buildServiceCommissionDetailRow(ServiceCommissionDetails rowValue, int absRowIndex, boolean isFriend) {
        GWT.log("inside build service commission detail row");
        // Calculate the row styles.

        // boolean isSelected = (selectionModel == null || rowValue == null)
        // ? false : selectionModel.isSelected(rowValue);
        // boolean isEven = absRowIndex % 2 == 0;
        StringBuilder trClasses = new StringBuilder(rowStyle);
        // if (isSelected) {
        // trClasses.append(selectedRowStyle);
        // }

        // Calculate the cell styles.
        String cellStyles = cellStyle;

        // cellStyles += selectedCellStyle;

        cellStyles += childCell;

        TableRowBuilder row = startRow();
        row.className(trClasses.toString());

        /*
         * Checkbox column.
         * 
         * This table will uses a checkbox column for selection. Alternatively, you can call dataGrid.setSelectionEnabled(true) to
         * enable mouse selection.
         */
        TableCellBuilder td = row.startTD();
        td.className(cellStyles);
        td.style().outlineStyle(OutlineStyle.NONE).endStyle();

        td.endTD();

        /*
         * View friends column.
         * 
         * Displays a link to "show friends". When clicked, the list of friends is displayed below the contact.
         */

        // First name column.
        td = row.startTD();
        td.className(cellStyles);
        td.style().outlineStyle(OutlineStyle.NONE).endStyle();

        td.text(rowValue.getServiceName());

        td.endTD();

        td = row.startTD();
        td.className(cellStyles);
        td.style().outlineStyle(OutlineStyle.NONE).endStyle();

        td.text(rowValue.getSuperZoneCommission());

        td.endTD();

        td = row.startTD();
        td.className(cellStyles);
        td.style().outlineStyle(OutlineStyle.NONE).endStyle();

        td.text(rowValue.getZoneCommission());

        td.endTD();

        // Last name column.
        row.endTR();
    }
}

private class CustomHeaderBuilder extends AbstractHeaderOrFooterBuilder<ServiceCategorywiseCommissionDetails> {

    private Header<String> firstNameHeader = new TextHeader("Co mmission List");

    public CustomHeaderBuilder() {
        super(commissionListDataGrid, false);
        setSortIconStartOfLine(false);
    }

    @Override
    protected boolean buildHeaderOrFooterImpl() {
        Style style = commissionListDataGrid.getResources().style();
        String groupHeaderCell = "Header Cell";

        // Add a 2x2 header above the checkbox and show friends columns.
        TableRowBuilder tr = startRow();
        tr.startTH().colSpan(2).rowSpan(2).className(style.header() + " " + style.firstColumnHeader());
        tr.endTH();

        /*
         * Name group header. Associated with the last name column, so clicking on the group header sorts by last name.
         */

        // Get information about the sorted column.
        ColumnSortList sortList = commissionListDataGrid.getColumnSortList();
        ColumnSortInfo sortedInfo = (sortList.size() == 0) ? null : sortList.get(0);
        Column<?, ?> sortedColumn = (sortedInfo == null) ? null : sortedInfo.getColumn();
        boolean isSortAscending = (sortedInfo == null) ? false : sortedInfo.isAscending();

        // Add column headers.
        tr = startRow();
        buildHeader(tr, firstNameHeader, serviceType, sortedColumn, isSortAscending, false, false);

        tr.endTR();

        return true;
    }

    private void buildHeader(TableRowBuilder out, Header<?> header, Column<ServiceCategorywiseCommissionDetails, ?> column, Column<?, ?> sortedColumn, boolean isSortAscending, boolean isFirst,
            boolean isLast) {

        // Choose the classes to include with the element.
        Style style = commissionListDataGrid.getResources().style();
        boolean isSorted = (sortedColumn == column);
        StringBuilder classesBuilder = new StringBuilder(style.header());
        if (isFirst) {
            classesBuilder.append(" " + style.firstColumnHeader());
        }
        if (isLast) {
            classesBuilder.append(" " + style.lastColumnHeader());
        }
        // if (column.isSortable()) {
        // classesBuilder.append(" " + style.sortableHeader());
        // }
        if (isSorted) {
            classesBuilder.append(" " + (isSortAscending ? style.sortedHeaderAscending() : style.sortedHeaderDescending()));
        }

        // Create the table cell.
        TableCellBuilder th = out.startTH().className(classesBuilder.toString());

        // Associate the cell with the column to enable sorting of the
        // column.
        enableColumnHandlers(th, column);

        // Render the header.
        Context context = new Context(0, 2, header.getKey());
        renderSortableHeader(th, context, header, isSorted, isSortAscending);

        // End the table cell.
        th.endTH();
    }
}


public void setCommissionListDataGrid(ListDataProvider<ServiceCategorywiseCommissionDetails> dataProvider) {
    GWT.log("inside set commissionListDataGrid size is " + dataProvider.getList().size());
    commissionListDataGrid.setRowData(dataProvider.getList());

}

我在演示者中有一个方法,它调用方法集CommissionListDataGrid并设置其行值。

执行此操作时,不会显示数据网格。但是,如果我在构造函数'ZoneCommissionListView()'中以下列方式添加simplelayoutpanel

RootPanel.get().add(commissionListLayoutPanel);

然后显示数据网格。我到底错过了什么。任何建议都将不胜感激

1 个答案:

答案 0 :(得分:0)

  

DataGrid需要放在实现的LayoutPanel或Panel中   ProvideResize接口可见。

Source

您测试的SimpelLayoutPanel也实现了ProvidesResize接口。