GWT UiBinder CellTable不使用AsyncDataProvider呈现

时间:2013-04-09 14:54:17

标签: gwt uibinder celltable

我希望在我的页面上创建UIBinder一个CellTable,其中包含从服务器收到的数据。 我想我不太明白UiBinder的逻辑。在致电initWidget()之前我应该​​采取哪些措施,以及我不应该做什么?
当我删除对configureDataProvider()的呼叫时,表格会显示,但它是空的,并且进度条正在运行。当我包含configureDataProvider()时,表格根本不会呈现。

请您解释一下这里发生了什么?

 
@UiField(provided = true)
VerticalPanel tableSelection;

@UiField(provided = true)
Anchor productTableAnchor;

@UiField(provided = true)
Anchor ordersTableAnchor;

@UiField(provided = true)
Anchor staffTableAnchor;
List<ProductDTO> products;

@UiField(provided = true)
CellTable<ProductDTO> productTable;

@UiField(provided = true)
SimplePager pager;

public TableSelectionWidget() {
SimplePager.Resources pagerResources = GWT
    .create(SimplePager.Resources.class);
tableSelection = new VerticalPanel();
productTableAnchor = new Anchor();
ordersTableAnchor = new Anchor();
staffTableAnchor = new Anchor();
productTable = new CellTable<ProductDTO>();
productTable.setPageSize(10);
configureTable();
pager = new SimplePager(TextLocation.CENTER, pagerResources, false, 0,
    true);
pager.setDisplay(productTable);
initWidget(uiBinder.createAndBindUi(this));
}

private void configureTable() {
Column<ProductDTO, String> nameColumn = new Column<ProductDTO, String>(
    new TextCell()) {
    @Override
    public String getValue(ProductDTO object) {
    return object.getName();
    }
};
Column<ProductDTO, String> priceColumn = new Column<ProductDTO, String>(
    new TextCell()) {
    @Override
    public String getValue(ProductDTO object) {
    return object.getPrice().toString();
    }
};
Column<ProductDTO, String> quantityColumn = new Column<ProductDTO, String>(
    new TextCell()) {
    @Override
    public String getValue(ProductDTO object) {
    return String.valueOf(object.getQuantity());
    }
};
productTable.addColumn(nameColumn, "Name");
productTable.addColumn(priceColumn, "Price");
productTable.addColumn(quantityColumn, "Qty");

configureDataProvider();

}

private void configureDataProvider() {
AsyncDataProvider<ProductDTO> provider = new AsyncDataProvider<ProductDTO>() {
    @Override
    protected void onRangeChanged(HasData<ProductDTO> display) {
    final int start = display.getVisibleRange().getStart();
    int length = display.getVisibleRange().getLength();
    AsyncCallback<List<ProductDTO>> callback = new AsyncCallback<List<ProductDTO>>() {
        public void onFailure(Throwable caught) {
        Window.alert(caught.getMessage());
        }

        public void onSuccess(List<ProductDTO> result) {
        products = result;
        updateRowData(start, result);
        }
    };

    productService.getAllProducts(callback);
    }
};

provider.addDataDisplay(productTable);
provider.updateRowCount(products.size(), true);
}

编辑添加了ui.xml

    <!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
    xmlns:g="urn:import:com.google.gwt.user.client.ui" xmlns:c="urn:import:com.google.gwt.user.cellview.client"
    xmlns:ct="urn:import:com.ooo1sk.client.widgets">
    <ui:style>
    </ui:style>
    <g:HTMLPanel>
        <g:VerticalPanel styleName="" ui:field="tableSelection">
            <g:Anchor ui:field="ordersTableAnchor" text="Orders"></g:Anchor>
            <g:Anchor ui:field="productTableAnchor" text="Product"></g:Anchor>
            <g:Anchor ui:field="staffTableAnchor" text="Staff"></g:Anchor>
        </g:VerticalPanel>
        <g:HTMLPanel>
            <table cellspacing='0' cellpadding='0' style='width:100%;'>
                <tr>
                    <td valign='top'>
                        <c:CellTable addStyleNames='infoTable' pageSize='15'
                            ui:field='productTable' />
                    </td>
                </tr>
                <tr>
                    <td align='center'>
                        <c:SimplePager ui:field='pager' />
                    </td>
                </tr>
            </table>
        </g:HTMLPanel>
    </g:HTMLPanel>
</ui:UiBinder> 

UPD:更改后它可以正常工作,但只向我显示前15行,但是Pager会显示所有91行,并在点击后更改数字。

 private void configureDataProvider() {

    AsyncDataProvider<ProductDTO> provider = new AsyncDataProvider<ProductDTO>() {
        @Override
        protected void onRangeChanged(HasData<ProductDTO> display) {
        final int start = display.getVisibleRange().getStart();
        final int length = display.getVisibleRange().getLength();

        productService
            .getAllProducts(new AsyncCallback<List<ProductDTO>>() {

                public void onSuccess(List<ProductDTO> result) {
                label.setText("start " + start + " "
                    + "resultsize " + result.size() + " "
                    + "length " + length);
                products = result;
                updateRowCount(result.size(), true);
                updateRowData(start, result);

                }

                public void onFailure(Throwable caught) {
                Window.alert(caught.getMessage());
                }
            });
        }
    };
    provider.addDataDisplay(productTable);
    }

好的,玩数字修复了这个问题。在updateRowData(start, result);我将start更改为0,现在工作正常。不过,我非常感谢专家对此的任何评论。

1 个答案:

答案 0 :(得分:2)

initWidget(Widget widget)Composite类的一种方法,用于将自身初始化为可能的小部件内部聚合的包装器。通常,您创建一个以给定窗口小部件为基础的窗口小部件组合,然后将其传递给initiWidget()方法。这样,rooted元素不会被暴露(就其方法而言),但仍然就像从未被包装一样。

在您的示例中,您UiBinder负责定义UI,因此您将返回绑定创建(uiBinder.createAndBindUi(this))的方法传递给此类方法。在此调用之前(实际上,在createAndBindUi()之前,但通常它与initWidget()绑定),您必须实例化标有@UiField(provided = true)的所有小部件,即,所有那些需要正确初始化的人,否则无法以简单的UiBinder风格进行。在您的情况下,您无需标记所有内容provided = true,只需要CellTable要求(专业化)。

回到你的问题,如果你删除configureDataProvider()电话,你将永远不会设置AsyncDataProvider而只是你根本看不到任何数据(旋转的gif是因为某处 - 不能记住现在的位置 - 行计数已设置为0)。如果您启用此功能,则对addDataDisplay()的调用将隐式强制进行范围更新,最终会调用AsyncDataProvider来请求您的数据。

问题是您没有在onSuccess()回调中同时更新行数据行数。请参阅here

此外,您无需在updateRowCount()之后调用addDataDisplay(),否则将发出两个请求来检索相同的初始数据,这是不必要的。