Upgrade to ExtJS 3.0.0 - Released 07/06/2009
[extjs.git] / docs / source / field-to-grid-dd.html
1 <html>\r
2 <head>\r
3   <title>The source code</title>\r
4     <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />\r
5     <script type="text/javascript" src="../resources/prettify/prettify.js"></script>\r
6 </head>\r
7 <body  onload="prettyPrint();">\r
8     <pre class="prettyprint lang-js">// A DropZone which cooperates with DragZones whose dragData contains\r
9 // a "field" property representing a form Field. Fields may be dropped onto\r
10 // grid data cells containing a matching data type.\r
11 Ext.ux.CellFieldDropZone = Ext.extend(Ext.dd.DropZone, {\r
12     constructor: function(){},\r
13 \r
14 //  Call the DropZone constructor using the View's scrolling element\r
15 //  only after the grid has been rendered.\r
16     init: function(grid) {\r
17         if (grid.rendered) {\r
18             this.grid = grid;\r
19             this.view = grid.getView();\r
20             this.store = grid.getStore();\r
21             Ext.ux.CellFieldDropZone.superclass.constructor.call(this, this.view.scroller);\r
22         } else {\r
23             grid.on('render', this.init, this);\r
24         }\r
25     },\r
26 \r
27 //  Scroll the main configured Element when we drag close to the edge\r
28     containerScroll: true,\r
29 \r
30     getTargetFromEvent: function(e) {\r
31 //      Ascertain whether the mousemove is within a grid cell\r
32         var t = e.getTarget(this.view.cellSelector);\r
33         if (t) {\r
34 \r
35 //          We *are* within a grid cell, so ask the View exactly which one,\r
36 //          Extract data from the Model to create a target object for\r
37 //          processing in subsequent onNodeXXXX methods. Note that the target does\r
38 //          not have to be a DOM element. It can be whatever the noNodeXXX methods are\r
39 //          programmed to expect.\r
40             var rowIndex = this.view.findRowIndex(t);\r
41             var columnIndex = this.view.findCellIndex(t);\r
42             if ((rowIndex !== false) && (columnIndex !== false)) {\r
43                 return {\r
44                     node: t,\r
45                     record: this.store.getAt(rowIndex),\r
46                     fieldName: this.grid.getColumnModel().getDataIndex(columnIndex)\r
47                 }\r
48             }\r
49         }\r
50     },\r
51 \r
52 //  On Node enter, see if it is valid for us to drop the field on that type of column.\r
53     onNodeEnter: function(target, dd, e, dragData) {\r
54         delete this.dropOK;\r
55         if (!target) {\r
56             return;\r
57         }\r
58 \r
59 //      Check that a field is being dragged.\r
60         var f = dragData.field;\r
61         if (!f) {\r
62             return;\r
63         }\r
64 \r
65 //      Check whether the data type of the column being dropped on accepts the\r
66 //      dragged field type. If so, set dropOK flag, and highlight the target node.\r
67         var type = target.record.fields.get(target.fieldName).type;\r
68         switch (type) {\r
69             case 'float':\r
70             case 'int':\r
71                 if (!f.isXType('numberfield')) {\r
72                     return;\r
73                 }\r
74                 break;\r
75             case 'date':\r
76                 if (!f.isXType('datefield')) {\r
77                     return;\r
78                 }\r
79                 break;\r
80             case 'boolean':\r
81                 if (!f.isXType('checkbox')) {\r
82                     return;\r
83                 }\r
84         }\r
85         this.dropOK = true;\r
86         Ext.fly(target.node).addClass('x-drop-target-active');\r
87     },\r
88 \r
89 //  Return the class name to add to the drag proxy. This provides a visual indication\r
90 //  of drop allowed or not allowed.\r
91     onNodeOver: function(target, dd, e, dragData) {\r
92         return this.dropOK ? this.dropAllowed : this.dropNotAllowed;\r
93     },\r
94 \r
95 //   nhighlight the target node.\r
96     onNodeOut: function(target, dd, e, dragData) {\r
97         Ext.fly(target.node).removeClass('x-drop-target-active');\r
98     },\r
99 \r
100 //  Process the drop event if we have previously ascertained that a drop is OK.\r
101     onNodeDrop: function(target, dd, e, dragData) {\r
102         if (this.dropOK) {\r
103             target.record.set(target.fieldName, dragData.field.getValue());\r
104             return true;\r
105         }\r
106     }\r
107 });\r
108 \r
109 //  A class which makes Fields within a Panel draggable.\r
110 //  the dragData delivered to a coooperating DropZone's methods contains\r
111 //  the dragged Field in the property "field".\r
112 Ext.ux.PanelFieldDragZone = Ext.extend(Ext.dd.DragZone, {\r
113     constructor: function(){},\r
114 \r
115 //  Call the DRagZone's constructor. The Panel must have been rendered.\r
116     init: function(panel) {\r
117         if (panel.nodeType) {\r
118             Ext.ux.PanelFieldDragZone.superclass.init.apply(this, arguments);\r
119         } else {\r
120             if (panel.rendered) {\r
121                 Ext.ux.PanelFieldDragZone.superclass.constructor.call(this, panel.getEl());\r
122                 var i = Ext.fly(panel.getEl()).select('input');\r
123                 i.unselectable();\r
124             } else {\r
125                 panel.on('afterlayout', this.init, this, {single: true});\r
126             }\r
127         }\r
128     },\r
129 \r
130     scroll: false,\r
131 \r
132 //  On mousedown, we ascertain whether it is on one of our draggable Fields.\r
133 //  If so, we collect data about the draggable object, and return a drag data\r
134 //  object which contains our own data, plus a "ddel" property which is a DOM\r
135 //  node which provides a "view" of the dragged data.\r
136     getDragData: function(e) {\r
137         var t = e.getTarget('input');\r
138         if (t) {\r
139             e.stopEvent();\r
140 \r
141 //          Ugly code to "detach" the drag gesture from the input field.\r
142 //          Without this, Opera never changes the mouseover target from the input field\r
143 //          even when dragging outside of the field - it just keeps selecting.\r
144             if (Ext.isOpera) {\r
145                 Ext.fly(t).on('mousemove', function(e1){\r
146                     t.style.visibility = 'hidden';\r
147                     (function(){\r
148                         t.style.visibility = '';\r
149                     }).defer(1);\r
150                 }, null, {single:true});\r
151             }\r
152 \r
153 //          Get the data we are dragging: the Field\r
154 //          create a ddel for the drag proxy to display\r
155             var f = Ext.getCmp(t.id);\r
156             var d = document.createElement('div');\r
157             d.className = 'x-form-text';\r
158             d.appendChild(document.createTextNode(t.value));\r
159             Ext.fly(d).setWidth(f.getEl().getWidth());\r
160             return {\r
161                 field: f,\r
162                 ddel: d\r
163             };\r
164         }\r
165     },\r
166 \r
167 //  The coordinates to slide the drag proxy back to on failed drop.\r
168     getRepairXY: function() {\r
169         return this.dragData.field.getEl().getXY();\r
170     }\r
171 });\r
172 \r
173 Ext.onReady(function(){\r
174 \r
175     var myData = [\r
176         ['3m Co',71.72,0.02,0.03,'9/1 12:00am'],\r
177         ['Alcoa Inc',29.01,0.42,1.47,'9/1 12:00am'],\r
178         ['Altria Group Inc',83.81,0.28,0.34,'9/1 12:00am'],\r
179         ['American Express Company',52.55,0.01,0.02,'9/1 12:00am'],\r
180         ['American International Group, Inc.',64.13,0.31,0.49,'9/1 12:00am'],\r
181         ['AT&T Inc.',31.61,-0.48,-1.54,'9/1 12:00am'],\r
182         ['Boeing Co.',75.43,0.53,0.71,'9/1 12:00am'],\r
183         ['Caterpillar Inc.',67.27,0.92,1.39,'9/1 12:00am'],\r
184         ['Citigroup, Inc.',49.37,0.02,0.04,'9/1 12:00am'],\r
185         ['E.I. du Pont de Nemours and Company',40.48,0.51,1.28,'9/1 12:00am'],\r
186         ['Exxon Mobil Corp',68.1,-0.43,-0.64,'9/1 12:00am'],\r
187         ['General Electric Company',34.14,-0.08,-0.23,'9/1 12:00am'],\r
188         ['General Motors Corporation',30.27,1.09,3.74,'9/1 12:00am'],\r
189         ['Hewlett-Packard Co.',36.53,-0.03,-0.08,'9/1 12:00am'],\r
190         ['Honeywell Intl Inc',38.77,0.05,0.13,'9/1 12:00am'],\r
191         ['Intel Corporation',19.88,0.31,1.58,'9/1 12:00am'],\r
192         ['International Business Machines',81.41,0.44,0.54,'9/1 12:00am'],\r
193         ['Johnson & Johnson',64.72,0.06,0.09,'9/1 12:00am'],\r
194         ['JP Morgan & Chase & Co',45.73,0.07,0.15,'9/1 12:00am'],\r
195         ['McDonald\'s Corporation',36.76,0.86,2.40,'9/1 12:00am'],\r
196         ['Merck & Co., Inc.',40.96,0.41,1.01,'9/1 12:00am'],\r
197         ['Microsoft Corporation',25.84,0.14,0.54,'9/1 12:00am'],\r
198         ['Pfizer Inc',27.96,0.4,1.45,'9/1 12:00am'],\r
199         ['The Coca-Cola Company',45.07,0.26,0.58,'9/1 12:00am'],\r
200         ['The Home Depot, Inc.',34.64,0.35,1.02,'9/1 12:00am'],\r
201         ['The Procter & Gamble Company',61.91,0.01,0.02,'9/1 12:00am'],\r
202         ['United Technologies Corporation',63.26,0.55,0.88,'9/1 12:00am'],\r
203         ['Verizon Communications',35.57,0.39,1.11,'9/1 12:00am'],\r
204         ['Wal-Mart Stores, Inc.',45.45,0.73,1.63,'9/1 12:00am']\r
205     ];\r
206 \r
207     // example of custom renderer function\r
208     function change(val){\r
209         if(val > 0){\r
210             return '<span style="color:green;">' + val + '</span>';\r
211         }else if(val < 0){\r
212             return '<span style="color:red;">' + val + '</span>';\r
213         }\r
214         return val;\r
215     }\r
216 \r
217     // example of custom renderer function\r
218     function pctChange(val){\r
219         if(val > 0){\r
220             return '<span style="color:green;">' + val + '%</span>';\r
221         }else if(val < 0){\r
222             return '<span style="color:red;">' + val + '%</span>';\r
223         }\r
224         return val;\r
225     }\r
226 \r
227     // create the data store\r
228     var store = new Ext.data.ArrayStore({\r
229         fields: [\r
230            {name: 'company'},\r
231            {name: 'price', type: 'float'},\r
232            {name: 'change', type: 'float'},\r
233            {name: 'pctChange', type: 'float'},\r
234            {name: 'lastChange', type: 'date', dateFormat: 'n/j h:ia'}\r
235         ]\r
236     });\r
237     store.loadData(myData);\r
238     \r
239     var helpWindow = new Ext.Window({\r
240         title: 'Source code',\r
241         width: 920,\r
242         height: 500,\r
243         closeAction: 'hide',\r
244         bodyCfg: {tag: 'textarea', readonly: true},\r
245         bodyStyle: {\r
246             backgroundColor: 'white',\r
247             margin: '0px',\r
248             border: '0px none'\r
249         },\r
250         listeners: {\r
251             render: function(w) {\r
252                 Ext.Ajax.request({\r
253                     url: 'field-to-grid-dd.js',\r
254                     success: function(r) {\r
255                         w.body.dom.value = r.responseText;\r
256                     }\r
257                 });\r
258             }\r
259         }\r
260     });\r
261 \r
262     // create the Grid\r
263     var grid = new Ext.grid.GridPanel({\r
264         store: store,\r
265         columns: [\r
266             {id:'company',header: "Company", width: 160, sortable: true, dataIndex: 'company'},\r
267             {header: "Price", width: 75, sortable: true, renderer: 'usMoney', dataIndex: 'price'},\r
268             {header: "Change", width: 75, sortable: true, renderer: change, dataIndex: 'change'},\r
269             {header: "% Change", width: 75, sortable: true, renderer: pctChange, dataIndex: 'pctChange'},\r
270             {header: "Last Updated", width: 85, sortable: true, renderer: Ext.util.Format.dateRenderer('m/d/Y'), dataIndex: 'lastChange'}\r
271         ],\r
272         plugins: new Ext.ux.CellFieldDropZone(),\r
273         stripeRows: true,\r
274         autoExpandColumn: 'company',\r
275         height:350,\r
276         width:600,\r
277         title:'Array Grid',\r
278         bbar: new Ext.PagingToolbar({\r
279             buttons: [{\r
280                 text: 'View Source',\r
281                 handler: function() {\r
282                     helpWindow.show();\r
283                 }\r
284             }],\r
285             store: store,\r
286             pageSize: 25\r
287         })\r
288     });\r
289 \r
290     grid.render('grid-example');\r
291     grid.getSelectionModel().selectFirstRow();\r
292 \r
293     var f = new Ext.Panel({\r
294         frame: true,\r
295         layout: 'form',\r
296         width: 600,\r
297         plugins: new Ext.ux.PanelFieldDragZone(),\r
298         style: {\r
299             'margin-top': '10px'\r
300         },\r
301         labelWidth: 150,\r
302         items: [{\r
303             xtype: 'textfield',\r
304             fieldLabel: 'Drag this text',\r
305             value: 'test'\r
306         },{\r
307             xtype: 'numberfield',\r
308             fieldLabel: 'Drag this number',\r
309             value: '1.2'\r
310         },{\r
311             xtype: 'datefield',\r
312             fieldLabel: 'Drag this date',\r
313             value: new Date()\r
314         }],\r
315         renderTo: Ext.getBody()\r
316     });\r
317 });</pre>    \r
318 </body>\r
319 </html>