2013-04-09 70 views
0

我想在我的页面上使用UIBinder a CellTable创建从服务器接收的数据。 我想我不太了解UiBinder的逻辑。在致电initWidget()之前我应该​​做什么步骤,以及我不应该做什么?
当我删除对configureDataProvider()的呼叫时,表格会显示,但它是空的,并且进度条正在运行。当我包含configureDataProvider()时,表格根本不呈现。GWT UiBinder CellTable不是用AsyncDataProvider呈现的

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

@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行,但是寻呼机显示所有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);我改变了start0现在它工作正常。不过,如果对此有任何评论,我将不胜感激。

+1

请包含UiBinder源文件('TableSelectionWidget.ui.xml')。为什么在代码中创建所有的小部件,而不是使用UiBinder XML文件来创建它们? – 2013-04-09 15:25:16

回答

2

initWidget(Widget widget)是用于初始化自身作为小部件的一种可能的内聚集的包装的类Composite的方法。一般来说,您会创建一个根据给定小部件生成的小部件组合,然后将其传递给initiWidget()方法。这样,根源元素就不会暴露(就其方法而言),但仍然像从未包装过一样。

在您的示例中,您有UiBinder负责定义UI,因此您将此绑定创建的返回传递给此方法(uiBinder.createAndBindUi(this))。这个调用(实际上是createAndBindUi()前,但一般是联系在一起的initWidget())之前,你必须实例所有的部件标有@UiField(provided = true)即所有那些谁需要一个正确的初始化纯UiBinder风格,否则是不可能的。在你的情况下,你不需要标记所有的provided = true,只有CellTable需要(用于专业化)。

回到你的问题,如果你删除了configureDataProvider()电话,你将永远不会设置AsyncDataProvider,只是你根本看不到任何数据(这个旋转的gif是因为某处 - 现在不记得哪里了 - 行数已被设置为0)。如果启用它,对addDataDisplay()的调用将隐含地强制范围更新,最终将调用AsyncDataProvider来请求您的数据。

问题在于,在onSuccess()回调中,您不会同时更新行数据的行数。见here

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

+0

在调用'initWidget()'之前,我是否还必须配置小部件(也就是在Button中添加标签,将列添加到Table中)? – frf 2013-04-10 11:56:48

+0

不,结构(将此添加到此)由UiBinder XML定义中的小部件嵌套定义。之后,您可以很好地在您定义的每个窗口小部件上设置样式,属性等。但是可能你会自己帮忙,尽可能地使用'UiBinder',用于样式,文本等等。有关所有详细信息,请参阅[此处](https://developers.google.com/web-toolkit/doc/latest/DevGuideUiBinder)。 – 2013-04-10 12:53:43

+0

谢谢,它的作品,但另一个问题出现了。请查看更新后的问题。 – frf 2013-04-10 13:49:28