Table.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-layout-container-Table'>/**
  19. </span> * This layout allows you to easily render content into an HTML table. The total number of columns can be specified, and
  20. * rowspan and colspan can be used to create complex layouts within the table. This class is intended to be extended or
  21. * created via the `layout: {type: 'table'}` {@link Ext.container.Container#layout} config, and should generally not
  22. * need to be created directly via the new keyword.
  23. *
  24. * Note that when creating a layout via config, the layout-specific config properties must be passed in via the {@link
  25. * Ext.container.Container#layout} object which will then be applied internally to the layout. In the case of
  26. * TableLayout, the only valid layout config properties are {@link #columns} and {@link #tableAttrs}. However, the items
  27. * added to a TableLayout can supply the following table-specific config properties:
  28. *
  29. * - **rowspan** Applied to the table cell containing the item.
  30. * - **colspan** Applied to the table cell containing the item.
  31. * - **cellId** An id applied to the table cell containing the item.
  32. * - **cellCls** A CSS class name added to the table cell containing the item.
  33. *
  34. * The basic concept of building up a TableLayout is conceptually very similar to building up a standard HTML table. You
  35. * simply add each panel (or &quot;cell&quot;) that you want to include along with any span attributes specified as the special
  36. * config properties of rowspan and colspan which work exactly like their HTML counterparts. Rather than explicitly
  37. * creating and nesting rows and columns as you would in HTML, you simply specify the total column count in the
  38. * layout config and start adding panels in their natural order from left to right, top to bottom. The layout will
  39. * automatically figure out, based on the column count, rowspans and colspans, how to position each panel within the
  40. * table. Just like with HTML tables, your rowspans and colspans must add up correctly in your overall layout or you'll
  41. * end up with missing and/or extra cells! Example usage:
  42. *
  43. * @example
  44. * Ext.create('Ext.panel.Panel', {
  45. * title: 'Table Layout',
  46. * width: 300,
  47. * height: 150,
  48. * layout: {
  49. * type: 'table',
  50. * // The total column count must be specified here
  51. * columns: 3
  52. * },
  53. * defaults: {
  54. * // applied to each contained panel
  55. * bodyStyle: 'padding:20px'
  56. * },
  57. * items: [{
  58. * html: 'Cell A content',
  59. * rowspan: 2
  60. * },{
  61. * html: 'Cell B content',
  62. * colspan: 2
  63. * },{
  64. * html: 'Cell C content',
  65. * cellCls: 'highlight'
  66. * },{
  67. * html: 'Cell D content'
  68. * }],
  69. * renderTo: Ext.getBody()
  70. * });
  71. */
  72. Ext.define('Ext.layout.container.Table', {
  73. /* Begin Definitions */
  74. alias: ['layout.table'],
  75. extend: 'Ext.layout.container.Container',
  76. alternateClassName: 'Ext.layout.TableLayout',
  77. /* End Definitions */
  78. <span id='Ext-layout-container-Table-cfg-columns'> /**
  79. </span> * @cfg {Number} columns
  80. * The total number of columns to create in the table for this layout. If not specified, all Components added to
  81. * this layout will be rendered into a single row using one column per Component.
  82. */
  83. // private
  84. monitorResize:false,
  85. type: 'table',
  86. clearEl: true, // Base class will not create it if already truthy. Not needed in tables.
  87. targetCls: Ext.baseCSSPrefix + 'table-layout-ct',
  88. tableCls: Ext.baseCSSPrefix + 'table-layout',
  89. cellCls: Ext.baseCSSPrefix + 'table-layout-cell',
  90. <span id='Ext-layout-container-Table-cfg-tableAttrs'> /**
  91. </span> * @cfg {Object} tableAttrs
  92. * An object containing properties which are added to the {@link Ext.DomHelper DomHelper} specification used to
  93. * create the layout's `&lt;table&gt;` element. Example:
  94. *
  95. * {
  96. * xtype: 'panel',
  97. * layout: {
  98. * type: 'table',
  99. * columns: 3,
  100. * tableAttrs: {
  101. * style: {
  102. * width: '100%'
  103. * }
  104. * }
  105. * }
  106. * }
  107. */
  108. tableAttrs: null,
  109. <span id='Ext-layout-container-Table-cfg-trAttrs'> /**
  110. </span> * @cfg {Object} trAttrs
  111. * An object containing properties which are added to the {@link Ext.DomHelper DomHelper} specification used to
  112. * create the layout's `&lt;tr&gt;` elements.
  113. */
  114. <span id='Ext-layout-container-Table-cfg-tdAttrs'> /**
  115. </span> * @cfg {Object} tdAttrs
  116. * An object containing properties which are added to the {@link Ext.DomHelper DomHelper} specification used to
  117. * create the layout's `&lt;td&gt;` elements.
  118. */
  119. itemSizePolicy: {
  120. setsWidth: 0,
  121. setsHeight: 0
  122. },
  123. getItemSizePolicy: function (item) {
  124. return this.itemSizePolicy;
  125. },
  126. getLayoutItems: function() {
  127. var me = this,
  128. result = [],
  129. items = me.callParent(),
  130. item,
  131. len = items.length, i;
  132. for (i = 0; i &lt; len; i++) {
  133. item = items[i];
  134. if (!item.hidden) {
  135. result.push(item);
  136. }
  137. }
  138. return result;
  139. },
  140. <span id='Ext-layout-container-Table-method-renderChildren'> /**
  141. </span> * @private
  142. * Iterates over all passed items, ensuring they are rendered in a cell in the proper
  143. * location in the table structure.
  144. */
  145. renderChildren: function() {
  146. var me = this,
  147. items = me.getLayoutItems(),
  148. tbody = me.owner.getTargetEl().child('table', true).tBodies[0],
  149. rows = tbody.rows,
  150. i = 0,
  151. len = items.length,
  152. cells, curCell, rowIdx, cellIdx, item, trEl, tdEl, itemCt;
  153. // Calculate the correct cell structure for the current items
  154. cells = me.calculateCells(items);
  155. // Loop over each cell and compare to the current cells in the table, inserting/
  156. // removing/moving cells as needed, and making sure each item is rendered into
  157. // the correct cell.
  158. for (; i &lt; len; i++) {
  159. curCell = cells[i];
  160. rowIdx = curCell.rowIdx;
  161. cellIdx = curCell.cellIdx;
  162. item = items[i];
  163. // If no row present, create and insert one
  164. trEl = rows[rowIdx];
  165. if (!trEl) {
  166. trEl = tbody.insertRow(rowIdx);
  167. if (me.trAttrs) {
  168. trEl.set(me.trAttrs);
  169. }
  170. }
  171. // If no cell present, create and insert one
  172. itemCt = tdEl = Ext.get(trEl.cells[cellIdx] || trEl.insertCell(cellIdx));
  173. if (me.needsDivWrap()) { //create wrapper div if needed - see docs below
  174. itemCt = tdEl.first() || tdEl.createChild({tag: 'div'});
  175. itemCt.setWidth(null);
  176. }
  177. // Render or move the component into the cell
  178. if (!item.rendered) {
  179. me.renderItem(item, itemCt, 0);
  180. }
  181. else if (!me.isValidParent(item, itemCt, rowIdx, cellIdx, tbody)) {
  182. me.moveItem(item, itemCt, 0);
  183. }
  184. // Set the cell properties
  185. if (me.tdAttrs) {
  186. tdEl.set(me.tdAttrs);
  187. }
  188. if (item.tdAttrs) {
  189. tdEl.set(item.tdAttrs);
  190. }
  191. tdEl.set({
  192. colSpan: item.colspan || 1,
  193. rowSpan: item.rowspan || 1,
  194. id: item.cellId || '',
  195. cls: me.cellCls + ' ' + (item.cellCls || '')
  196. });
  197. // If at the end of a row, remove any extra cells
  198. if (!cells[i + 1] || cells[i + 1].rowIdx !== rowIdx) {
  199. cellIdx++;
  200. while (trEl.cells[cellIdx]) {
  201. trEl.deleteCell(cellIdx);
  202. }
  203. }
  204. }
  205. // Delete any extra rows
  206. rowIdx++;
  207. while (tbody.rows[rowIdx]) {
  208. tbody.deleteRow(rowIdx);
  209. }
  210. },
  211. calculate: function (ownerContext) {
  212. if (!ownerContext.hasDomProp('containerChildrenDone')) {
  213. this.done = false;
  214. } else {
  215. var targetContext = ownerContext.targetContext,
  216. widthShrinkWrap = ownerContext.widthModel.shrinkWrap,
  217. heightShrinkWrap = ownerContext.heightModel.shrinkWrap,
  218. shrinkWrap = heightShrinkWrap || widthShrinkWrap,
  219. table = shrinkWrap &amp;&amp; targetContext.el.child('table', true),
  220. targetPadding = shrinkWrap &amp;&amp; targetContext.getPaddingInfo();
  221. if (widthShrinkWrap) {
  222. ownerContext.setContentWidth(table.offsetWidth + targetPadding.width, true);
  223. }
  224. if (heightShrinkWrap) {
  225. ownerContext.setContentHeight(table.offsetHeight + targetPadding.height, true);
  226. }
  227. }
  228. },
  229. finalizeLayout: function() {
  230. if (this.needsDivWrap()) {
  231. // set wrapper div width to match layed out item - see docs below
  232. var items = this.getLayoutItems(),
  233. i,
  234. iLen = items.length,
  235. item;
  236. for (i = 0; i &lt; iLen; i++) {
  237. item = items[i];
  238. Ext.fly(item.el.dom.parentNode).setWidth(item.getWidth());
  239. }
  240. }
  241. if (Ext.isIE6 || (Ext.isIEQuirks)) {
  242. // Fixes an issue where the table won't paint
  243. this.owner.getTargetEl().child('table').repaint();
  244. }
  245. },
  246. <span id='Ext-layout-container-Table-method-calculateCells'> /**
  247. </span> * @private
  248. * Determine the row and cell indexes for each component, taking into consideration
  249. * the number of columns and each item's configured colspan/rowspan values.
  250. * @param {Array} items The layout components
  251. * @return {Object[]} List of row and cell indexes for each of the components
  252. */
  253. calculateCells: function(items) {
  254. var cells = [],
  255. rowIdx = 0,
  256. colIdx = 0,
  257. cellIdx = 0,
  258. totalCols = this.columns || Infinity,
  259. rowspans = [], //rolling list of active rowspans for each column
  260. i = 0, j,
  261. len = items.length,
  262. item;
  263. for (; i &lt; len; i++) {
  264. item = items[i];
  265. // Find the first available row/col slot not taken up by a spanning cell
  266. while (colIdx &gt;= totalCols || rowspans[colIdx] &gt; 0) {
  267. if (colIdx &gt;= totalCols) {
  268. // move down to next row
  269. colIdx = 0;
  270. cellIdx = 0;
  271. rowIdx++;
  272. // decrement all rowspans
  273. for (j = 0; j &lt; totalCols; j++) {
  274. if (rowspans[j] &gt; 0) {
  275. rowspans[j]--;
  276. }
  277. }
  278. } else {
  279. colIdx++;
  280. }
  281. }
  282. // Add the cell info to the list
  283. cells.push({
  284. rowIdx: rowIdx,
  285. cellIdx: cellIdx
  286. });
  287. // Increment
  288. for (j = item.colspan || 1; j; --j) {
  289. rowspans[colIdx] = item.rowspan || 1;
  290. ++colIdx;
  291. }
  292. ++cellIdx;
  293. }
  294. return cells;
  295. },
  296. getRenderTree: function() {
  297. var me = this,
  298. items = me.getLayoutItems(),
  299. cells,
  300. rows = [],
  301. result = Ext.apply({
  302. tag: 'table',
  303. role: 'presentation',
  304. cls: me.tableCls,
  305. cellspacing: 0,
  306. cn: {
  307. tag: 'tbody',
  308. cn: rows
  309. }
  310. }, me.tableAttrs),
  311. tdAttrs = me.tdAttrs,
  312. needsDivWrap = me.needsDivWrap(),
  313. i, len = items.length, item, curCell, tr, rowIdx, cellIdx, cell;
  314. // Calculate the correct cell structure for the current items
  315. cells = me.calculateCells(items);
  316. for (i = 0; i &lt; len; i++) {
  317. item = items[i];
  318. curCell = cells[i];
  319. rowIdx = curCell.rowIdx;
  320. cellIdx = curCell.cellIdx;
  321. // If no row present, create and insert one
  322. tr = rows[rowIdx];
  323. if (!tr) {
  324. tr = rows[rowIdx] = {
  325. tag: 'tr',
  326. cn: []
  327. };
  328. if (me.trAttrs) {
  329. Ext.apply(tr, me.trAttrs);
  330. }
  331. }
  332. // If no cell present, create and insert one
  333. cell = tr.cn[cellIdx] = {
  334. tag: 'td'
  335. };
  336. if (tdAttrs) {
  337. Ext.apply(cell, tdAttrs);
  338. }
  339. Ext.apply(cell, {
  340. colSpan: item.colspan || 1,
  341. rowSpan: item.rowspan || 1,
  342. id: item.cellId || '',
  343. cls: me.cellCls + ' ' + (item.cellCls || '')
  344. });
  345. if (needsDivWrap) { //create wrapper div if needed - see docs below
  346. cell = cell.cn = {
  347. tag: 'div'
  348. };
  349. }
  350. me.configureItem(item);
  351. // The DomHelper config of the item is the cell's sole child
  352. cell.cn = item.getRenderTree();
  353. }
  354. return result;
  355. },
  356. isValidParent: function(item, target, rowIdx, cellIdx) {
  357. var tbody,
  358. correctCell,
  359. table;
  360. // If we were called with the 3 arg signature just check that the parent table of the item is within the render target
  361. if (arguments.length === 3) {
  362. table = item.el.up('table');
  363. return table &amp;&amp; table.dom.parentNode === target.dom;
  364. }
  365. tbody = this.owner.getTargetEl().child('table', true).tBodies[0];
  366. correctCell = tbody.rows[rowIdx].cells[cellIdx];
  367. return item.el.dom.parentNode === correctCell;
  368. },
  369. <span id='Ext-layout-container-Table-method-needsDivWrap'> /**
  370. </span> * @private
  371. * Opera 10.5 has a bug where if a table cell's child has box-sizing:border-box and padding, it
  372. * will include that padding in the size of the cell, making it always larger than the
  373. * shrink-wrapped size of its contents. To get around this we have to wrap the contents in a div
  374. * and then set that div's width to match the item rendered within it afterLayout. This method
  375. * determines whether we need the wrapper div; it currently does a straight UA sniff as this bug
  376. * seems isolated to just Opera 10.5, but feature detection could be added here if needed.
  377. */
  378. needsDivWrap: function() {
  379. return Ext.isOpera10_5;
  380. }
  381. });
  382. </pre>
  383. </body>
  384. </html>