remove-all-recs.html 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Stateful Array Grid Example</title>
  6. <link rel="stylesheet" type="text/css" href="../../resources/css/ext-all.css" />
  7. <link rel="stylesheet" type="text/css" href="../shared/example.css" />
  8. <!-- GC -->
  9. <script type="text/javascript" src="../../ext.js"></script>
  10. <!-- page specific -->
  11. <style type="text/css">
  12. /* style rows on mouseover */
  13. .x-grid-row-over .x-grid-cell-inner {
  14. font-weight: bold;
  15. }
  16. /* shared styles for the ActionColumn icons */
  17. .x-action-col-cell img {
  18. height: 16px;
  19. width: 16px;
  20. cursor: pointer;
  21. }
  22. /* custom icon for the "buy" ActionColumn icon */
  23. .x-action-col-cell img.buy-col {
  24. background-image: url(../shared/icons/fam/accept.png);
  25. }
  26. /* custom icon for the "alert" ActionColumn icon */
  27. .x-action-col-cell img.alert-col {
  28. background-image: url(../shared/icons/fam/error.png);
  29. }
  30. .x-ie6 .x-action-col-cell img.buy-col {
  31. background-image: url(../shared/icons/fam/accept.gif);
  32. }
  33. .x-ie6.x-action-col-cell img.alert-col {
  34. background-image: url(../shared/icons/fam/error.gif);
  35. }
  36. .x-ie6 .x-action-col-cell img {
  37. position:relative;
  38. top:-1px;
  39. }
  40. </style>
  41. <script type="text/javascript">
  42. Ext.require([
  43. 'Ext.grid.*',
  44. 'Ext.data.*',
  45. 'Ext.util.*',
  46. 'Ext.state.*'
  47. ]);
  48. Ext.onReady(function() {
  49. Ext.QuickTips.init();
  50. // setup the state provider, all state information will be saved to a cookie
  51. Ext.state.Manager.setProvider(Ext.create('Ext.state.CookieProvider'));
  52. // sample static data for the store
  53. var myData = [
  54. ['3m Co', 71.72, 0.02, 0.03, '9/1 12:00am'],
  55. ['Alcoa Inc', 29.01, 0.42, 1.47, '9/1 12:00am'],
  56. ['Altria Group Inc', 83.81, 0.28, 0.34, '9/1 12:00am'],
  57. ['American Express Company', 52.55, 0.01, 0.02, '9/1 12:00am'],
  58. ['American International Group, Inc.', 64.13, 0.31, 0.49, '9/1 12:00am'],
  59. ['AT&T Inc.', 31.61, -0.48, -1.54, '9/1 12:00am'],
  60. ['Boeing Co.', 75.43, 0.53, 0.71, '9/1 12:00am'],
  61. ['Caterpillar Inc.', 67.27, 0.92, 1.39, '9/1 12:00am'],
  62. ['Citigroup, Inc.', 49.37, 0.02, 0.04, '9/1 12:00am'],
  63. ['E.I. du Pont de Nemours and Company', 40.48, 0.51, 1.28, '9/1 12:00am'],
  64. ['Exxon Mobil Corp', 68.1, -0.43, -0.64, '9/1 12:00am'],
  65. ['General Electric Company', 34.14, -0.08, -0.23, '9/1 12:00am'],
  66. ['General Motors Corporation', 30.27, 1.09, 3.74, '9/1 12:00am'],
  67. ['Hewlett-Packard Co.', 36.53, -0.03, -0.08, '9/1 12:00am'],
  68. ['Honeywell Intl Inc', 38.77, 0.05, 0.13, '9/1 12:00am'],
  69. ['Intel Corporation', 19.88, 0.31, 1.58, '9/1 12:00am'],
  70. ['International Business Machines', 81.41, 0.44, 0.54, '9/1 12:00am'],
  71. ['Johnson & Johnson', 64.72, 0.06, 0.09, '9/1 12:00am'],
  72. ['JP Morgan & Chase & Co', 45.73, 0.07, 0.15, '9/1 12:00am'],
  73. ['McDonald\'s Corporation', 36.76, 0.86, 2.40, '9/1 12:00am'],
  74. ['Merck & Co., Inc.', 40.96, 0.41, 1.01, '9/1 12:00am'],
  75. ['Microsoft Corporation', 25.84, 0.14, 0.54, '9/1 12:00am'],
  76. ['Pfizer Inc', 27.96, 0.4, 1.45, '9/1 12:00am'],
  77. ['The Coca-Cola Company', 45.07, 0.26, 0.58, '9/1 12:00am'],
  78. ['The Home Depot, Inc.', 34.64, 0.35, 1.02, '9/1 12:00am'],
  79. ['The Procter & Gamble Company', 61.91, 0.01, 0.02, '9/1 12:00am'],
  80. ['United Technologies Corporation', 63.26, 0.55, 0.88, '9/1 12:00am'],
  81. ['Verizon Communications', 35.57, 0.39, 1.11, '9/1 12:00am'],
  82. ['Wal-Mart Stores, Inc.', 45.45, 0.73, 1.63, '9/1 12:00am']
  83. ];
  84. /**
  85. * Custom function used for column renderer
  86. * @param {Object} val
  87. */
  88. function change(val) {
  89. if (val > 0) {
  90. return '<span style="color:green;">' + val + '</span>';
  91. } else if (val < 0) {
  92. return '<span style="color:red;">' + val + '</span>';
  93. }
  94. return val;
  95. }
  96. /**
  97. * Custom function used for column renderer
  98. * @param {Object} val
  99. */
  100. function pctChange(val) {
  101. if (val > 0) {
  102. return '<span style="color:green;">' + val + '%</span>';
  103. } else if (val < 0) {
  104. return '<span style="color:red;">' + val + '%</span>';
  105. }
  106. return val;
  107. }
  108. // create the data store
  109. var store = Ext.create('Ext.data.ArrayStore', {
  110. fields: [
  111. {name: 'company'},
  112. {name: 'price', type: 'float'},
  113. {name: 'change', type: 'float'},
  114. {name: 'pctChange', type: 'float'},
  115. {name: 'lastChange', type: 'date', dateFormat: 'n/j h:ia'}
  116. ],
  117. data: myData
  118. });
  119. // create the Grid
  120. var grid = Ext.create('Ext.grid.Panel', {
  121. store: store,
  122. stateful: true,
  123. stateId: 'stateGrid',
  124. columns: [
  125. {
  126. text : 'Company',
  127. flex : 1,
  128. sortable : false,
  129. dataIndex: 'company'
  130. },
  131. {
  132. text : 'Price',
  133. width : 75,
  134. sortable : true,
  135. renderer : 'usMoney',
  136. dataIndex: 'price'
  137. },
  138. {
  139. text : 'Change',
  140. width : 75,
  141. sortable : true,
  142. renderer : change,
  143. dataIndex: 'change'
  144. },
  145. {
  146. text : '% Change',
  147. width : 75,
  148. sortable : true,
  149. renderer : pctChange,
  150. dataIndex: 'pctChange'
  151. },
  152. {
  153. text : 'Last Updated',
  154. width : 85,
  155. sortable : true,
  156. renderer : Ext.util.Format.dateRenderer('m/d/Y'),
  157. dataIndex: 'lastChange'
  158. },
  159. {
  160. xtype: 'actioncolumn',
  161. width: 50,
  162. items: [{
  163. icon : '../shared/icons/fam/delete.gif', // Use a URL in the icon config
  164. tooltip: 'Sell stock',
  165. handler: function(grid, rowIndex, colIndex) {
  166. var rec = store.getAt(rowIndex);
  167. alert("Sell " + rec.get('company'));
  168. }
  169. }, {
  170. getClass: function(v, meta, rec) { // Or return a class from a function
  171. if (rec.get('change') < 0) {
  172. this.items[1].tooltip = 'Hold stock';
  173. return 'alert-col';
  174. } else {
  175. this.items[1].tooltip = 'Buy stock';
  176. return 'buy-col';
  177. }
  178. },
  179. handler: function(grid, rowIndex, colIndex) {
  180. var rec = store.getAt(rowIndex);
  181. alert((rec.get('change') < 0 ? "Hold " : "Buy ") + rec.get('company'));
  182. }
  183. }]
  184. }
  185. ],
  186. height: 350,
  187. width: 600,
  188. title: 'Array Grid',
  189. renderTo: document.body,
  190. viewConfig: {
  191. stripeRows: true
  192. }
  193. });
  194. var recs;
  195. var removeButton = Ext.create('Ext.Button', {
  196. text: "Remove all",
  197. renderTo: document.body,
  198. handler: function() {
  199. recs = store.getRange();
  200. store.removeAll();
  201. addButton.enable();
  202. }
  203. });
  204. var addButton = Ext.create('Ext.Button', {
  205. text: 'Add back',
  206. disabled: true,
  207. renderTo: document.body,
  208. handler: function() {
  209. store.add(recs);
  210. removeButton.enable();
  211. }
  212. });
  213. });
  214. </script>
  215. </head>
  216. <body></body>
  217. </html>