3 This file is part of Ext JS 4
5 Copyright (c) 2011 Sencha Inc
7 Contact: http://www.sencha.com/contact
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.
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
16 * This layout allows you to easily render content into an HTML table. The total number of columns can be specified, and
17 * rowspan and colspan can be used to create complex layouts within the table. This class is intended to be extended or
18 * created via the `layout: {type: 'table'}` {@link Ext.container.Container#layout} config, and should generally not
19 * need to be created directly via the new keyword.
21 * Note that when creating a layout via config, the layout-specific config properties must be passed in via the {@link
22 * Ext.container.Container#layout} object which will then be applied internally to the layout. In the case of
23 * TableLayout, the only valid layout config properties are {@link #columns} and {@link #tableAttrs}. However, the items
24 * added to a TableLayout can supply the following table-specific config properties:
26 * - **rowspan** Applied to the table cell containing the item.
27 * - **colspan** Applied to the table cell containing the item.
28 * - **cellId** An id applied to the table cell containing the item.
29 * - **cellCls** A CSS class name added to the table cell containing the item.
31 * The basic concept of building up a TableLayout is conceptually very similar to building up a standard HTML table. You
32 * simply add each panel (or "cell") that you want to include along with any span attributes specified as the special
33 * config properties of rowspan and colspan which work exactly like their HTML counterparts. Rather than explicitly
34 * creating and nesting rows and columns as you would in HTML, you simply specify the total column count in the
35 * layoutConfig and start adding panels in their natural order from left to right, top to bottom. The layout will
36 * automatically figure out, based on the column count, rowspans and colspans, how to position each panel within the
37 * table. Just like with HTML tables, your rowspans and colspans must add up correctly in your overall layout or you'll
38 * end up with missing and/or extra cells! Example usage:
41 * Ext.create('Ext.panel.Panel', {
42 * title: 'Table Layout',
47 * // The total column count must be specified here
51 * // applied to each contained panel
52 * bodyStyle: 'padding:20px'
55 * html: 'Cell A content',
58 * html: 'Cell B content',
61 * html: 'Cell C content',
62 * cellCls: 'highlight'
64 * html: 'Cell D content'
66 * renderTo: Ext.getBody()
69 Ext.define('Ext.layout.container.Table', {
71 /* Begin Definitions */
73 alias: ['layout.table'],
74 extend: 'Ext.layout.container.Auto',
75 alternateClassName: 'Ext.layout.TableLayout',
80 * @cfg {Number} columns
81 * The total number of columns to create in the table for this layout. If not specified, all Components added to
82 * this layout will be rendered into a single row using one column per Component.
90 // Table layout is a self-sizing layout. When an item of for example, a dock layout, the Panel must expand to accommodate
91 // a table layout. See in particular AbstractDock::onLayout for use of this flag.
94 clearEl: true, // Base class will not create it if already truthy. Not needed in tables.
96 targetCls: Ext.baseCSSPrefix + 'table-layout-ct',
97 tableCls: Ext.baseCSSPrefix + 'table-layout',
98 cellCls: Ext.baseCSSPrefix + 'table-layout-cell',
101 * @cfg {Object} tableAttrs
102 * An object containing properties which are added to the {@link Ext.DomHelper DomHelper} specification used to
103 * create the layout's `<table>` element. Example:
121 * @cfg {Object} trAttrs
122 * An object containing properties which are added to the {@link Ext.DomHelper DomHelper} specification used to
123 * create the layout's <tr> elements.
127 * @cfg {Object} tdAttrs
128 * An object containing properties which are added to the {@link Ext.DomHelper DomHelper} specification used to
129 * create the layout's <td> elements.
134 * Iterates over all passed items, ensuring they are rendered in a cell in the proper
135 * location in the table structure.
137 renderItems: function(items) {
138 var tbody = this.getTable().tBodies[0],
142 cells, curCell, rowIdx, cellIdx, item, trEl, tdEl, itemCt;
144 // Calculate the correct cell structure for the current items
145 cells = this.calculateCells(items);
147 // Loop over each cell and compare to the current cells in the table, inserting/
148 // removing/moving cells as needed, and making sure each item is rendered into
150 for (; i < len; i++) {
152 rowIdx = curCell.rowIdx;
153 cellIdx = curCell.cellIdx;
156 // If no row present, create and insert one
159 trEl = tbody.insertRow(rowIdx);
161 trEl.set(this.trAttrs);
165 // If no cell present, create and insert one
166 itemCt = tdEl = Ext.get(trEl.cells[cellIdx] || trEl.insertCell(cellIdx));
167 if (this.needsDivWrap()) { //create wrapper div if needed - see docs below
168 itemCt = tdEl.first() || tdEl.createChild({tag: 'div'});
169 itemCt.setWidth(null);
172 // Render or move the component into the cell
173 if (!item.rendered) {
174 this.renderItem(item, itemCt, 0);
176 else if (!this.isValidParent(item, itemCt, 0)) {
177 this.moveItem(item, itemCt, 0);
180 // Set the cell properties
182 tdEl.set(this.tdAttrs);
185 colSpan: item.colspan || 1,
186 rowSpan: item.rowspan || 1,
187 id: item.cellId || '',
188 cls: this.cellCls + ' ' + (item.cellCls || '')
191 // If at the end of a row, remove any extra cells
192 if (!cells[i + 1] || cells[i + 1].rowIdx !== rowIdx) {
194 while (trEl.cells[cellIdx]) {
195 trEl.deleteCell(cellIdx);
200 // Delete any extra rows
202 while (tbody.rows[rowIdx]) {
203 tbody.deleteRow(rowIdx);
207 afterLayout: function() {
210 if (this.needsDivWrap()) {
211 // set wrapper div width to match layed out item - see docs below
212 Ext.Array.forEach(this.getLayoutItems(), function(item) {
213 Ext.fly(item.el.dom.parentNode).setWidth(item.getWidth());
220 * Determine the row and cell indexes for each component, taking into consideration
221 * the number of columns and each item's configured colspan/rowspan values.
222 * @param {Array} items The layout components
223 * @return {Object[]} List of row and cell indexes for each of the components
225 calculateCells: function(items) {
230 totalCols = this.columns || Infinity,
231 rowspans = [], //rolling list of active rowspans for each column
236 for (; i < len; i++) {
239 // Find the first available row/col slot not taken up by a spanning cell
240 while (colIdx >= totalCols || rowspans[colIdx] > 0) {
241 if (colIdx >= totalCols) {
242 // move down to next row
247 // decrement all rowspans
248 for (j = 0; j < totalCols; j++) {
249 if (rowspans[j] > 0) {
258 // Add the cell info to the list
265 for (j = item.colspan || 1; j; --j) {
266 rowspans[colIdx] = item.rowspan || 1;
277 * Return the layout's table element, creating it if necessary.
279 getTable: function() {
280 var table = this.table;
282 table = this.table = this.getTarget().createChild(
285 role: 'presentation',
287 cellspacing: 0, //TODO should this be specified or should CSS handle it?
298 * Opera 10.5 has a bug where if a table cell's child has box-sizing:border-box and padding, it
299 * will include that padding in the size of the cell, making it always larger than the
300 * shrink-wrapped size of its contents. To get around this we have to wrap the contents in a div
301 * and then set that div's width to match the item rendered within it afterLayout. This method
302 * determines whether we need the wrapper div; it currently does a straight UA sniff as this bug
303 * seems isolated to just Opera 10.5, but feature detection could be added here if needed.
305 needsDivWrap: function() {
306 return Ext.isOpera10_5;