Upgrade to ExtJS 3.0.3 - Released 10/11/2009
[extjs.git] / examples / grid / totals.js
1 /*!
2  * Ext JS Library 3.0.3
3  * Copyright(c) 2006-2009 Ext JS, LLC
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 Ext.onReady(function(){\r
8 \r
9     Ext.QuickTips.init();\r
10 \r
11     var xg = Ext.grid;\r
12 \r
13     var reader = new Ext.data.JsonReader({\r
14         idProperty: 'taskId',\r
15         fields: [\r
16             {name: 'projectId', type: 'int'},\r
17             {name: 'project', type: 'string'},\r
18             {name: 'taskId', type: 'int'},\r
19             {name: 'description', type: 'string'},\r
20             {name: 'estimate', type: 'float'},\r
21             {name: 'rate', type: 'float'},\r
22             {name: 'cost', type: 'float'},\r
23             {name: 'due', type: 'date', dateFormat:'m/d/Y'}\r
24         ]\r
25 \r
26     });\r
27 \r
28     // define a custom summary function\r
29     Ext.ux.grid.GroupSummary.Calculations['totalCost'] = function(v, record, field){\r
30         return v + (record.data.estimate * record.data.rate);\r
31     };\r
32 \r
33         // utilize custom extension for Group Summary\r
34     var summary = new Ext.ux.grid.GroupSummary();\r
35 \r
36     var grid = new xg.EditorGridPanel({\r
37         ds: new Ext.data.GroupingStore({\r
38             reader: reader,\r
39                         // use local data\r
40             data: app.grid.dummyData,\r
41             sortInfo: {field: 'due', direction: 'ASC'},\r
42             groupField: 'project'\r
43         }),\r
44         columns: [\r
45             {\r
46                 id: 'description',\r
47                 header: 'Task',\r
48                 width: 80,\r
49                 sortable: true,\r
50                 dataIndex: 'description',\r
51                 summaryType: 'count',\r
52                 hideable: false,\r
53                 summaryRenderer: function(v, params, data){\r
54                     return ((v === 0 || v > 1) ? '(' + v +' Tasks)' : '(1 Task)');\r
55                 },\r
56                 editor: new Ext.form.TextField({\r
57                    allowBlank: false\r
58                 })\r
59             },{\r
60                 header: 'Project',\r
61                 width: 20,\r
62                 sortable: true,\r
63                 dataIndex: 'project'\r
64             },{\r
65                 header: 'Due Date',\r
66                 width: 25,\r
67                 sortable: true,\r
68                 dataIndex: 'due',\r
69                 summaryType: 'max',\r
70                 renderer: Ext.util.Format.dateRenderer('m/d/Y'),\r
71                 editor: new Ext.form.DateField({\r
72                     format: 'm/d/Y'\r
73                 })\r
74             },{\r
75                 header: 'Estimate',\r
76                 width: 20,\r
77                 sortable: true,\r
78                 dataIndex: 'estimate',\r
79                 summaryType: 'sum',\r
80                 renderer : function(v){\r
81                     return v +' hours';\r
82                 },\r
83                 editor: new Ext.form.NumberField({\r
84                    allowBlank: false,\r
85                    allowNegative: false,\r
86                    style: 'text-align:left'\r
87                 })\r
88             },{\r
89                 header: 'Rate',\r
90                 width: 20,\r
91                 sortable: true,\r
92                 renderer: Ext.util.Format.usMoney,\r
93                 dataIndex: 'rate',\r
94                 summaryType: 'average',\r
95                 editor: new Ext.form.NumberField({\r
96                     allowBlank: false,\r
97                     allowNegative: false,\r
98                     style: 'text-align:left'\r
99                 })\r
100             },{\r
101                 id: 'cost',\r
102                 header: 'Cost',\r
103                 width: 20,\r
104                 sortable: false,\r
105                 groupable: false,\r
106                 renderer: function(v, params, record){\r
107                     return Ext.util.Format.usMoney(record.data.estimate * record.data.rate);\r
108                 },\r
109                 dataIndex: 'cost',\r
110                 summaryType: 'totalCost',\r
111                 summaryRenderer: Ext.util.Format.usMoney\r
112             }\r
113         ],\r
114 \r
115         view: new Ext.grid.GroupingView({\r
116             forceFit: true,\r
117             showGroupName: false,\r
118             enableNoGroups: false,\r
119                         enableGroupingMenu: false,\r
120             hideGroupedColumn: true\r
121         }),\r
122 \r
123         plugins: summary,\r
124 \r
125         tbar : [{\r
126             text: 'Toggle',\r
127             tooltip: 'Toggle the visibility of summary row',\r
128             handler: function(){summary.toggleSummaries();}\r
129         }],\r
130 \r
131         frame: true,\r
132         width: 800,\r
133         height: 450,\r
134         clicksToEdit: 1,\r
135         collapsible: true,\r
136         animCollapse: false,\r
137         trackMouseOver: false,\r
138         //enableColumnMove: false,\r
139         title: 'Sponsored Projects',\r
140         iconCls: 'icon-grid',\r
141         renderTo: document.body\r
142     });\r
143 \r
144 });\r
145 \r
146 // set up namespace for application\r
147 Ext.ns('app.grid');\r
148 // store dummy data in the app namespace\r
149 app.grid.dummyData = [\r
150     {projectId: 100, project: 'Ext Forms: Field Anchoring', taskId: 112, description: 'Integrate 2.0 Forms with 2.0 Layouts', estimate: 6, rate: 150, due:'06/24/2007'},\r
151     {projectId: 100, project: 'Ext Forms: Field Anchoring', taskId: 113, description: 'Implement AnchorLayout', estimate: 4, rate: 150, due:'06/25/2007'},\r
152     {projectId: 100, project: 'Ext Forms: Field Anchoring', taskId: 114, description: 'Add support for multiple types of anchors', estimate: 4, rate: 150, due:'06/27/2007'},\r
153     {projectId: 100, project: 'Ext Forms: Field Anchoring', taskId: 115, description: 'Testing and debugging', estimate: 8, rate: 0, due:'06/29/2007'},\r
154     {projectId: 101, project: 'Ext Grid: Single-level Grouping', taskId: 101, description: 'Add required rendering "hooks" to GridView', estimate: 6, rate: 100, due:'07/01/2007'},\r
155     {projectId: 101, project: 'Ext Grid: Single-level Grouping', taskId: 102, description: 'Extend GridView and override rendering functions', estimate: 6, rate: 100, due:'07/03/2007'},\r
156     {projectId: 101, project: 'Ext Grid: Single-level Grouping', taskId: 103, description: 'Extend Store with grouping functionality', estimate: 4, rate: 100, due:'07/04/2007'},\r
157     {projectId: 101, project: 'Ext Grid: Single-level Grouping', taskId: 121, description: 'Default CSS Styling', estimate: 2, rate: 100, due:'07/05/2007'},\r
158     {projectId: 101, project: 'Ext Grid: Single-level Grouping', taskId: 104, description: 'Testing and debugging', estimate: 6, rate: 100, due:'07/06/2007'},\r
159     {projectId: 102, project: 'Ext Grid: Summary Rows', taskId: 105, description: 'Ext Grid plugin integration', estimate: 4, rate: 125, due:'07/01/2007'},\r
160     {projectId: 102, project: 'Ext Grid: Summary Rows', taskId: 106, description: 'Summary creation during rendering phase', estimate: 4, rate: 125, due:'07/02/2007'},\r
161     {projectId: 102, project: 'Ext Grid: Summary Rows', taskId: 107, description: 'Dynamic summary updates in editor grids', estimate: 6, rate: 125, due:'07/05/2007'},\r
162     {projectId: 102, project: 'Ext Grid: Summary Rows', taskId: 108, description: 'Remote summary integration', estimate: 4, rate: 125, due:'07/05/2007'},\r
163     {projectId: 102, project: 'Ext Grid: Summary Rows', taskId: 109, description: 'Summary renderers and calculators', estimate: 4, rate: 125, due:'07/06/2007'},\r
164     {projectId: 102, project: 'Ext Grid: Summary Rows', taskId: 110, description: 'Integrate summaries with GroupingView', estimate: 10, rate: 125, due:'07/11/2007'},\r
165     {projectId: 102, project: 'Ext Grid: Summary Rows', taskId: 111, description: 'Testing and debugging', estimate: 8, rate: 125, due:'07/15/2007'}\r
166 ];