Как добавить новую запись в GWT DataGrid? - PullRequest
1 голос
/ 19 ноября 2011

Я пытался найти какое-либо решение для добавления новой записи в виджет 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");
            }
        });
    }

} 

Может быть, у кого-нибудь естьрешение?Спасибо.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...