The ExtJS 3.2+ examples include a “RESTful store” which is a very interesting no-code way to get full CRUD behaviour from a GridPanel. Unfortunately the example is not written with a reusable structure - here is the same example, but as a pre-configured class:
Ext.onReady(function() {
Ext.QuickTips.init();
var TheGrid = Ext.extend(Ext.grid.GridPanel, {
title: 'Users',
frame: true,
height: 300,
width: 500,
viewConfig: {
forceFit: true
},
editor: new Ext.ux.grid.RowEditor({
saveText: 'Update'
}),
onAdd: function(btn, ev) {
var u = new this.store.recordType({
first: '',
last: '',
email: ''
});
this.editor.stopEditing();
this.store.insert(0, u);
this.editor.startEditing(0);
},
onDelete: function() {
var rec = this.getSelectionModel().getSelected();
if (rec) {
this.store.remove(rec);
}
},
initComponent: function() {
var proxy = new Ext.data.HttpProxy({
url: 'app.php/users'
});
var reader = new Ext.data.JsonReader({
totalProperty: 'total',
successProperty: 'success',
idProperty: 'id',
root: 'data',
messageProperty: 'message' // attribute in server response for user message...
}, [{
name: 'id'
}, {
name: 'email'
}, {
name: 'first',
allowBlank: false
}, {
name: 'last'
}]);
var writer = new Ext.data.JsonWriter({
encode: false
});
var store = new Ext.data.Store({
restful: true,
proxy: proxy,
reader: reader,
writer: writer
});
var config = {
store: store,
plugins: [this.editor],
columns: [{
header: "ID",
width: 40,
sortable: true,
dataIndex: 'id'
}, {
header: "Email",
width: 100,
sortable: true,
dataIndex: 'email',
editor: new Ext.form.TextField({})
}, {
header: "First",
width: 50,
sortable: true,
dataIndex: 'first',
editor: new Ext.form.TextField({})
}, {
header: "Last",
width: 50,
sortable: true,
dataIndex: 'last',
editor: new Ext.form.TextField({})
}],
tbar: [{
text: 'Add',
iconCls: 'silk-add',
handler: this.onAdd,
scope: this
}, '-', {
text: 'Delete',
iconCls: 'silk-delete',
handler: this.onDelete,
scope: this
}, '-']
};
Ext.apply(this, Ext.apply(this.initialConfig, config));
TheGrid.superclass.initComponent.apply(this, arguments);
},
onRender: function() {
this.store.load();
TheGrid.superclass.onRender.apply(this, arguments);
}
});
Ext.reg('my_grid', TheGrid);
var w = new Ext.Window({
modal: true,
items: {
xtype: 'my_grid',
title: 'Panel 1'
}
});
w.show();
});
