You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
4.4 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. This file is part of Ext JS 3.4
  3. Copyright (c) 2011-2013 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
  7. published by the Free Software Foundation and appearing in the file LICENSE included in the
  8. packaging of this file.
  9. Please review the following information to ensure the GNU General Public License version 3.0
  10. requirements will be met: http://www.gnu.org/copyleft/gpl.html.
  11. If you are unsure which license is appropriate for your use, please contact the sales department
  12. at http://www.sencha.com/contact.
  13. Build date: 2013-04-03 15:07:25
  14. */
  15. // Application instance for showing user-feedback messages.
  16. var App = new Ext.App({});
  17. // Create a standard HttpProxy instance.
  18. var proxy = new Ext.data.HttpProxy({
  19. url: 'app.php/users'
  20. });
  21. // Typical JsonReader. Notice additional meta-data params for defining the core attributes of your json-response
  22. var reader = new Ext.data.JsonReader({
  23. totalProperty: 'total',
  24. successProperty: 'success',
  25. idProperty: 'id',
  26. root: 'data',
  27. messageProperty: 'message' // <-- New "messageProperty" meta-data
  28. }, [
  29. {name: 'id'},
  30. {name: 'email', allowBlank: false},
  31. {name: 'first', allowBlank: false},
  32. {name: 'last', allowBlank: false}
  33. ]);
  34. // The new DataWriter component.
  35. var writer = new Ext.data.JsonWriter({
  36. encode: false // <-- don't return encoded JSON -- causes Ext.Ajax#request to send data using jsonData config rather than HTTP params
  37. });
  38. // Typical Store collecting the Proxy, Reader and Writer together.
  39. var store = new Ext.data.Store({
  40. id: 'user',
  41. restful: true, // <-- This Store is RESTful
  42. proxy: proxy,
  43. reader: reader,
  44. writer: writer // <-- plug a DataWriter into the store just as you would a Reader
  45. });
  46. // load the store immeditately
  47. store.load();
  48. ////
  49. // ***New*** centralized listening of DataProxy events "beforewrite", "write" and "writeexception"
  50. // upon Ext.data.DataProxy class. This is handy for centralizing user-feedback messaging into one place rather than
  51. // attaching listenrs to EACH Store.
  52. //
  53. // Listen to all DataProxy beforewrite events
  54. //
  55. Ext.data.DataProxy.addListener('beforewrite', function(proxy, action) {
  56. App.setAlert(App.STATUS_NOTICE, "Before " + action);
  57. });
  58. ////
  59. // all write events
  60. //
  61. Ext.data.DataProxy.addListener('write', function(proxy, action, result, res, rs) {
  62. App.setAlert(true, action + ':' + res.message);
  63. });
  64. ////
  65. // all exception events
  66. //
  67. Ext.data.DataProxy.addListener('exception', function(proxy, type, action, options, res) {
  68. App.setAlert(false, "Something bad happend while executing " + action);
  69. });
  70. // Let's pretend we rendered our grid-columns with meta-data from our ORM framework.
  71. var userColumns = [
  72. {header: "ID", width: 40, sortable: true, dataIndex: 'id'},
  73. {header: "Email", width: 100, sortable: true, dataIndex: 'email', editor: new Ext.form.TextField({})},
  74. {header: "First", width: 50, sortable: true, dataIndex: 'first', editor: new Ext.form.TextField({})},
  75. {header: "Last", width: 50, sortable: true, dataIndex: 'last', editor: new Ext.form.TextField({})}
  76. ];
  77. Ext.onReady(function() {
  78. Ext.QuickTips.init();
  79. // use RowEditor for editing
  80. var editor = new Ext.ux.grid.RowEditor({
  81. saveText: 'Update'
  82. });
  83. // Create a typical GridPanel with RowEditor plugin
  84. var userGrid = new Ext.grid.GridPanel({
  85. renderTo: 'user-grid',
  86. iconCls: 'icon-grid',
  87. frame: true,
  88. title: 'Users',
  89. height: 300,
  90. store: store,
  91. plugins: [editor],
  92. columns : userColumns,
  93. tbar: [{
  94. text: 'Add',
  95. iconCls: 'silk-add',
  96. handler: onAdd
  97. }, '-', {
  98. text: 'Delete',
  99. iconCls: 'silk-delete',
  100. handler: onDelete
  101. }, '-'],
  102. viewConfig: {
  103. forceFit: true
  104. }
  105. });
  106. /**
  107. * onAdd
  108. */
  109. function onAdd(btn, ev) {
  110. var u = new userGrid.store.recordType({
  111. first : '',
  112. last: '',
  113. email : ''
  114. });
  115. editor.stopEditing();
  116. userGrid.store.insert(0, u);
  117. editor.startEditing(0);
  118. }
  119. /**
  120. * onDelete
  121. */
  122. function onDelete() {
  123. var rec = userGrid.getSelectionModel().getSelected();
  124. if (!rec) {
  125. return false;
  126. }
  127. userGrid.store.remove(rec);
  128. }
  129. });