grid.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. This file is a modified version of direct-grid.js found in the Ext JS 4 package.
  3. Copyright (c) 2011 Sencha Inc
  4. Contact: http://www.sencha.com/contact
  5. GNU General Public License Usage
  6. This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file. Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
  7. If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
  8. */
  9. Ext.onReady(function() {
  10. Ext.direct.Manager.addProvider(Ext.app.REMOTING_API);
  11. //added model inside onready
  12. Ext.define('PersonalInfo', {
  13. extend: 'Ext.data.Model',
  14. fields: ['id', 'name', 'address', 'state']
  15. });
  16. //separated store into unique var for guaranteeRange
  17. var store = Ext.create('Ext.data.Store', {
  18. id: 'store',
  19. model: 'PersonalInfo',
  20. autoLoad: true,
  21. proxy: {
  22. type: 'direct',
  23. api: {
  24. create: QueryDatabase.createRecord,
  25. read: QueryDatabase.getResults,
  26. update: QueryDatabase.updateRecords,
  27. destroy: QueryDatabase.destroyRecord
  28. }
  29. }
  30. });
  31. var rowEditing = Ext.create('Ext.grid.plugin.RowEditing', {
  32. clicksToMoveEditor: 1,
  33. autoCancel: false
  34. });
  35. var alphaSpaceTest = /^[-\sa-zA-Z]+$/;
  36. Ext.apply(Ext.form.field.VTypes, {
  37. // vtype validation function
  38. alphaSpace: function(val, field) {
  39. return alphaSpaceTest.test(val);
  40. },
  41. // vtype Text property: The error text to display when the validation function returns false
  42. alphaSpaceText: 'Not a valid state. Must not contain numbers.',
  43. // vtype Mask property: The keystroke filter mask
  44. alphaSpaceMask: /^[-\sa-zA-Z]+$/
  45. });
  46. // create the Grid
  47. var grid = Ext.create('Ext.grid.Panel', {
  48. height: 450,
  49. width: 700,
  50. cls: 'grid',
  51. title: 'Velociraptor Owners',
  52. store: store,
  53. columns: [{
  54. dataIndex: 'id',
  55. width: 50,
  56. text: 'ID'
  57. }, {
  58. dataIndex: 'name',
  59. flex: 1,
  60. text: 'Name',
  61. //pt 2
  62. allowBlank: false,
  63. field: {
  64. type: 'textfield',
  65. allowBlank: false
  66. }
  67. }, {
  68. dataIndex: 'address',
  69. flex: 1.3,
  70. text: 'Address',
  71. //pt 2
  72. allowBlank: false,
  73. field: {
  74. type: 'textfield',
  75. allowBlank: false
  76. }
  77. }, {
  78. dataIndex: 'state',
  79. flex: 0.8,
  80. text: 'State',
  81. //pt 2
  82. allowBlank: false,
  83. field: {
  84. type: 'textfield',
  85. allowBlank: false,
  86. vtype: 'alphaSpace'
  87. }
  88. }],
  89. renderTo: Ext.getBody(),
  90. //pt 2
  91. plugins: [
  92. rowEditing
  93. ],
  94. dockedItems: [{
  95. xtype: 'toolbar',
  96. store: store,
  97. dock: 'bottom',
  98. //creating, add items
  99. items: [{
  100. iconCls: 'add',
  101. text: 'Add',
  102. handler: function() {
  103. rowEditing.cancelEdit();
  104. // create a record
  105. var newRecord = Ext.create('PersonalInfo');
  106. store.insert(0, newRecord);
  107. rowEditing.startEdit(0, 0);
  108. // write about this section in tutorial
  109. var sm = grid.getSelectionModel();
  110. grid.on('edit', function() {
  111. var record = sm.getSelection()
  112. store.sync();
  113. store.remove(record);
  114. store.load();
  115. });
  116. }
  117. }, {
  118. iconCls: 'delete',
  119. text: 'Delete',
  120. handler: function() {
  121. rowEditing.cancelEdit();
  122. var sm = grid.getSelectionModel();
  123. Ext.Msg.show({
  124. title:'Delete Record?',
  125. msg: 'You are deleting a record permanently, this cannot be undone. Proceed?',
  126. buttons: Ext.Msg.YESNO,
  127. icon: Ext.Msg.QUESTION,
  128. fn: function(btn){
  129. if(btn === 'yes') {
  130. store.remove(sm.getSelection());
  131. store.sync();
  132. }
  133. }
  134. });
  135. }
  136. }]
  137. }]
  138. });
  139. });