4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-grid-column-Column'>/**
19 </span> * @class Ext.grid.column.Column
20 * @extends Ext.grid.header.Container
22 * This class specifies the definition for a column inside a {@link Ext.grid.Panel}. It encompasses
23 * both the grid header configuration as well as displaying data within the grid itself. If the
24 * {@link #columns} configuration is specified, this column will become a column group and can
25 * container other columns inside. In general, this class will not be created directly, rather
26 * an array of column configurations will be passed to the grid:
28 * {@img Ext.grid.column.Column/Ext.grid.column.Column.png Ext.grid.column.Column grid column}
32 * Ext.create('Ext.data.Store', {
33 * storeId:'employeeStore',
34 * fields:['firstname', 'lastname', 'senority', 'dep', 'hired'],
36 * {firstname:"Michael", lastname:"Scott", senority:7, dep:"Manangement", hired:"01/10/2004"},
37 * {firstname:"Dwight", lastname:"Schrute", senority:2, dep:"Sales", hired:"04/01/2004"},
38 * {firstname:"Jim", lastname:"Halpert", senority:3, dep:"Sales", hired:"02/22/2006"},
39 * {firstname:"Kevin", lastname:"Malone", senority:4, dep:"Accounting", hired:"06/10/2007"},
40 * {firstname:"Angela", lastname:"Martin", senority:5, dep:"Accounting", hired:"10/21/2008"}
44 * Ext.create('Ext.grid.Panel', {
45 * title: 'Column Demo',
46 * store: Ext.data.StoreManager.lookup('employeeStore'),
48 * {text: 'First Name', dataIndex:'firstname'},
49 * {text: 'Last Name', dataIndex:'lastname'},
50 * {text: 'Hired Month', dataIndex:'hired', xtype:'datecolumn', format:'M'},
51 * {text: 'Deparment (Yrs)', xtype:'templatecolumn', tpl:'{dep} ({senority})'}
54 * renderTo: Ext.getBody()
57 * ## Convenience Subclasses
58 * There are several column subclasses that provide default rendering for various data types
60 * - {@link Ext.grid.column.Action}: Renders icons that can respond to click events inline
61 * - {@link Ext.grid.column.Boolean}: Renders for boolean values
62 * - {@link Ext.grid.column.Date}: Renders for date values
63 * - {@link Ext.grid.column.Number}: Renders for numeric values
64 * - {@link Ext.grid.column.Template}: Renders a value using an {@link Ext.XTemplate} using the record data
67 * The columns are laid out by a {@link Ext.layout.container.HBox} layout, so a column can either
68 * be given an explicit width value or a flex configuration. If no width is specified the grid will
69 * automatically the size the column to 100px. For column groups, the size is calculated by measuring
70 * the width of the child columns, so a width option should not be specified in that case.
73 * - {@link #text}: Sets the header text for the column
74 * - {@link #sortable}: Specifies whether the column can be sorted by clicking the header or using the column menu
75 * - {@link #hideable}: Specifies whether the column can be hidden using the column menu
76 * - {@link #menuDisabled}: Disables the column header menu
77 * - {@link #draggable}: Specifies whether the column header can be reordered by dragging
78 * - {@link #groupable}: Specifies whether the grid can be grouped by the column dataIndex. See also {@link Ext.grid.feature.Grouping}
81 * - {@link #dataIndex}: The dataIndex is the field in the underlying {@link Ext.data.Store} to use as the value for the column.
82 * - {@link #renderer}: Allows the underlying store value to be transformed before being displayed in the grid
84 Ext.define('Ext.grid.column.Column', {
85 extend: 'Ext.grid.header.Container',
86 alias: 'widget.gridcolumn',
87 requires: ['Ext.util.KeyNav'],
88 alternateClassName: 'Ext.grid.Column',
90 baseCls: Ext.baseCSSPrefix + 'column-header ' + Ext.baseCSSPrefix + 'unselectable',
92 // Not the standard, automatically applied overCls because we must filter out overs of child headers.
93 hoverCls: Ext.baseCSSPrefix + 'column-header-over',
99 possibleSortStates: ['ASC', 'DESC'],
102 '<div class="' + Ext.baseCSSPrefix + 'column-header-inner">' +
103 '<span class="' + Ext.baseCSSPrefix + 'column-header-text">' +
106 '<tpl if="!values.menuDisabled"><div class="' + Ext.baseCSSPrefix + 'column-header-trigger"></div></tpl>' +
109 <span id='Ext-grid-column-Column-cfg-columns'> /**
110 </span> * @cfg {Array} columns
111 * <p>An optional array of sub-column definitions. This column becomes a group, and houses the columns defined in the <code>columns</code> config.</p>
112 * <p>Group columns may not be sortable. But they may be hideable and moveable. And you may move headers into and out of a group. Note that
113 * if all sub columns are dragged out of a group, the group is destroyed.
116 <span id='Ext-grid-column-Column-cfg-dataIndex'> /**
117 </span> * @cfg {String} dataIndex <p><b>Required</b>. The name of the field in the
118 * grid's {@link Ext.data.Store}'s {@link Ext.data.Model} definition from
119 * which to draw the column's value.</p>
123 <span id='Ext-grid-column-Column-cfg-text'> /**
124 </span> * @cfg {String} text Optional. The header text to be used as innerHTML
125 * (html tags are accepted) to display in the Grid. <b>Note</b>: to
126 * have a clickable header with no text displayed you can use the
127 * default of <tt>'&#160;'</tt>.
131 <span id='Ext-grid-column-Column-cfg-sortable'> /**
132 </span> * @cfg {Boolean} sortable Optional. <tt>true</tt> if sorting is to be allowed on this column.
133 * Whether local/remote sorting is used is specified in <code>{@link Ext.data.Store#remoteSort}</code>.
137 <span id='Ext-grid-column-Column-cfg-groupable'> /**
138 </span> * @cfg {Boolean} groupable Optional. If the grid uses a {@link Ext.grid.feature.Grouping}, this option
139 * may be used to disable the header menu item to group by the column selected. By default,
140 * the header menu group option is enabled. Set to false to disable (but still show) the
141 * group option in the header menu for the column.
144 <span id='Ext-grid-column-Column-cfg-fixed'> /**
145 </span> * @cfg {Boolean} fixed Prevents the column from being resizable
148 <span id='Ext-grid-column-Column-cfg-resizable'> /**
149 </span> * @cfg {Boolean} resizable This config has no effect on a grid column, please see {@link #fixed} instead.
152 <span id='Ext-grid-column-Column-cfg-hideable'> /**
153 </span> * @cfg {Boolean} hideable Optional. Specify as <tt>false</tt> to prevent the user from hiding this column
154 * (defaults to true).
158 <span id='Ext-grid-column-Column-cfg-menuDisabled'> /**
159 </span> * @cfg {Boolean} menuDisabled
160 * True to disabled the column header menu containing sort/hide options. Defaults to false.
164 <span id='Ext-grid-column-Column-method-renderer'> /**
166 * <p>A renderer is an 'interceptor' method which can be used transform data (value, appearance, etc.) before it
167 * is rendered. Example:</p>
168 * <pre><code>{
169 renderer: function(value){
173 return value + ' people';
176 * </code></pre>
177 * @param {Mixed} value The data value for the current cell
178 * @param {Object} metaData A collection of metadata about the current cell; can be used or modified by
179 * the renderer. Recognized properties are: <tt>tdCls</tt>, <tt>tdAttr</tt>, and <tt>style</tt>.
180 * @param {Ext.data.Model} record The record for the current row
181 * @param {Number} rowIndex The index of the current row
182 * @param {Number} colIndex The index of the current column
183 * @param {Ext.data.Store} store The data store
184 * @param {Ext.view.View} view The current view
185 * @return {String} The HTML to be rendered
189 <span id='Ext-grid-column-Column-cfg-align'> /**
190 </span> * @cfg {String} align Sets the alignment of the header and rendered columns.
191 * Defaults to 'left'.
195 <span id='Ext-grid-column-Column-cfg-draggable'> /**
196 </span> * @cfg {Boolean} draggable Indicates whether or not the header can be drag and drop re-ordered.
201 // Header does not use the typical ComponentDraggable class and therefore we
202 // override this with an emptyFn. It is controlled at the HeaderDragZone.
203 initDraggable: Ext.emptyFn,
205 <span id='Ext-grid-column-Column-cfg-tdCls'> /**
206 </span> * @cfg {String} tdCls <p>Optional. A CSS class names to apply to the table cells for this column.</p>
209 <span id='Ext-grid-column-Column-property-triggerEl'> /**
210 </span> * @property {Ext.core.Element} triggerEl
213 <span id='Ext-grid-column-Column-property-textEl'> /**
214 </span> * @property {Ext.core.Element} textEl
217 <span id='Ext-grid-column-Column-property-isHeader'> /**
219 * Set in this class to identify, at runtime, instances which are not instances of the
220 * HeaderContainer base class, but are in fact, the subclass: Header.
224 initComponent: function() {
229 if (Ext.isDefined(me.header)) {
234 // Flexed Headers need to have a minWidth defined so that they can never be squeezed out of existence by the
235 // HeaderContainer's specialized Box layout, the ColumnLayout. The ColumnLayout's overridden calculateChildboxes
236 // method extends the available layout space to accommodate the "desiredWidth" of all the columns.
238 me.minWidth = me.minWidth || Ext.grid.plugin.HeaderResizer.prototype.minColWidth;
240 // Non-flexed Headers may never be squeezed in the event of a shortfall so
241 // always set their minWidth to their current width.
243 me.minWidth = me.width;
246 if (!me.triStateSort) {
247 me.possibleSortStates.length = 2;
250 // A group header; It contains items which are themselves Headers
251 if (Ext.isDefined(me.columns)) {
252 me.isGroupHeader = true;
256 Ext.Error.raise('Ext.grid.column.Column: Group header may not accept a dataIndex');
258 if ((me.width && me.width !== Ext.grid.header.Container.prototype.defaultWidth) || me.flex) {
259 Ext.Error.raise('Ext.grid.column.Column: Group header does not support setting explicit widths or flexs. The group header width is calculated by the sum of its children.');
263 // The headers become child items
264 me.items = me.columns;
269 // Acquire initial width from sub headers
270 for (i = 0, len = me.items.length; i < len; i++) {
271 me.width += me.items[i].width || Ext.grid.header.Container.prototype.defaultWidth;
273 if (me.items[i].flex) {
274 Ext.Error.raise('Ext.grid.column.Column: items of a grouped header do not support flexed values. Each item must explicitly define its width.');
278 me.minWidth = me.width;
280 me.cls = (me.cls||'') + ' ' + Ext.baseCSSPrefix + 'group-header';
286 Ext.applyIf(me.renderSelectors, {
287 titleContainer: '.' + Ext.baseCSSPrefix + 'column-header-inner',
288 triggerEl: '.' + Ext.baseCSSPrefix + 'column-header-trigger',
289 textEl: '.' + Ext.baseCSSPrefix + 'column-header-text'
292 // Initialize as a HeaderContainer
293 me.callParent(arguments);
296 onAdd: function(childHeader) {
297 childHeader.isSubHeader = true;
298 childHeader.addCls(Ext.baseCSSPrefix + 'group-sub-header');
301 onRemove: function(childHeader) {
302 childHeader.isSubHeader = false;
303 childHeader.removeCls(Ext.baseCSSPrefix + 'group-sub-header');
306 initRenderData: function() {
309 Ext.applyIf(me.renderData, {
311 menuDisabled: me.menuDisabled
313 return me.callParent(arguments);
316 <span id='Ext-grid-column-Column-method-setText'> /**
317 </span> * Sets the header text for this Column.
318 * @param text The header to display on this Column.
320 setText: function(text) {
323 this.textEl.update(text);
327 // Find the topmost HeaderContainer: An ancestor which is NOT a Header.
328 // Group Headers are themselves HeaderContainers
329 getOwnerHeaderCt: function() {
330 return this.up(':not([isHeader])');
333 <span id='Ext-grid-column-Column-method-getIndex'> /**
334 </span> * Returns the true grid column index assiciated with this Column only if this column is a base level Column.
335 * If it is a group column, it returns <code>false</code>
337 getIndex: function() {
338 return this.isGroupColumn ? false : this.getOwnerHeaderCt().getHeaderIndex(this);
341 afterRender: function() {
345 me.callParent(arguments);
347 el.addCls(Ext.baseCSSPrefix + 'column-header-align-' + me.align).addClsOnOver(me.overCls);
351 dblclick: me.onElDblClick,
355 // BrowserBug: Ie8 Strict Mode, this will break the focus for this browser,
356 // must be fixed when focus management will be implemented.
357 if (!Ext.isIE8 || !Ext.isStrict) {
358 me.mon(me.getFocusEl(), {
359 focus: me.onTitleMouseOver,
360 blur: me.onTitleMouseOut,
365 me.mon(me.titleContainer, {
366 mouseenter: me.onTitleMouseOver,
367 mouseleave: me.onTitleMouseOut,
371 me.keyNav = Ext.create('Ext.util.KeyNav', el, {
372 enter: me.onEnterKey,
378 setSize: function(width, height) {
380 headerCt = me.ownerCt,
381 ownerHeaderCt = me.getOwnerHeaderCt(),
384 oldWidth = me.getWidth(),
386 readyForSizing = true,
390 if (width !== oldWidth) {
392 // Bubble size changes upwards to group headers
393 if (headerCt.isGroupHeader) {
394 siblings = headerCt.items.items;
395 len = siblings.length;
398 * setSize will be called for each column as it's rendered
399 * so we want to wait until all sub columns have been rendered
400 * before we try and calculate the size of the outer container.
401 * We also take into account hidden columns, because they won't
402 * be rendered, but we'll still need to make the calculation.
404 for (i = 0; i < len; i++) {
405 sibling = siblings[i];
406 hidden = sibling.hidden;
407 if (!sibling.rendered && !hidden) {
408 readyForSizing = false;
412 newWidth += (sibling === me) ? width : sibling.getWidth();
416 if (readyForSizing) {
417 headerCt.minWidth = newWidth;
418 headerCt.setWidth(newWidth);
421 me.callParent(arguments);
425 afterComponentLayout: function(width, height) {
427 ownerHeaderCt = this.getOwnerHeaderCt();
429 me.callParent(arguments);
431 // Only changes at the base level inform the grid's HeaderContainer which will update the View
432 // Skip this if the width is null or undefined which will be the Box layout's initial pass through the child Components
433 // Skip this if it's the initial size setting in which case there is no ownerheaderCt yet - that is set afterRender
434 if (width && !me.isGroupHeader && ownerHeaderCt) {
435 ownerHeaderCt.onHeaderResize(me, width, true);
440 // After the container has laid out and stretched, it calls this to correctly pad the inner to center the text vertically
441 setPadding: function() {
444 lineHeight = parseInt(me.textEl.getStyle('line-height'), 10);
446 // Top title containing element must stretch to match height of sibling group headers
447 if (!me.isGroupHeader) {
448 headerHeight = me.el.getViewSize().height;
449 if (me.titleContainer.getHeight() < headerHeight) {
450 me.titleContainer.dom.style.height = headerHeight + 'px';
453 headerHeight = me.titleContainer.getViewSize().height;
455 // Vertically center the header text in potentially vertically stretched header
457 me.titleContainer.setStyle({
458 paddingTop: Math.max(((headerHeight - lineHeight) / 2), 0) + 'px'
462 // Only IE needs this
463 if (Ext.isIE && me.triggerEl) {
464 me.triggerEl.setHeight(headerHeight);
468 onDestroy: function() {
470 Ext.destroy(me.keyNav);
472 me.callParent(arguments);
475 onTitleMouseOver: function() {
476 this.titleContainer.addCls(this.hoverCls);
479 onTitleMouseOut: function() {
480 this.titleContainer.removeCls(this.hoverCls);
483 onDownKey: function(e) {
484 if (this.triggerEl) {
485 this.onElClick(e, this.triggerEl.dom || this.el.dom);
489 onEnterKey: function(e) {
490 this.onElClick(e, this.el.dom);
493 <span id='Ext-grid-column-Column-method-onElDblClick'> /**
499 onElDblClick: function(e, t) {
501 ownerCt = me.ownerCt;
502 if (ownerCt && Ext.Array.indexOf(ownerCt.items, me) !== 0 && me.isOnLeftEdge(e) ) {
503 ownerCt.expandToFit(me.previousSibling('gridcolumn'));
507 onElClick: function(e, t) {
509 // The grid's docked HeaderContainer.
511 ownerHeaderCt = me.getOwnerHeaderCt();
513 if (ownerHeaderCt && !ownerHeaderCt.ddLock) {
514 // Firefox doesn't check the current target in a within check.
515 // Therefore we check the target directly and then within (ancestors)
516 if (me.triggerEl && (e.target === me.triggerEl.dom || t === me.triggerEl.dom || e.within(me.triggerEl))) {
517 ownerHeaderCt.onHeaderTriggerClick(me, e, t);
518 // if its not on the left hand edge, sort
519 } else if (e.getKey() || (!me.isOnLeftEdge(e) && !me.isOnRightEdge(e))) {
520 me.toggleSortState();
521 ownerHeaderCt.onHeaderClick(me, e, t);
526 <span id='Ext-grid-column-Column-method-processEvent'> /**
528 * Process UI events from the view. The owning TablePanel calls this method, relaying events from the TableView
529 * @param {String} type Event type, eg 'click'
530 * @param {TableView} view TableView Component
531 * @param {HtmlElement} cell Cell HtmlElement the event took place within
532 * @param {Number} recordIndex Index of the associated Store Model (-1 if none)
533 * @param {Number} cellIndex Cell index within the row
534 * @param {EventObject} e Original event
536 processEvent: function(type, view, cell, recordIndex, cellIndex, e) {
537 return this.fireEvent.apply(this, arguments);
540 toggleSortState: function() {
546 idx = Ext.Array.indexOf(me.possibleSortStates, me.sortState);
548 nextIdx = (idx + 1) % me.possibleSortStates.length;
549 me.setSortState(me.possibleSortStates[nextIdx]);
553 doSort: function(state) {
554 var ds = this.up('tablepanel').store;
556 property: this.getSortParam(),
561 <span id='Ext-grid-column-Column-method-getSortParam'> /**
562 </span> * Returns the parameter to sort upon when sorting this header. By default
563 * this returns the dataIndex and will not need to be overriden in most cases.
565 getSortParam: function() {
566 return this.dataIndex;
569 //setSortState: function(state, updateUI) {
570 //setSortState: function(state, doSort) {
571 setSortState: function(state, skipClear, initial) {
573 colSortClsPrefix = Ext.baseCSSPrefix + 'column-header-sort-',
574 ascCls = colSortClsPrefix + 'ASC',
575 descCls = colSortClsPrefix + 'DESC',
576 nullCls = colSortClsPrefix + 'null',
577 ownerHeaderCt = me.getOwnerHeaderCt(),
578 oldSortState = me.sortState;
580 if (oldSortState !== state && me.getSortParam()) {
581 me.addCls(colSortClsPrefix + state);
582 // don't trigger a sort on the first time, we just want to update the UI
583 if (state && !initial) {
588 me.removeCls([ascCls, nullCls]);
591 me.removeCls([descCls, nullCls]);
594 me.removeCls([ascCls, descCls]);
597 if (ownerHeaderCt && !me.triStateSort && !skipClear) {
598 ownerHeaderCt.clearOtherSortStates(me);
600 me.sortState = state;
601 ownerHeaderCt.fireEvent('sortchange', ownerHeaderCt, me, state);
611 ownerHeaderCt = me.getOwnerHeaderCt();
613 // Hiding means setting to zero width, so cache the width
614 me.oldWidth = me.getWidth();
616 // Hiding a group header hides itself, and then informs the HeaderContainer about its sub headers (Suppressing header layout)
617 if (me.isGroupHeader) {
618 items = me.items.items;
619 me.callParent(arguments);
620 ownerHeaderCt.onHeaderHide(me);
621 for (i = 0, len = items.length; i < len; i++) {
622 items[i].hidden = true;
623 ownerHeaderCt.onHeaderHide(items[i], true);
628 // TODO: Work with Jamie to produce a scheme where we can show/hide/resize without triggering a layout cascade
629 lb = me.ownerCt.componentLayout.layoutBusy;
630 me.ownerCt.componentLayout.layoutBusy = true;
631 me.callParent(arguments);
632 me.ownerCt.componentLayout.layoutBusy = lb;
634 // Notify owning HeaderContainer
635 ownerHeaderCt.onHeaderHide(me);
637 if (me.ownerCt.isGroupHeader) {
638 // If we've just hidden the last header in a group, then hide the group
639 items = me.ownerCt.query('>:not([hidden])');
643 // Size the group down to accommodate fewer sub headers
645 for (i = 0, len = items.length; i < len; i++) {
646 newWidth += items[i].getWidth();
648 me.ownerCt.minWidth = newWidth;
649 me.ownerCt.setWidth(newWidth);
656 ownerCt = me.getOwnerHeaderCt(),
662 // TODO: Work with Jamie to produce a scheme where we can show/hide/resize without triggering a layout cascade
663 lb = me.ownerCt.componentLayout.layoutBusy;
664 me.ownerCt.componentLayout.layoutBusy = true;
665 me.callParent(arguments);
666 me.ownerCt.componentLayout.layoutBusy = lb;
668 // If a sub header, ensure that the group header is visible
669 if (me.isSubHeader) {
670 if (!me.ownerCt.isVisible()) {
675 // If we've just shown a group with all its sub headers hidden, then show all its sub headers
676 if (me.isGroupHeader && !me.query(':not([hidden])').length) {
677 items = me.query('>*');
678 for (i = 0, len = items.length; i < len; i++) {
683 // Resize the owning group to accommodate
684 if (me.ownerCt.isGroupHeader) {
685 items = me.ownerCt.query('>:not([hidden])');
686 for (i = 0, len = items.length; i < len; i++) {
687 newWidth += items[i].getWidth();
689 me.ownerCt.minWidth = newWidth;
690 me.ownerCt.setWidth(newWidth);
693 // Notify owning HeaderContainer
695 ownerCt.onHeaderShow(me);
699 getDesiredWidth: function() {
701 if (me.rendered && me.componentLayout && me.componentLayout.lastComponentSize) {
702 // headers always have either a width or a flex
703 // because HeaderContainer sets a defaults width
704 // therefore we can ignore the natural width
705 // we use the componentLayout's tracked width so that
706 // we can calculate the desired width when rendered
707 // but not visible because its being obscured by a layout
708 return me.componentLayout.lastComponentSize.width;
709 // Flexed but yet to be rendered this could be the case
710 // where a HeaderContainer and Headers are simply used as data
711 // structures and not rendered.
714 // this is going to be wrong, the defaultWidth
722 getCellSelector: function() {
723 return '.' + Ext.baseCSSPrefix + 'grid-cell-' + this.getItemId();
726 getCellInnerSelector: function() {
727 return this.getCellSelector() + ' .' + Ext.baseCSSPrefix + 'grid-cell-inner';
730 isOnLeftEdge: function(e) {
731 return (e.getXY()[0] - this.el.getLeft() <= this.handleWidth);
734 isOnRightEdge: function(e) {
735 return (this.el.getRight() - e.getXY()[0] <= this.handleWidth);
738 <span id='Ext-grid-column-Column-method-getEditor'> /**
739 </span> * Retrieves the editing field for editing associated with this header. Returns false if there
740 * is no field associated with the Header the method will return false. If the
741 * field has not been instantiated it will be created. Note: These methods only has an implementation
742 * if a Editing plugin has been enabled on the grid.
743 * @param record The {@link Ext.data.Model Model} instance being edited.
744 * @param {Mixed} defaultField An object representing a default field to be created
745 * @returns {Ext.form.field.Field} field
748 // intentionally omit getEditor and setEditor definitions bc we applyIf into columns
749 // when the editing plugin is injected
752 <span id='Ext-grid-column-Column-method-setEditor'> /**
753 </span> * Sets the form field to be used for editing. Note: This method only has an implementation
754 * if an Editing plugin has been enabled on the grid.
755 * @param {Mixed} field An object representing a field to be created. If no xtype is specified a 'textfield' is assumed.