Upgrade to ExtJS 4.0.2 - Released 06/09/2011
[extjs.git] / examples / ux / grid / TransformGrid.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 /**
16  * @class Ext.ux.grid.TransformGrid
17  * @extends Ext.grid.Panel
18  * A Grid which creates itself from an existing HTML table element.
19  * @history
20  * 2007-03-01 Original version by Nige "Animal" White
21  * 2007-03-10 jvs Slightly refactored to reuse existing classes * @constructor
22  * @param {String/HTMLElement/Ext.Element} table The table element from which this grid will be created -
23  * The table MUST have some type of size defined for the grid to fill. The container will be
24  * automatically set to position relative if it isn't already.
25  * @param {Object} config A config object that sets properties on this grid and has two additional (optional)
26  * properties: fields and columns which allow for customizing data fields and columns for this grid.
27  */
28 Ext.define('Ext.ux.grid.TransformGrid', {
29     extend: 'Ext.grid.Panel',
30
31     constructor: function(table, config) {
32         config = Ext.apply({}, config);
33         table = this.table = Ext.get(table);
34
35         var configFields = config.fields || [],
36             configColumns = config.columns || [],
37             fields = [],
38             cols = [],
39             ct = table.insertSibling(),
40             headers = table.query("thead th"),
41             i = 0,
42             len = headers.length,
43             data = table.dom,
44             width,
45             height,
46             store,
47             col,
48             text,
49             name;
50
51         for (; i < len; ++i) {
52             col = headers[i];
53
54             text = col.innerHTML;
55             name = 'tcol-' + i;
56
57             fields.push(Ext.applyIf(configFields[i] || {}, {
58                 name: name,
59                 mapping: 'td:nth(' + (i + 1) + ')/@innerHTML'
60             }));
61
62             cols.push(Ext.applyIf(configColumns[i] || {}, {
63                 text: text,
64                 dataIndex: name,
65                 width: col.offsetWidth,
66                 tooltip: col.title,
67                 sortable: true
68             }));
69         }
70
71         if (config.width) {
72             width = config.width;
73         } else {
74             width = table.getWidth() + 1;
75         }
76
77         if (config.height) {
78             height = config.height;
79         }
80
81         if (config.remove !== false) {
82             // Don't use table.remove() as that destroys the row/cell data in the table in
83             // IE6-7 so it cannot be read by the data reader.
84             data.parentNode.removeChild(data);
85         }
86
87
88         Ext.applyIf(config, {
89             store: {
90                 data: data,
91                 fields: fields,
92                 proxy: {
93                     type: 'memory',
94                     reader: {
95                         record: 'tbody tr',
96                         type: 'xml'
97                     }
98                 }
99             },
100             columns: cols,
101             width: width,
102             autoHeight: height ? false : true,
103             height: height,
104             el: ct
105         });
106         this.callParent([config]);
107     },
108
109     onDestroy: function() {
110         this.callParent();
111         this.table.remove();
112         delete this.table;
113     }
114 });