The Grid itself is just a Render. You have to work directly inside a Store.
In your store you have a Loader, that is responsible to load the Store Data. This is easy, just implement a RpcProxy and you are done.
To work with modifications, like insert, update and delete, you must implement the way is more easy to you.
I'll explain my implementation
I have a object MyStore, that extends a ListStore<ModelData>. In this store I set a Loader(With a RpcProxy), so it can be loaded without any work.
To insert a record I have a Toolbar with a New Record button that call this method in MyStore:
Code:
public ModelData createNewRecord()
{
final ModelData modelData = new ModelData();
this.store.insert(modelData, 0);
// I create a field(That I use as Primary Key) just to flag this modelData as Dirty, so I know it's a new one.
final Record record = this.store.getRecord(modelData);
record.beginEdit();
record.set(RecordConstant.PRIMARY_KEY, null);
record.endEdit();
return modelData;
}
This will create an empty ModelData, and will return it, so I can start editing by doing this(I use a Row Editor):
Code:
int modelIndex = this.store.indexOf(this.store.createNewRecord());
this.rowEditor.startEditing(modelIndex, false);
Well, this will start editing this new record.
To save the modified records I do this(I use the Command Pattern, so I wrap the action inside an Action object):
Code:
public void saveModifiedRecords(final AsyncCallback<Integer> callback)
{
if (callback == null)
{
throw new IllegalArgumentException("callback cannot be null!");
}
final List<Action<?>> actions = new ArrayList<Action<?>>();
// Create an SaveStoreAction for every modified Record
final List<Record> modifiedRecords = this.store.getModifiedRecords();
if (modifiedRecords.size() > 0)
{
for (final Record record : modifiedRecords)
{
final int documentId = this.document.getId();
actions.add(new SaveStoreAction(documentId, this.parentModelData, (ModelData) record.getModel()));
}
// Create a Batch Action
final BatchAction batchAction = new BatchAction(OnExceptionType.IGNORE, actions);
this.actionService.executeAction(batchAction, new AsyncCallback<BatchResponse>()
{
public void onFailure(final Throwable caught)
{
// Action failed!
callback.onFailure(caught);
}
public void onSuccess(final BatchResponse result)
{
int unsavedRecords = 0;
for (int i = 0; i < result.getResponses().size(); i++)
{
final SaveStoreResponse saveResponse = (SaveStoreResponse) result.getResponses().get(i);
final Exception saveException = result.getExceptions().get(i);
// If no Exception, commit the modification
if (saveException == null)
{
final Record record = modifiedRecords.get(i);
record.commit(false);
}
else
{
unsavedRecords++;
}
}
callback.onSuccess(unsavedRecords);
}
});
}
else
{
// No Modified Records to save!
}
}
This method when called, will create a Action object for every Modified Record in the Store, this Action is dispatched to the Server, that will update the Hibernate Objects based on the ModelData and will commit to Database, if an error occurs, the Action will return it to Client so the Client knows that that record has not been saved.
The deletion is basically the same, but you send the "this.grid.getSelectionModel().getSelectedItem();" to the Server.
Hope it helps.