array-grid.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. Ext.require([
  2. 'Ext.grid.*',
  3. 'Ext.data.*',
  4. 'Ext.util.*',
  5. 'Ext.state.*'
  6. ]);
  7. // Define Company entity
  8. // Null out built in convert functions for performance *because the raw data is known to be valid*
  9. // Specifying defaultValue as undefined will also save code. *As long as there will always be values in the data, or the app tolerates undefined field values*
  10. Ext.define('Company', {
  11. extend: 'Ext.data.Model',
  12. fields: [
  13. {name: 'company'},
  14. {name: 'price', type: 'float', convert: null, defaultValue: undefined},
  15. {name: 'change', type: 'float', convert: null, defaultValue: undefined},
  16. {name: 'pctChange', type: 'float', convert: null, defaultValue: undefined},
  17. {name: 'lastChange', type: 'date', dateFormat: 'n/j h:ia', defaultValue: undefined}
  18. ],
  19. idProperty: 'company'
  20. });
  21. Ext.onReady(function() {
  22. Ext.QuickTips.init();
  23. // setup the state provider, all state information will be saved to a cookie
  24. Ext.state.Manager.setProvider(Ext.create('Ext.state.CookieProvider'));
  25. // sample static data for the store
  26. var myData = [
  27. ['3m Co', 71.72, 0.02, 0.03, '9/1 12:00am'],
  28. ['Alcoa Inc', 29.01, 0.42, 1.47, '9/1 12:00am'],
  29. ['Altria Group Inc', 83.81, 0.28, 0.34, '9/1 12:00am'],
  30. ['American Express Company', 52.55, 0.01, 0.02, '9/1 12:00am'],
  31. ['American International Group, Inc.', 64.13, 0.31, 0.49, '9/1 12:00am'],
  32. ['AT&T Inc.', 31.61, -0.48, -1.54, '9/1 12:00am'],
  33. ['Boeing Co.', 75.43, 0.53, 0.71, '9/1 12:00am'],
  34. ['Caterpillar Inc.', 67.27, 0.92, 1.39, '9/1 12:00am'],
  35. ['Citigroup, Inc.', 49.37, 0.02, 0.04, '9/1 12:00am'],
  36. ['E.I. du Pont de Nemours and Company', 40.48, 0.51, 1.28, '9/1 12:00am'],
  37. ['Exxon Mobil Corp', 68.1, -0.43, -0.64, '9/1 12:00am'],
  38. ['General Electric Company', 34.14, -0.08, -0.23, '9/1 12:00am'],
  39. ['General Motors Corporation', 30.27, 1.09, 3.74, '9/1 12:00am'],
  40. ['Hewlett-Packard Co.', 36.53, -0.03, -0.08, '9/1 12:00am'],
  41. ['Honeywell Intl Inc', 38.77, 0.05, 0.13, '9/1 12:00am'],
  42. ['Intel Corporation', 19.88, 0.31, 1.58, '9/1 12:00am'],
  43. ['International Business Machines', 81.41, 0.44, 0.54, '9/1 12:00am'],
  44. ['Johnson & Johnson', 64.72, 0.06, 0.09, '9/1 12:00am'],
  45. ['JP Morgan & Chase & Co', 45.73, 0.07, 0.15, '9/1 12:00am'],
  46. ['McDonald\'s Corporation', 36.76, 0.86, 2.40, '9/1 12:00am'],
  47. ['Merck & Co., Inc.', 40.96, 0.41, 1.01, '9/1 12:00am'],
  48. ['Microsoft Corporation', 25.84, 0.14, 0.54, '9/1 12:00am'],
  49. ['Pfizer Inc', 27.96, 0.4, 1.45, '9/1 12:00am'],
  50. ['The Coca-Cola Company', 45.07, 0.26, 0.58, '9/1 12:00am'],
  51. ['The Home Depot, Inc.', 34.64, 0.35, 1.02, '9/1 12:00am'],
  52. ['The Procter & Gamble Company', 61.91, 0.01, 0.02, '9/1 12:00am'],
  53. ['United Technologies Corporation', 63.26, 0.55, 0.88, '9/1 12:00am'],
  54. ['Verizon Communications', 35.57, 0.39, 1.11, '9/1 12:00am'],
  55. ['Wal-Mart Stores, Inc.', 45.45, 0.73, 1.63, '9/1 12:00am']
  56. ];
  57. /**
  58. * Custom function used for column renderer
  59. * @param {Object} val
  60. */
  61. function change(val) {
  62. if (val > 0) {
  63. return '<span style="color:green;">' + val + '</span>';
  64. } else if (val < 0) {
  65. return '<span style="color:red;">' + val + '</span>';
  66. }
  67. return val;
  68. }
  69. /**
  70. * Custom function used for column renderer
  71. * @param {Object} val
  72. */
  73. function pctChange(val) {
  74. if (val > 0) {
  75. return '<span style="color:green;">' + val + '%</span>';
  76. } else if (val < 0) {
  77. return '<span style="color:red;">' + val + '%</span>';
  78. }
  79. return val;
  80. }
  81. // create the data store
  82. var store = Ext.create('Ext.data.ArrayStore', {
  83. model: 'Company',
  84. data: myData
  85. });
  86. // create the Grid
  87. var grid = Ext.create('Ext.grid.Panel', {
  88. store: store,
  89. stateful: true,
  90. collapsible: true,
  91. multiSelect: true,
  92. stateId: 'stateGrid',
  93. columns: [
  94. {
  95. text : 'Company',
  96. flex : 1,
  97. sortable : false,
  98. dataIndex: 'company'
  99. },
  100. {
  101. text : 'Price',
  102. width : 75,
  103. sortable : true,
  104. renderer : 'usMoney',
  105. dataIndex: 'price'
  106. },
  107. {
  108. text : 'Change',
  109. width : 75,
  110. sortable : true,
  111. renderer : change,
  112. dataIndex: 'change'
  113. },
  114. {
  115. text : '% Change',
  116. width : 75,
  117. sortable : true,
  118. renderer : pctChange,
  119. dataIndex: 'pctChange'
  120. },
  121. {
  122. text : 'Last Updated',
  123. width : 85,
  124. sortable : true,
  125. renderer : Ext.util.Format.dateRenderer('m/d/Y'),
  126. dataIndex: 'lastChange'
  127. },
  128. {
  129. menuDisabled: true,
  130. sortable: false,
  131. xtype: 'actioncolumn',
  132. width: 50,
  133. items: [{
  134. icon : '../shared/icons/fam/delete.gif', // Use a URL in the icon config
  135. tooltip: 'Sell stock',
  136. handler: function(grid, rowIndex, colIndex) {
  137. var rec = store.getAt(rowIndex);
  138. alert("Sell " + rec.get('company'));
  139. }
  140. }, {
  141. getClass: function(v, meta, rec) { // Or return a class from a function
  142. if (rec.get('change') < 0) {
  143. this.items[1].tooltip = 'Hold stock';
  144. return 'alert-col';
  145. } else {
  146. this.items[1].tooltip = 'Buy stock';
  147. return 'buy-col';
  148. }
  149. },
  150. handler: function(grid, rowIndex, colIndex) {
  151. var rec = store.getAt(rowIndex);
  152. alert((rec.get('change') < 0 ? "Hold " : "Buy ") + rec.get('company'));
  153. }
  154. }]
  155. }
  156. ],
  157. height: 350,
  158. width: 600,
  159. title: 'Array Grid',
  160. renderTo: 'grid-example',
  161. viewConfig: {
  162. stripeRows: true,
  163. enableTextSelection: true
  164. }
  165. });
  166. });