source: trunk/web/addons/job_monarch/lib/extjs/examples/dd/dnd_grid_to_grid.js @ 619

Last change on this file since 619 was 619, checked in by ramonb, 15 years ago

lib/:

  • added new AJAX dependancies: ExtJS, pChart, Lightbox2
File size: 4.8 KB
Line 
1/*
2 * Ext JS Library 2.2.1
3 * Copyright(c) 2006-2009, Ext JS, LLC.
4 * licensing@extjs.com
5 *
6 * http://extjs.com/license
7 */
8
9/*
10 * Ext JS Library 2.1
11 * Copyright(c) 2006-2009, Ext JS, LLC.
12 * licensing@extjs.com
13 *
14 * http://extjs.com/license
15 */
16
17Ext.onReady(function(){
18 
19    var myData = {
20                records : [
21                        { name : "Rec 0", column1 : "0", column2 : "0" },
22                        { name : "Rec 1", column1 : "1", column2 : "1" },
23                        { name : "Rec 2", column1 : "2", column2 : "2" },
24                        { name : "Rec 3", column1 : "3", column2 : "3" },
25                        { name : "Rec 4", column1 : "4", column2 : "4" },
26                        { name : "Rec 5", column1 : "5", column2 : "5" },
27                        { name : "Rec 6", column1 : "6", column2 : "6" },
28                        { name : "Rec 7", column1 : "7", column2 : "7" },
29                        { name : "Rec 8", column1 : "8", column2 : "8" },
30                        { name : "Rec 9", column1 : "9", column2 : "9" }
31                ]
32        };
33
34
35        // Generic fields array to use in both store defs.
36        var fields = [
37           {name: 'name', mapping : 'name'},
38           {name: 'column1', mapping : 'column1'},
39           {name: 'column2', mapping : 'column2'}
40        ];
41       
42    // create the data store
43    var firstGridStore = new Ext.data.JsonStore({
44        fields : fields,
45                data   : myData,
46                root   : 'records'
47    });
48       
49
50        // Column Model shortcut array
51        var cols = [
52                { id : 'name', header: "Record Name", width: 160, sortable: true, dataIndex: 'name'},
53                {header: "column1", width: 50, sortable: true, dataIndex: 'column1'},
54                {header: "column2", width: 50, sortable: true, dataIndex: 'column2'}
55        ];
56   
57        // declare the source Grid
58    var firstGrid = new Ext.grid.GridPanel({
59                ddGroup          : 'secondGridDDGroup',
60        store            : firstGridStore,
61        columns          : cols,
62                enableDragDrop   : true,
63        stripeRows       : true,
64        autoExpandColumn : 'name',
65        width            : 325,
66                region           : 'west',
67        title            : 'First Grid'
68    });
69
70    var secondGridStore = new Ext.data.JsonStore({
71        fields : fields,
72                root   : 'records'
73    });
74       
75    // create the destination Grid
76    var secondGrid = new Ext.grid.GridPanel({
77                ddGroup          : 'firstGridDDGroup',
78        store            : secondGridStore,
79        columns          : cols,
80                enableDragDrop   : true,
81        stripeRows       : true,
82        autoExpandColumn : 'name',
83        width            : 325,
84                region           : 'center',
85        title            : 'Second Grid'
86    });
87
88       
89        //Simple 'border layout' panel to house both grids
90        var displayPanel = new Ext.Panel({
91                width    : 650,
92                height   : 300,
93                layout   : 'border',
94                renderTo : 'panel',
95                items    : [
96                        firstGrid,
97                        secondGrid
98                ],
99                bbar    : [
100                        '->', // Fill
101                        {
102                                text    : 'Reset both grids',
103                                handler : function() {
104                                        //refresh source grid
105                                        firstGridStore.loadData(myData);
106                                       
107                                        //purge destination grid
108                                        secondGridStore.removeAll();
109                                }
110                        }
111                ]
112        });
113
114        // used to add records to the destination stores
115        var blankRecord =  Ext.data.Record.create(fields);
116
117        /****
118        * Setup Drop Targets
119        ***/
120        // This will make sure we only drop to the view container
121        var firstGridDropTargetEl =  firstGrid.getView().el.dom.childNodes[0].childNodes[1];
122        var firstGridDropTarget = new Ext.dd.DropTarget(firstGridDropTargetEl, {
123                ddGroup    : 'firstGridDDGroup',
124                copy       : true,
125                notifyDrop : function(ddSource, e, data){
126                       
127                        // Generic function to add records.
128                        function addRow(record, index, allItems) {
129                               
130                                // Search for duplicates
131                                var foundItem = firstGridStore.find('name', record.data.name);
132                                // if not found
133                                if (foundItem  == -1) {
134                                        firstGridStore.add(record);
135                                       
136                                        // Call a sort dynamically
137                                        firstGridStore.sort('name', 'ASC');
138                                       
139                                        //Remove Record from the source
140                                        ddSource.grid.store.remove(record);
141                                }
142                        }
143
144                        // Loop through the selections
145                        Ext.each(ddSource.dragData.selections ,addRow);
146                        return(true);
147                }
148        });     
149
150       
151        // This will make sure we only drop to the view container
152        var secondGridDropTargetEl = secondGrid.getView().el.dom.childNodes[0].childNodes[1]
153       
154        var destGridDropTarget = new Ext.dd.DropTarget(secondGridDropTargetEl, {
155                ddGroup    : 'secondGridDDGroup',
156                copy       : false,
157                notifyDrop : function(ddSource, e, data){
158                       
159                        // Generic function to add records.
160                        function addRow(record, index, allItems) {
161                               
162                                // Search for duplicates
163                                var foundItem = secondGridStore.find('name', record.data.name);
164                                // if not found
165                                if (foundItem  == -1) {
166                                        secondGridStore.add(record);
167                                        // Call a sort dynamically
168                                        secondGridStore.sort('name', 'ASC');
169                       
170                                        //Remove Record from the source
171                                        ddSource.grid.store.remove(record);
172                                }
173                        }
174                        // Loop through the selections
175                        Ext.each(ddSource.dragData.selections ,addRow);
176                        return(true);
177                }
178        }); 
179});
Note: See TracBrowser for help on using the repository browser.