Upgrade to ExtJS 4.0.2 - Released 06/09/2011
[extjs.git] / examples / tree / treegrid.js
1 /*
2
3 This file is part of Ext JS 4
4
5 Copyright (c) 2011 Sencha Inc
6
7 Contact:  http://www.sencha.com/contact
8
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file.  Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
11
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
13
14 */
15 Ext.require([
16     'Ext.data.*',
17     'Ext.grid.*',
18     'Ext.tree.*'
19 ]);
20
21 Ext.onReady(function() {
22     //we want to setup a model and store instead of using dataUrl
23     Ext.define('Task', {
24         extend: 'Ext.data.Model',
25         fields: [
26             {name: 'task',     type: 'string'},
27             {name: 'user',     type: 'string'},
28             {name: 'duration', type: 'string'}
29         ]
30     });
31
32     var store = Ext.create('Ext.data.TreeStore', {
33         model: 'Task',
34         proxy: {
35             type: 'ajax',
36             //the store will get the content from the .json file
37             url: 'treegrid.json'
38         },
39         folderSort: true
40     });
41
42     //Ext.ux.tree.TreeGrid is no longer a Ux. You can simply use a tree.TreePanel
43     var tree = Ext.create('Ext.tree.Panel', {
44         title: 'Core Team Projects',
45         width: 500,
46         height: 300,
47         renderTo: Ext.getBody(),
48         collapsible: true,
49         useArrows: true,
50         rootVisible: false,
51         store: store,
52         multiSelect: true,
53         singleExpand: true,
54         //the 'columns' property is now 'headers'
55         columns: [{
56             xtype: 'treecolumn', //this is so we know which column will show the tree
57             text: 'Task',
58             flex: 2,
59             sortable: true,
60             dataIndex: 'task'
61         },{
62             //we must use the templateheader component so we can use a custom tpl
63             xtype: 'templatecolumn',
64             text: 'Duration',
65             flex: 1,
66             sortable: true,
67             dataIndex: 'duration',
68             align: 'center',
69             //add in the custom tpl for the rows
70             tpl: Ext.create('Ext.XTemplate', '{duration:this.formatHours}', {
71                 formatHours: function(v) {
72                     if (v < 1) {
73                         return Math.round(v * 60) + ' mins';
74                     } else if (Math.floor(v) !== v) {
75                         var min = v - Math.floor(v);
76                         return Math.floor(v) + 'h ' + Math.round(min * 60) + 'm';
77                     } else {
78                         return v + ' hour' + (v === 1 ? '' : 's');
79                     }
80                 }
81             })
82         },{
83             text: 'Assigned To',
84             flex: 1,
85             dataIndex: 'user',
86             sortable: true
87         }]
88     });
89 });
90