如何在GWT DataGrid中添加新条目?

时间:2011-11-18 21:39:40

标签: gwt datagrid

我试图找到将新条目添加到GWT DataGrid小部件中的任何解决方案,但它没有成功。

我有一个自定义DataGrid小部件的以下代码:

/**
 * 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));
    }

}

我有一个自定义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");
            }
        });
    }

} 

也许有人有解决方案?感谢。

0 个答案:

没有答案