form-grid.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Ext JS Library 2.0 Copyright(c) 2006-2007, Ext JS, LLC. licensing@extjs.com
  3. *
  4. * http://extjs.com/license
  5. */
  6. Ext.onReady(function() {
  7. Ext.QuickTips.init();
  8. // turn on validation errors beside the field globally
  9. Ext.form.Field.prototype.msgTarget = 'side';
  10. var bd = Ext.getBody();
  11. // Define the Grid data and create the Grid
  12. var myData = [
  13. ['3m Co', 71.72, 0.02, 0.03, '9/1 12:00am'],
  14. ['Alcoa Inc', 29.01, 0.42, 1.47, '9/1 12:00am'],
  15. ['Altria Group Inc', 83.81, 0.28, 0.34, '9/1 12:00am'],
  16. ['American Express Company', 52.55, 0.01, 0.02,
  17. '9/1 12:00am'],
  18. ['American International Group, Inc.', 64.13, 0.31, 0.49,
  19. '9/1 12:00am'],
  20. ['AT&T Inc.', 31.61, -0.48, -1.54, '9/1 12:00am'],
  21. ['Boeing Co.', 75.43, 0.53, 0.71, '9/1 12:00am'],
  22. ['Caterpillar Inc.', 67.27, 0.92, 1.39, '9/1 12:00am'],
  23. ['Citigroup, Inc.', 49.37, 0.02, 0.04, '9/1 12:00am'],
  24. ['E.I. du Pont de Nemours and Company', 40.48, 0.51, 1.28,
  25. '9/1 12:00am'],
  26. ['Exxon Mobil Corp', 68.1, -0.43, -0.64, '9/1 12:00am'],
  27. ['General Electric Company', 34.14, -0.08, -0.23,
  28. '9/1 12:00am'],
  29. ['General Motors Corporation', 30.27, 1.09, 3.74,
  30. '9/1 12:00am'],
  31. ['Hewlett-Packard Co.', 36.53, -0.03, -0.08, '9/1 12:00am'],
  32. ['Honeywell Intl Inc', 38.77, 0.05, 0.13, '9/1 12:00am'],
  33. ['Intel Corporation', 19.88, 0.31, 1.58, '9/1 12:00am'],
  34. ['International Business Machines', 81.41, 0.44, 0.54,
  35. '9/1 12:00am'],
  36. ['Johnson & Johnson', 64.72, 0.06, 0.09, '9/1 12:00am'],
  37. ['JP Morgan & Chase & Co', 45.73, 0.07, 0.15, '9/1 12:00am'],
  38. ['McDonald\'s Corporation', 36.76, 0.86, 2.40,
  39. '9/1 12:00am'],
  40. ['Merck & Co., Inc.', 40.96, 0.41, 1.01, '9/1 12:00am'],
  41. ['Microsoft Corporation', 25.84, 0.14, 0.54, '9/1 12:00am'],
  42. ['Pfizer Inc', 27.96, 0.4, 1.45, '9/1 12:00am'],
  43. ['The Coca-Cola Company', 45.07, 0.26, 0.58, '9/1 12:00am'],
  44. ['The Home Depot, Inc.', 34.64, 0.35, 1.02, '9/1 12:00am'],
  45. ['The Procter & Gamble Company', 61.91, 0.01, 0.02,
  46. '9/1 12:00am'],
  47. ['United Technologies Corporation', 63.26, 0.55, 0.88,
  48. '9/1 12:00am'],
  49. ['Verizon Communications', 35.57, 0.39, 1.11, '9/1 12:00am'],
  50. ['Wal-Mart Stores, Inc.', 45.45, 0.73, 1.63, '9/1 12:00am']];
  51. var ds = new Ext.data.Store({
  52. reader : new Ext.data.ArrayReader({}, [{
  53. name : 'company'
  54. }, {
  55. name : 'price',
  56. type : 'float'
  57. }, {
  58. name : 'change',
  59. type : 'float'
  60. }, {
  61. name : 'pctChange',
  62. type : 'float'
  63. }, {
  64. name : 'lastChange',
  65. type : 'date',
  66. dateFormat : 'n/j h:ia'
  67. }])
  68. });
  69. ds.loadData(myData);
  70. // example of custom renderer function
  71. function italic(value) {
  72. return '<i>' + value + '</i>';
  73. }
  74. // example of custom renderer function
  75. function change(val) {
  76. if (val > 0) {
  77. return '<span style="color:green;">' + val + '</span>';
  78. } else if (val < 0) {
  79. return '<span style="color:red;">' + val + '</span>';
  80. }
  81. return val;
  82. }
  83. // example of custom renderer function
  84. function pctChange(val) {
  85. if (val > 0) {
  86. return '<span style="color:green;">' + val + '%</span>';
  87. } else if (val < 0) {
  88. return '<span style="color:red;">' + val + '%</span>';
  89. }
  90. return val;
  91. }
  92. // the DefaultColumnModel expects this blob to define columns. It
  93. // can be extended to provide
  94. // custom or reusable ColumnModels
  95. var colModel = new Ext.grid.ColumnModel([{
  96. id : 'company',
  97. header : "Company",
  98. width : 160,
  99. sortable : true,
  100. locked : false,
  101. dataIndex : 'company'
  102. }, {
  103. header : "Price",
  104. width : 75,
  105. sortable : true,
  106. renderer : Ext.util.Format.usMoney,
  107. dataIndex : 'price'
  108. }, {
  109. header : "Change",
  110. width : 75,
  111. sortable : true,
  112. renderer : change,
  113. dataIndex : 'change'
  114. }, {
  115. header : "% Change",
  116. width : 75,
  117. sortable : true,
  118. renderer : pctChange,
  119. dataIndex : 'pctChange'
  120. }, {
  121. header : "Last Updated",
  122. width : 85,
  123. sortable : true,
  124. renderer : Ext.util.Format.dateRenderer('m/d/Y'),
  125. dataIndex : 'lastChange'
  126. }]);
  127. bd.createChild({
  128. tag : 'h2',
  129. html : 'Using a Grid with a Form'
  130. });
  131. /*
  132. * Here is where we create the Form
  133. */
  134. var gridForm = new Ext.FormPanel({
  135. id : 'company-form',
  136. frame : true,
  137. labelAlign : 'left',
  138. title : 'Company data',
  139. bodyStyle : 'padding:5px',
  140. width : 750,
  141. layout : 'column', // Specifies that the items will now
  142. // be arranged in columns
  143. items : [{
  144. columnWidth : 0.6,
  145. layout : 'fit',
  146. items : {
  147. xtype : 'grid',
  148. ds : ds,
  149. cm : colModel,
  150. sm : new Ext.grid.RowSelectionModel({
  151. singleSelect : true,
  152. listeners : {
  153. rowselect : function(sm, row,
  154. rec) {
  155. Ext.getCmp("company-form")
  156. .getForm()
  157. .loadRecord(rec);
  158. }
  159. }
  160. }),
  161. autoExpandColumn : 'company',
  162. height : 350,
  163. title : 'Company Data',
  164. border : true,
  165. listeners : {
  166. render : function(g) {
  167. g.getSelectionModel().selectRow(0);
  168. },
  169. delay : 10
  170. // Allow rows to be rendered.
  171. }
  172. }
  173. }, {
  174. columnWidth : 0.4,
  175. xtype : 'fieldset',
  176. labelWidth : 90,
  177. title : 'Company details',
  178. defaults : {
  179. width : 140
  180. }, // Default config options for child items
  181. defaultType : 'textfield',
  182. autoHeight : true,
  183. bodyStyle : Ext.isIE
  184. ? 'padding:0 0 5px 15px;'
  185. : 'padding:10px 15px;',
  186. border : false,
  187. style : {
  188. "margin-left" : "10px", // when you add custom
  189. // margin in IE 6...
  190. "margin-right" : Ext.isIE6 ? (Ext.isStrict
  191. ? "-10px"
  192. : "-13px") : "0" // you have to
  193. // adjust for it
  194. // somewhere else
  195. },
  196. items : [{
  197. fieldLabel : 'Name',
  198. name : 'company'
  199. }, {
  200. fieldLabel : 'Price',
  201. name : 'price'
  202. }, {
  203. fieldLabel : '% Change',
  204. name : 'pctChange'
  205. }, {
  206. xtype : 'datefield',
  207. fieldLabel : 'Last Updated',
  208. name : 'lastChange'
  209. }]
  210. }],
  211. renderTo : bd
  212. });
  213. // Create Panel view code. Ignore.
  214. createCodePanel('form-grid.js', 'View code to create this Form');
  215. });