extgrid-json.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. Ext.require(['Ext.data.*', 'Ext.grid.*']);
  2. Ext.define('Person', {
  3. extend: 'Ext.data.Model',
  4. fields: [{
  5. name: 'ID',
  6. type: 'int',
  7. useNull: true
  8. }, 'LOGIN', 'NAME', 'EMAIL'],
  9. validations: [{
  10. type: 'length',
  11. field: 'LOGIN',
  12. min: 1
  13. }, {
  14. type: 'length',
  15. field: 'NAME',
  16. min: 1
  17. }, {
  18. type: 'length',
  19. field: 'EMAIL',
  20. min: 1
  21. }]
  22. });
  23. Ext.onReady(function(){
  24. var store = Ext.create('Ext.data.Store', {
  25. autoLoad: true,
  26. autoSync: true,
  27. model: 'Person',
  28. proxy: {
  29. type: 'ajax',
  30. api: {
  31. read: "/cgi-bin/extgrid.exe/Provider/Read/",
  32. update: "/cgi-bin/extgrid.exe/Provider/Update/",
  33. create: "/cgi-bin/extgrid.exe/Provider/Insert/",
  34. destroy: "/cgi-bin/extgrid.exe/Provider/Delete/"
  35. },
  36. reader: {
  37. type: 'json',
  38. successProperty: 'success',
  39. root: 'rows',
  40. messageProperty: 'message'
  41. },
  42. writer: {
  43. type: 'json',
  44. encode: true,
  45. //writeAllFields: false,
  46. root: 'rows'
  47. }
  48. },
  49. listeners: {
  50. write: function(store, operation){
  51. var record = operation.getRecords()[0],
  52. name = Ext.String.capitalize(operation.action),
  53. verb;
  54. if (name == 'Destroy') {
  55. record = operation.records[0];
  56. verb = 'delete';
  57. } else if (name == 'Update') {
  58. verb = 'update';
  59. } else if (name == 'Create') {
  60. verb = 'create';
  61. }
  62. Ext.example.msg(verb, Ext.String.format("{0} user: {1}", verb, record.get('NAME')));
  63. }
  64. }
  65. });
  66. var rowEditing = Ext.create('Ext.grid.plugin.RowEditing');
  67. var grid = Ext.create('Ext.grid.Panel', {
  68. renderTo: document.body,
  69. plugins: [rowEditing],
  70. width: 400,
  71. height: 300,
  72. frame: true,
  73. title: 'Users',
  74. store: store,
  75. iconCls: 'icon-user',
  76. columns: [
  77. {
  78. text: 'LOGIN',
  79. flex: 1,
  80. sortable: true,
  81. dataIndex: 'LOGIN',
  82. field: {
  83. xtype: 'textfield'
  84. }
  85. }, {
  86. header: 'NAME',
  87. width: 80,
  88. sortable: true,
  89. dataIndex: 'NAME',
  90. field: {
  91. xtype: 'textfield'
  92. }
  93. }, {
  94. text: 'EMAIL',
  95. width: 80,
  96. sortable: true,
  97. dataIndex: 'EMAIL',
  98. field: {
  99. xtype: 'textfield'
  100. }
  101. }, {
  102. text: 'LASTLOGIN',
  103. width: 80,
  104. sortable: true,
  105. dataIndex: 'LASTLOGIN',
  106. field: {
  107. xtype: 'textfield'
  108. }
  109. }],
  110. dockedItems: [{
  111. xtype: 'toolbar',
  112. items: [{
  113. text: 'Add',
  114. iconCls: 'icon-add',
  115. handler: function(){
  116. // empty record
  117. store.insert(0, new Person());
  118. rowEditing.startEdit(0, 0);
  119. }
  120. }, '-', {
  121. itemId: 'delete',
  122. text: 'Delete',
  123. iconCls: 'icon-delete',
  124. disabled: true,
  125. handler: function(){
  126. var selection = grid.getView().getSelectionModel().getSelection()[0];
  127. if (selection) {
  128. store.remove(selection);
  129. }
  130. }
  131. }]
  132. }]
  133. });
  134. grid.getSelectionModel().on('selectionchange', function(selModel, selections){
  135. grid.down('#delete').setDisabled(selections.length === 0);
  136. });
  137. });