form-grid.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. Ext.require([
  2. 'Ext.form.*',
  3. 'Ext.data.*',
  4. 'Ext.grid.Panel',
  5. 'Ext.layout.container.Column'
  6. ]);
  7. Ext.onReady(function(){
  8. Ext.QuickTips.init();
  9. var bd = Ext.getBody();
  10. // sample static data for the store
  11. var myData = [
  12. ['3m Co', 71.72, 0.02, 0.03, '9/1 12:00am'],
  13. ['Alcoa Inc', 29.01, 0.42, 1.47, '9/1 12:00am'],
  14. ['Altria Group Inc', 83.81, 0.28, 0.34, '9/1 12:00am'],
  15. ['American Express Company', 52.55, 0.01, 0.02, '9/1 12:00am'],
  16. ['American International Group, Inc.', 64.13, 0.31, 0.49, '9/1 12:00am'],
  17. ['AT&T Inc.', 31.61, -0.48, -1.54, '9/1 12:00am'],
  18. ['Boeing Co.', 75.43, 0.53, 0.71, '9/1 12:00am'],
  19. ['Caterpillar Inc.', 67.27, 0.92, 1.39, '9/1 12:00am'],
  20. ['Citigroup, Inc.', 49.37, 0.02, 0.04, '9/1 12:00am'],
  21. ['E.I. du Pont de Nemours and Company', 40.48, 0.51, 1.28, '9/1 12:00am'],
  22. ['Exxon Mobil Corp', 68.1, -0.43, -0.64, '9/1 12:00am'],
  23. ['General Electric Company', 34.14, -0.08, -0.23, '9/1 12:00am'],
  24. ['General Motors Corporation', 30.27, 1.09, 3.74, '9/1 12:00am'],
  25. ['Hewlett-Packard Co.', 36.53, -0.03, -0.08, '9/1 12:00am'],
  26. ['Honeywell Intl Inc', 38.77, 0.05, 0.13, '9/1 12:00am'],
  27. ['Intel Corporation', 19.88, 0.31, 1.58, '9/1 12:00am'],
  28. ['International Business Machines', 81.41, 0.44, 0.54, '9/1 12:00am'],
  29. ['Johnson & Johnson', 64.72, 0.06, 0.09, '9/1 12:00am'],
  30. ['JP Morgan & Chase & Co', 45.73, 0.07, 0.15, '9/1 12:00am'],
  31. ['McDonald\'s Corporation', 36.76, 0.86, 2.40, '9/1 12:00am'],
  32. ['Merck & Co., Inc.', 40.96, 0.41, 1.01, '9/1 12:00am'],
  33. ['Microsoft Corporation', 25.84, 0.14, 0.54, '9/1 12:00am'],
  34. ['Pfizer Inc', 27.96, 0.4, 1.45, '9/1 12:00am'],
  35. ['The Coca-Cola Company', 45.07, 0.26, 0.58, '9/1 12:00am'],
  36. ['The Home Depot, Inc.', 34.64, 0.35, 1.02, '9/1 12:00am'],
  37. ['The Procter & Gamble Company', 61.91, 0.01, 0.02, '9/1 12:00am'],
  38. ['United Technologies Corporation', 63.26, 0.55, 0.88, '9/1 12:00am'],
  39. ['Verizon Communications', 35.57, 0.39, 1.11, '9/1 12:00am'],
  40. ['Wal-Mart Stores, Inc.', 45.45, 0.73, 1.63, '9/1 12:00am']
  41. ];
  42. var ds = Ext.create('Ext.data.ArrayStore', {
  43. fields: [
  44. {name: 'company'},
  45. {name: 'price', type: 'float'},
  46. {name: 'change', type: 'float'},
  47. {name: 'pctChange', type: 'float'},
  48. {name: 'lastChange', type: 'date', dateFormat: 'n/j h:ia'},
  49. // Rating dependent upon performance 0 = best, 2 = worst
  50. {name: 'rating', type: 'int', convert: function(value, record) {
  51. var pct = record.get('pctChange');
  52. if (pct < 0) return 2;
  53. if (pct < 1) return 1;
  54. return 0;
  55. }}
  56. ],
  57. data: myData
  58. });
  59. // example of custom renderer function
  60. function change(val){
  61. if(val > 0){
  62. return '<span style="color:green;">' + val + '</span>';
  63. }else if(val < 0){
  64. return '<span style="color:red;">' + val + '</span>';
  65. }
  66. return val;
  67. }
  68. // example of custom renderer function
  69. function pctChange(val){
  70. if(val > 0){
  71. return '<span style="color:green;">' + val + '%</span>';
  72. }else if(val < 0){
  73. return '<span style="color:red;">' + val + '%</span>';
  74. }
  75. return val;
  76. }
  77. // render rating as "A", "B" or "C" depending upon numeric value.
  78. function rating(v) {
  79. if (v == 0) return "A";
  80. if (v == 1) return "B";
  81. if (v == 2) return "C";
  82. }
  83. bd.createChild({tag: 'h2', html: 'Using a Grid with a Form'});
  84. /*
  85. * Here is where we create the Form
  86. */
  87. var gridForm = Ext.create('Ext.form.Panel', {
  88. id: 'company-form',
  89. frame: true,
  90. title: 'Company data',
  91. bodyPadding: 5,
  92. width: 750,
  93. layout: 'column', // Specifies that the items will now be arranged in columns
  94. fieldDefaults: {
  95. labelAlign: 'left',
  96. msgTarget: 'side'
  97. },
  98. items: [{
  99. columnWidth: 0.60,
  100. xtype: 'gridpanel',
  101. store: ds,
  102. height: 400,
  103. title:'Company Data',
  104. columns: [
  105. {
  106. id :'company',
  107. text : 'Company',
  108. flex: 1,
  109. sortable : true,
  110. dataIndex: 'company'
  111. },
  112. {
  113. text : 'Price',
  114. width : 75,
  115. sortable : true,
  116. dataIndex: 'price'
  117. },
  118. {
  119. text : 'Change',
  120. width : 75,
  121. sortable : true,
  122. renderer : change,
  123. dataIndex: 'change'
  124. },
  125. {
  126. text : '% Change',
  127. width : 75,
  128. sortable : true,
  129. renderer : pctChange,
  130. dataIndex: 'pctChange'
  131. },
  132. {
  133. text : 'Last Updated',
  134. width : 85,
  135. sortable : true,
  136. renderer : Ext.util.Format.dateRenderer('m/d/Y'),
  137. dataIndex: 'lastChange'
  138. },
  139. {
  140. text: 'Rating',
  141. width: 30,
  142. sortable: true,
  143. renderer: rating,
  144. dataIndex: 'rating'
  145. }
  146. ],
  147. listeners: {
  148. selectionchange: function(model, records) {
  149. if (records[0]) {
  150. this.up('form').getForm().loadRecord(records[0]);
  151. }
  152. }
  153. }
  154. }, {
  155. columnWidth: 0.4,
  156. margin: '0 0 0 10',
  157. xtype: 'fieldset',
  158. title:'Company details',
  159. defaults: {
  160. width: 240,
  161. labelWidth: 90
  162. },
  163. defaultType: 'textfield',
  164. items: [{
  165. fieldLabel: 'Name',
  166. name: 'company'
  167. },{
  168. fieldLabel: 'Price',
  169. name: 'price'
  170. },{
  171. fieldLabel: '% Change',
  172. name: 'pctChange'
  173. },{
  174. xtype: 'datefield',
  175. fieldLabel: 'Last Updated',
  176. name: 'lastChange'
  177. }, {
  178. xtype: 'radiogroup',
  179. fieldLabel: 'Rating',
  180. columns: 3,
  181. defaults: {
  182. name: 'rating' //Each radio has the same name so the browser will make sure only one is checked at once
  183. },
  184. items: [{
  185. inputValue: '0',
  186. boxLabel: 'A'
  187. }, {
  188. inputValue: '1',
  189. boxLabel: 'B'
  190. }, {
  191. inputValue: '2',
  192. boxLabel: 'C'
  193. }]
  194. }]
  195. }],
  196. renderTo: bd
  197. });
  198. gridForm.child('gridpanel').getSelectionModel().select(0);
  199. });