如何将新条目添加到GWT数据网格中?
我试图找到向GWT DataGrid小部件添加新条目的解决方案,但没有成功 我有以下自定义DataGrid小部件的代码:如何将新条目添加到GWT数据网格中?,gwt,datagrid,Gwt,Datagrid,我试图找到向GWT DataGrid小部件添加新条目的解决方案,但没有成功 我有以下自定义DataGrid小部件的代码: /** * Custom DataGridWidget with asynchronous data transferring. */ public class DataGridWidget implements EntryPoint { /* Path to UI template */ @UiTemplate("ui/DataGridWidget.u
/**
* Custom DataGridWidget with asynchronous data transferring.
*/
public class DataGridWidget implements EntryPoint {
/* Path to UI template */
@UiTemplate("ui/DataGridWidget.ui.xml")
/* Interface for using UiBinder */
interface IDataGridWidget extends UiBinder<Widget, DataGridWidget> {
}
/* Instance of IDataGridWidget interface for next initialization of this widget */
private static IDataGridWidget dataGridWidget = GWT.create(IDataGridWidget.class);
/* Instance of service class for getting data from server in async mode */
protected static GettingServiceAsync gettingService = GWT.create(GettingService.class);
/* DataGrid */
@UiField(provided=true)
DataGrid<Contact> dataGrid;
/* "Add entry" button */
@UiField(provided=true)
Button bAdd;
/**
* Entry point method.
*/
public void onModuleLoad() {
/* Initialization of dataGrid */
dataGrid = new DataGrid<Contact>();
/* Create name column */
TextColumn<Contact> nameColumn = new TextColumn<Contact>() {
@Override
public String getValue(Contact contact) {
return contact.name;
}
};
/* Create address column. */
TextColumn<Contact> addressColumn = new TextColumn<Contact>() {
@Override
public String getValue(Contact contact) {
return contact.address;
}
};
/* Add the columns */
table.addColumn(nameColumn, "Name");
table.addColumn(addressColumn, "Address");
/* Create a data provider */
CustomDataProvider dataProvider = new CustomDataProvider();
/* Connect the dataGrid to the data provider. */
dataProvider.addDataDisplay(dataGrid);
/* Initialization of the "Add entry" button */
bAdd = new Button("Add entry", new ClickHandler() {
public void onClick(ClickEvent event) {
/* SOME CODE FOR ADDING A NEW ENTRY */
}
});
/* Initialization of dataGridWidget */
initWidget(dataGridWidget.createAndBindUi(this));
}
}
/**
*具有异步数据传输的自定义DataGridWidget。
*/
公共类DataGridWidget实现入口点{
/*UI模板的路径*/
@UiTemplate(“ui/DataGridWidget.ui.xml”)
/*使用UiBinder的接口*/
接口IDataGridWidget扩展UiBinder{
}
/*IDataGridWidget接口的实例,用于此小部件的下一次初始化*/
私有静态IDataGridWidget dataGridWidget=GWT.create(IDataGridWidget.class);
/*用于以异步模式从服务器获取数据的服务类实例*/
受保护的静态gettingService同步gettingService=GWT.create(gettingService.class);
/*数据网格*/
@UiField(提供的=真)
数据网格;
/*“添加条目”按钮*/
@UiField(提供的=真)
按钮添加;
/**
*入口点法。
*/
moduleload()上的公共void{
/*数据网格的初始化*/
dataGrid=新的dataGrid();
/*创建名称列*/
TextColumn name column=新建TextColumn(){
@凌驾
公共字符串getValue(联系人){
返回contact.name;
}
};
/*创建地址列*/
TextColumn addressColumn=新的TextColumn(){
@凌驾
公共字符串getValue(联系人){
返回联系人地址;
}
};
/*添加列*/
表.addColumn(名称栏,“名称”);
表.addColumn(addressColumn,“地址”);
/*创建数据提供程序*/
CustomDataProvider dataProvider=新CustomDataProvider();
/*将dataGrid连接到数据提供程序*/
dataProvider.addDataDisplay(dataGrid);
/*“添加条目”按钮的初始化*/
bAdd=新建按钮(“添加条目”,新建ClickHandler()){
公共作废一次点击(点击事件){
/*一些用于添加新条目的代码*/
}
});
/*dataGridWidget的初始化*/
initWidget(dataGridWidget.createAndBindUi(this));
}
}
对于自定义AsyncDataProvider,我有以下代码:
/**
* Custom AsyncDataProvider for transferring data from the server.
*/
protected class CustomDataProvider extends AsyncDataProvider<Contact> {
@Override
protected void onRangeChanged(HasData<Contact> display) {
/* Get a new Range */
final Range range = display.getVisibleRange();
/* Start of range */
final int start = range.getStart();
/* How much entries we will get */
int length = range.getLength();
/* AsyncCallback for retreiveing entries form the server */
gettingService.getRangeContacts(start, length, new AsyncCallback<List<Contact>>() {
public void onSuccess(List<Contact> result) {
/* Updating data into the dataGrid */
updateRowData(start, result);
}
public void onFailure(Throwable caught) {
Window.alert("Error transferring data from the server");
}
});
}
}
/**
*用于从服务器传输数据的自定义AsyncDataProvider。
*/
受保护的类CustomDataProvider扩展了AsyncDataProvider{
@凌驾
受保护的无效onRangeChanged(HasData显示){
/*获得一个新的范围*/
最终范围=display.getVisibleRange();
/*射程起点*/
final int start=range.getStart();
/*我们将获得多少参赛作品*/
int length=range.getLength();
/*用于从服务器检索条目的异步回调*/
gettingService.getRangeContacts(开始、长度、新AsyncCallback(){
成功时公开作废(列表结果){
/*将数据更新到dataGrid中*/
updateRowData(开始、结果);
}
失败时的公共无效(可丢弃){
Window.alert(“从服务器传输数据时出错”);
}
});
}
}
也许有人有办法?谢谢。-1,谢谢你的提问。您需要进一步解释使用代码示例试图实现的目标。代码中的错误到底是什么?好的。用例:用户按下名为“AddEntry”的按钮,然后DataGrid访问一个空字段条目。就这些。我认为没有必要从GWT javadoc中给出DataGrid的一个示例。谢谢。我认为没有脏代码是不可能的。再见,格温。我将带着模块进入下一步。如果您理解DataGrid,您将发现这个问题已经完成。标记-1的人不理解问题,因此发现问题不完整。这个问题是完整和全面的,不需要进一步说明。