Ext.define('SchoolApp.view.student.StateList', { extend: 'Ext.grid.Panel', requires: ['SchoolApp.model.State'], alias: 'widget.StateList', config: {}, constructor: function (config) { return this.callParent(arguments); }, columnLines: true, selModel:'rowmodel', viewConfig: { stripeRows: true }, plugins: { ptype: 'rowediting', clicksToEdit: 1 }, initComponent: function () { Ext.apply(this, { columns: [{ xtype: "rownumberer" }, { text: "Id", dataIndex: 'Id', hidden: true, width: 35 }, { text: "State Name", flex: 1, dataIndex: "name", editor: 'textfield' }] }); this.callParent(arguments); } });
Ext.define('SchoolApp.view.student.StudentViewModel', { extend: 'Ext.app.ViewModel', alias: 'viewmodel.StudentViewModel', stores: { students: { model: 'SchoolApp.model.Student', session: true, autoLoad: true, sorters: [{ property: 'firstName', direction:'DESC' }] }, states: { model: 'SchoolApp.model.State', session: true, autoLoad: true, sorters: [{ property: 'name', direction: 'ASC' }] } } });
Ext.define('SchoolApp.view.student.StudentList', { extend: 'Ext.grid.Panel', alias: 'widget.StudentList', requires: ['SchoolApp.model.Student'], config: {}, constructor: function (config) { return this.callParent(arguments); }, columnLines: true, selModel:'rowmodel', viewConfig: { stripeRows: true }, plugins: { ptype: 'rowediting', clicksToEdit: 1 }, initComponent: function () { Ext.apply(this, { columns: [{ xtype: "rownumberer" }, { text: "Id", dataIndex: 'Id', hidden: true, width: 35 }, { text: "Student Name", flex: 1, dataIndex: "firstName", editor: 'textfield' }] }); this.callParent(arguments); } });
Ext.define('SchoolApp.model.Student', { extend: 'Ext.data.Model', idProperty: 'Id', schema: { namespace: 'SchoolApp.model', proxy: { type:'ajax', api: { read: '/ExampleService.svc/students/' }, reader:{ type:'json', rootProperty:'data' } } }, fields: [ { name: 'Id', type: 'int' }, { name: 'firstName', type: 'string' }, { name: 'middleName', type: 'string' }, { name: 'lastName', type: 'string' }, { name: 'birthDate', type: 'date' }, { name: 'address1', type: 'string' }, { name: 'address2', type: 'string' }, { name: 'city', type: 'string' }, { name: 'state', type: 'string' } ] });
Ext.define('SchoolApp.model.State', { extend: 'Ext.data.Model', idProperty: 'Id', proxy: { type:'ajax', api: { read: '/ExampleService.svc/students/' }, reader:{ type:'json', rootProperty:'data' }, writer: { allowSingle: false } }, fields: [ { name: 'Id', type: 'int' }, { name: 'name', type: 'string' } ] });
Ext.application({ name: 'SchoolApp', autoCreateViewport: false, views: ['SchoolApp.view.student.StudentList', 'SchoolApp.view.student.StateList'], requires: ['SchoolApp.view.student.StudentViewModel'], launch: function () { Ext.create('Ext.container.Viewport', { layout: 'border', dock: 'top', viewModel: { type: 'StudentViewModel' }, session: true, items: [ { region: 'north', xtype: 'toolbar', items: [{ text: 'Save Batch', handler: function () { try { var viewport = this.up().up(); var ses = viewport.getSession(); var batch = ses.getSaveBatch(); batch.on({ complete: function () { Ext.Msg.alert('Status', 'Data Saved Successfully!'); }, exception: function () { Ext.Msg.alert('Error', 'Error occurred'); } }); batch.start(); } catch (ex) { Ext.Msg.alert('Error', ex.message); } } }, { text: 'Show Changes', handler: function () { var viewport = this.up().up(); var ses = viewport.getSession(); var changes = ses.getChanges(); if (!changes) { Ext.Msg.alert('Status', 'No Changes'); return; } if (changes.State && changes.State.U) // if state updated { var changedStateNames = "Changed State Names: "; for (var i = 0; i < changes.State.U.length ; i++) { changedStateNames += changes.State.U[i].name + ", "; } Ext.Msg.alert('Status', changedStateNames); } if (changes.Student && changes.Student.U) { // if student updated var changedStudentNames = "Changed Student Names: "; for (var i = 0; i < changes.Student.U.length ; i++) { changedStudentNames += changes.Student.U[i].firstName + ", "; } Ext.Msg.alert('Status', changedStudentNames); } } }] }, { region: 'center', xtype: 'StudentList', bind: { store: '{students}' }, flex: 1 }, { xtype: 'StateList', region: 'south', bind: { store: '{states}' }, flex: 1 }] }); } });
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <link href="http://cdn.sencha.com/ext/gpl/5.1.0/packages/ext-theme-neptune/build/resources/ext-theme-neptune-all.css" rel="stylesheet" /> <script src="http://cdn.sencha.com/ext/gpl/5.1.0/build/ext-all-debug.js"></script> <script src="http://cdn.sencha.com/ext/gpl/5.1.0/packages/ext-theme-neptune/build/ext-theme-neptune.js"></script> <script src="app.js"></script> </head> <body> </body> </html>