examples/grid/edit-grid.js
[roojs1] / examples / grid / edit-grid.js
1 /*
2  * Based on:
3  * Ext JS Library 1.1.1
4  * Copyright(c) 2006-2007, Ext JS, LLC.
5  *
6  * Originally Released Under LGPL - original licence link has changed is not relivant.
7  *
8  * Fork - LGPL
9  * <script type="text/javascript">
10  */
11
12 Roo.BLANK_IMAGE_URL  = "../../images/default/s.gif";
13
14 var grid = false;
15
16 Roo.onReady(function(){
17     Roo.QuickTips.init();
18     function formatBoolean(value){
19         return value ? 'Yes' : 'No';  
20     };
21     
22     function formatDate(value){
23         return value ? value.dateFormat('M d, Y') : '';
24     };
25     // shorthand alias
26     var fm = Roo.form, Ed = Roo.grid.GridEditor;
27
28     // the column model has information about grid columns
29     // dataIndex maps the column to the specific data field in
30     // the data store (created below)
31     var cm = new Roo.grid.ColumnModel([{
32            header: "Common Name",
33            dataIndex: 'common',
34            width: 220  /*,
35            
36            editor: new Ed(new fm.TextField({
37                allowBlank: false
38            })) */
39         },{
40            header: "Light",
41            dataIndex: 'light',
42            width: 130,
43            editor: new Ed(new Roo.form.ComboBox({
44                typeAhead: true,
45                triggerAction: 'all',
46                transform:'light',
47                lazyRender:true
48             }))
49         },{
50            header: "Price",
51            dataIndex: 'price',
52            width: 70,
53            align: 'right',
54            renderer: 'usMoney',
55            editor: new Ed(new fm.NumberField({
56                allowBlank: false,
57                allowNegative: false,
58                maxValue: 10
59            }))
60         },{
61            header: "Available",
62            dataIndex: 'availDate',
63            width: 95,
64            renderer: formatDate,
65            editor: new Ed(new fm.DateField({
66                 format: 'm/d/y',
67                 minValue: '01/01/06',
68                 disabledDays: [0, 6],
69                 disabledDaysText: 'Plants are not available on the weekends'
70             }))
71         },{
72            header: "Indoor?",
73            dataIndex: 'indoor',
74            width: 55,
75            renderer: formatBoolean,
76            editor: new Ed(new fm.Checkbox())
77         }]);
78
79     // by default columns are sortable
80     cm.defaultSortable = true;
81
82     // this could be inline, but we want to define the Plant record
83     // type so we can add records dynamically
84     var Plant = Roo.data.Record.create([
85            // the "name" below matches the tag name to read, except "availDate"
86            // which is mapped to the tag "availability"
87            {name: 'common', type: 'string'},
88            {name: 'botanical', type: 'string'},
89            {name: 'light'},
90            {name: 'price', type: 'float'},             // automatic date conversions
91            {name: 'availDate', mapping: 'availability', type: 'date', dateFormat: 'm/d/Y'},
92            {name: 'indoor', type: 'bool'}
93       ]);
94
95     // create the Data Store
96     var ds = new Roo.data.Store({
97         // load using HTTP
98         proxy: new Roo.data.HttpProxy({url: 'plants.xml'}),
99         remoteSort : true,
100         
101         // the return will be XML, so lets set up a reader
102         reader: new Roo.data.XmlReader({
103                // records will have a "plant" tag
104                record: 'plant'
105            }, Plant)
106     });
107
108     // create the editor grid
109     grid = new Roo.grid.EditorGrid('editor-grid', {
110         ds: ds,
111         cm: cm,
112         enableColLock:false,
113         multiSort : true,
114         enableDragDrop : true,
115         listeners : {
116                enddrag : function(g, dd, targetid , e) {
117                     Roo.log('enddrag');
118                },
119                dragdrop : function(g, dd, targetid , e) {
120                     Roo.log('dragdrop');
121                },
122                 dragover : function(g, dd, targetid , e) {
123                     Roo.log('dragover');
124                },
125             dragenter : function(g, dd, targetid , e) {
126                     Roo.log('dragenter');
127                },
128                    dragout : function(g, dd, targetid , e) {
129                     Roo.log('dragout');
130                },
131         
132             
133             
134         }
135     });
136
137     var layout = Roo.BorderLayout.create({
138         center: {
139             margins:{left:3,top:3,right:3,bottom:3},
140             panels: [new Roo.GridPanel(grid)]
141         }
142     }, 'grid-panel');
143
144
145     // render it
146     grid.render();
147
148     
149     var gridHead = grid.getView().getHeaderPanel(true);
150     var tb = new Roo.Toolbar(gridHead, [{
151         text: 'Add Plant',
152         handler : function(){
153             var p = new Plant({
154                 common: 'New Plant 1',
155                 light: 'Mostly Shade',
156                 price: 0,
157                 availDate: new Date(),
158                 indoor: false
159             });
160             grid.stopEditing();
161             ds.insert(0, p);
162             grid.startEditing(0, 0);
163         }
164     }]);
165
166     // trigger the data store load
167     ds.load();
168 });