3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
4 <title>The source code</title>
5 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
6 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
8 <body onload="prettyPrint();">
9 <pre class="prettyprint lang-js">/*!
10 * Ext JS Library 3.3.1
11 * Copyright(c) 2006-2010 Sencha Inc.
12 * licensing@sencha.com
13 * http://www.sencha.com/license
15 <div id="cls-Ext.grid.PivotGridView"></div>/**
16 * @class Ext.grid.PivotGridView
17 * @extends Ext.grid.GridView
18 * Specialised GridView for rendering Pivot Grid components. Config can be passed to the PivotGridView via the PivotGrid constructor's
21 new Ext.grid.PivotGrid({
23 title: 'My Pivot Grid',
24 getCellCls: function(value) {
25 return value > 10 'red' : 'green';
30 * <p>Currently {@link #title} and {@link #getCellCls} are the only configuration options accepted by PivotGridView. All other
31 * interaction is performed via the {@link Ext.grid.PivotGrid PivotGrid} class.</p>
33 Ext.grid.PivotGridView = Ext.extend(Ext.grid.GridView, {
35 <div id="prop-Ext.grid.PivotGridView-colHeaderCellCls"></div>/**
36 * The CSS class added to all group header cells. Defaults to 'grid-hd-group-cell'
37 * @property colHeaderCellCls
40 colHeaderCellCls: 'grid-hd-group-cell',
42 <div id="cfg-Ext.grid.PivotGridView-title"></div>/**
43 * @cfg {String} title Optional title to be placed in the top left corner of the PivotGrid. Defaults to an empty string.
47 <div id="cfg-Ext.grid.PivotGridView-getCellCls"></div>/**
48 * @cfg {Function} getCellCls Optional function which should return a CSS class name for each cell value. This is useful when
49 * color coding cells based on their value. Defaults to undefined.
52 <div id="method-Ext.grid.PivotGridView-getColumnHeaders"></div>/**
53 * Returns the headers to be rendered at the top of the grid. Should be a 2-dimensional array, where each item specifies the number
54 * of columns it groups (column in this case refers to normal grid columns). In the example below we have 5 city groups, which are
55 * each part of a continent supergroup. The colspan for each city group refers to the number of normal grid columns that group spans,
56 * so in this case the grid would be expected to have a total of 12 columns:
61 {header: 'England', colspan: 5},
62 {header: 'USA', colspan: 3}
67 {header: 'London', colspan: 2},
68 {header: 'Cambridge', colspan: 3},
69 {header: 'Palo Alto', colspan: 3}
74 * In the example above we have cities nested under countries. The nesting could be deeper if desired - e.g. Continent -> Country ->
75 * State -> City, or any other structure. The only constaint is that the same depth must be used throughout the structure.
76 * @return {Array} A tree structure containing the headers to be rendered. Must include the colspan property at each level, which should
77 * be the sum of all child nodes beneath this node.
79 getColumnHeaders: function() {
80 return this.grid.topAxis.buildHeaders();;
83 <div id="method-Ext.grid.PivotGridView-getRowHeaders"></div>/**
84 * Returns the headers to be rendered on the left of the grid. Should be a 2-dimensional array, where each item specifies the number
85 * of rows it groups. In the example below we have 5 city groups, which are each part of a continent supergroup. The rowspan for each
86 * city group refers to the number of normal grid columns that group spans, so in this case the grid would be expected to have a
93 {header: 'England', rowspan: 5},
94 {header: 'USA', rowspan: 3}
100 {header: 'London', rowspan: 2},
101 {header: 'Cambridge', rowspan: 3},
102 {header: 'Palo Alto', rowspan: 3}
107 * In the example above we have cities nested under countries. The nesting could be deeper if desired - e.g. Continent -> Country ->
108 * State -> City, or any other structure. The only constaint is that the same depth must be used throughout the structure.
109 * @return {Array} A tree structure containing the headers to be rendered. Must include the colspan property at each level, which should
110 * be the sum of all child nodes beneath this node.
111 * Each group may specify the width it should be rendered with.
112 * @return {Array} The row groups
114 getRowHeaders: function() {
115 return this.grid.leftAxis.buildHeaders();
120 * Renders rows between start and end indexes
121 * @param {Number} startRow Index of the first row to render
122 * @param {Number} endRow Index of the last row to render
124 renderRows : function(startRow, endRow) {
125 var grid = this.grid,
126 rows = grid.extractData(),
127 rowCount = rows.length,
128 templates = this.templates,
129 renderer = grid.renderer,
130 hasRenderer = typeof renderer == 'function',
131 getCellCls = this.getCellCls,
132 hasGetCellCls = typeof getCellCls == 'function',
133 cellTemplate = templates.cell,
134 rowTemplate = templates.row,
137 tstyle = 'width:' + this.getGridInnerWidth() + 'px;',
138 colBuffer, column, i;
140 startRow = startRow || 0;
141 endRow = Ext.isDefined(endRow) ? endRow : rowCount - 1;
143 for (i = 0; i < rowCount; i++) {
145 colCount = row.length;
148 rowIndex = startRow + i;
150 //build up each column's HTML
151 for (j = 0; j < colCount; j++) {
154 meta.css = j === 0 ? 'x-grid3-cell-first ' : (j == (colCount - 1) ? 'x-grid3-cell-last ' : '');
155 meta.attr = meta.cellAttr = '';
158 if (Ext.isEmpty(meta.value)) {
159 meta.value = ' ';
163 meta.value = renderer(meta.value);
167 meta.css += getCellCls(meta.value) + ' ';
170 colBuffer[colBuffer.length] = cellTemplate.apply(meta);
173 rowBuffer[rowBuffer.length] = rowTemplate.apply({
176 cells : colBuffer.join(""),
181 return rowBuffer.join("");
184 <div id="prop-Ext.grid.PivotGridView-Ext.Template"></div>/**
185 * The master template to use when rendering the GridView. Has a default template
186 * @property Ext.Template
189 masterTpl: new Ext.Template(
190 '<div class="x-grid3 x-pivotgrid" hidefocus="true">',
191 '<div class="x-grid3-viewport">',
192 '<div class="x-grid3-header">',
193 '<div class="x-grid3-header-title"><span>{title}</span></div>',
194 '<div class="x-grid3-header-inner">',
195 '<div class="x-grid3-header-offset" style="{ostyle}"></div>',
197 '<div class="x-clear"></div>',
199 '<div class="x-grid3-scroller">',
200 '<div class="x-grid3-row-headers"></div>',
201 '<div class="x-grid3-body" style="{bstyle}">{body}</div>',
202 '<a href="#" class="x-grid3-focus" tabIndex="-1"></a>',
205 '<div class="x-grid3-resize-marker"> </div>',
206 '<div class="x-grid3-resize-proxy"> </div>',
212 * Adds a gcell template to the internal templates object. This is used to render the headers in a multi-level column header.
214 initTemplates: function() {
215 Ext.grid.PivotGridView.superclass.initTemplates.apply(this, arguments);
217 var templates = this.templates || {};
218 if (!templates.gcell) {
219 templates.gcell = new Ext.XTemplate(
220 '<td class="x-grid3-hd x-grid3-gcell x-grid3-td-{id} ux-grid-hd-group-row-{row} ' + this.colHeaderCellCls + '" style="{style}">',
221 '<div {tooltip} class="x-grid3-hd-inner x-grid3-hd-{id}" unselectable="on" style="{istyle}">',
222 this.grid.enableHdMenu ? '<a class="x-grid3-hd-btn" href="#"></a>' : '', '{value}',
228 this.templates = templates;
229 this.hrowRe = new RegExp("ux-grid-hd-group-row-(\\d+)", "");
234 * Sets up the reference to the row headers element
236 initElements: function() {
237 Ext.grid.PivotGridView.superclass.initElements.apply(this, arguments);
239 <div id="prop-Ext.grid.PivotGridView-rowHeadersEl"></div>/**
240 * @property rowHeadersEl
242 * The element containing all row headers
244 this.rowHeadersEl = new Ext.Element(this.scroller.child('div.x-grid3-row-headers'));
246 <div id="prop-Ext.grid.PivotGridView-headerTitleEl"></div>/**
247 * @property headerTitleEl
249 * The element that contains the optional title (top left section of the pivot grid)
251 this.headerTitleEl = new Ext.Element(this.mainHd.child('div.x-grid3-header-title'));
256 * Takes row headers into account when calculating total available width
258 getGridInnerWidth: function() {
259 var previousWidth = Ext.grid.PivotGridView.superclass.getGridInnerWidth.apply(this, arguments);
261 return previousWidth - this.getTotalRowHeaderWidth();
264 <div id="method-Ext.grid.PivotGridView-getTotalRowHeaderWidth"></div>/**
265 * Returns the total width of all row headers as specified by {@link #getRowHeaders}
266 * @return {Number} The total width
268 getTotalRowHeaderWidth: function() {
269 var headers = this.getRowHeaders(),
270 length = headers.length,
274 for (i = 0; i< length; i++) {
275 total += headers[i].width;
283 * Returns the total height of all column headers
284 * @return {Number} The total height
286 getTotalColumnHeaderHeight: function() {
287 return this.getColumnHeaders().length * 21;
292 * Slight specialisation of the GridView renderUI - just adds the row headers
294 renderUI : function() {
295 var templates = this.templates,
296 innerWidth = this.getGridInnerWidth();
298 return templates.master.apply({
299 body : templates.body.apply({rows:' '}),
300 ostyle: 'width:' + innerWidth + 'px',
301 bstyle: 'width:' + innerWidth + 'px'
307 * Make sure that the headers and rows are all sized correctly during layout
309 onLayout: function(width, height) {
310 Ext.grid.PivotGridView.superclass.onLayout.apply(this, arguments);
312 var width = this.getGridInnerWidth();
314 this.resizeColumnHeaders(width);
315 this.resizeAllRows(width);
318 <div id="method-Ext.grid.PivotGridView-refresh"></div>/**
319 * Refreshs the grid UI
320 * @param {Boolean} headersToo (optional) True to also refresh the headers
322 refresh : function(headersToo) {
323 this.fireEvent('beforerefresh', this);
324 this.grid.stopEditing(true);
326 var result = this.renderBody();
327 this.mainBody.update(result).setWidth(this.getGridInnerWidth());
328 if (headersToo === true) {
329 this.updateHeaders();
330 this.updateHeaderSortState();
332 this.processRows(0, true);
334 this.applyEmptyText();
335 this.fireEvent('refresh', this);
340 * Bypasses GridView's renderHeaders as they are taken care of separately by the PivotAxis instances
342 renderHeaders: Ext.emptyFn,
346 * Taken care of by PivotAxis
348 fitColumns: Ext.emptyFn,
352 * Called on layout, ensures that the width of each column header is correct. Omitting this can lead to faulty
353 * layouts when nested in a container.
354 * @param {Number} width The new width
356 resizeColumnHeaders: function(width) {
357 var topAxis = this.grid.topAxis;
359 if (topAxis.rendered) {
360 topAxis.el.setWidth(width);
366 * Sets the row header div to the correct width. Should be called after rendering and reconfiguration of headers
368 resizeRowHeaders: function() {
369 var rowHeaderWidth = this.getTotalRowHeaderWidth(),
370 marginStyle = String.format("margin-left: {0}px;", rowHeaderWidth);
372 this.rowHeadersEl.setWidth(rowHeaderWidth);
373 this.mainBody.applyStyles(marginStyle);
374 Ext.fly(this.innerHd).applyStyles(marginStyle);
376 this.headerTitleEl.setWidth(rowHeaderWidth);
377 this.headerTitleEl.setHeight(this.getTotalColumnHeaderHeight());
382 * Resizes all rendered rows to the given width. Usually called by onLayout
383 * @param {Number} width The new width
385 resizeAllRows: function(width) {
386 var rows = this.getRows(),
387 length = rows.length,
390 for (i = 0; i < length; i++) {
391 Ext.fly(rows[i]).setWidth(width);
392 Ext.fly(rows[i]).child('table').setWidth(width);
398 * Updates the Row Headers, deferring the updating of Column Headers to GridView
400 updateHeaders: function() {
401 this.renderGroupRowHeaders();
402 this.renderGroupColumnHeaders();
407 * Renders all row header groups at all levels based on the structure fetched from {@link #getGroupRowHeaders}
409 renderGroupRowHeaders: function() {
410 var leftAxis = this.grid.leftAxis;
412 this.resizeRowHeaders();
413 leftAxis.rendered = false;
414 leftAxis.render(this.rowHeadersEl);
416 this.setTitle(this.title);
419 <div id="method-Ext.grid.PivotGridView-setTitle"></div>/**
420 * Sets the title text in the top left segment of the PivotGridView
421 * @param {String} title The title
423 setTitle: function(title) {
424 this.headerTitleEl.child('span').dom.innerHTML = title;
429 * Renders all column header groups at all levels based on the structure fetched from {@link #getColumnHeaders}
431 renderGroupColumnHeaders: function() {
432 var topAxis = this.grid.topAxis;
434 topAxis.rendered = false;
435 topAxis.render(this.innerHd.firstChild);
440 * Overridden to test whether the user is hovering over a group cell, in which case we don't show the menu
442 isMenuDisabled: function(cellIndex, el) {