1 <!DOCTYPE html><html><head><title>Sencha Documentation Project</title><link rel="stylesheet" href="../reset.css" type="text/css"><link rel="stylesheet" href="../prettify.css" type="text/css"><link rel="stylesheet" href="../prettify_sa.css" type="text/css"><script type="text/javascript" src="../prettify.js"></script></head><body onload="prettyPrint()"><pre class="prettyprint"><pre><span id='Ext-grid.column.Column'>/**
2 </span> * @class Ext.grid.column.Column
3 * @extends Ext.grid.header.Container
5 * This class specifies the definition for a column inside a {@link Ext.grid.Panel}. It encompasses
6 * both the grid header configuration as well as displaying data within the grid itself. If the
7 * {@link #columns} configuration is specified, this column will become a column group and can
8 * container other columns inside. In general, this class will not be created directly, rather
9 * an array of column configurations will be passed to the grid:
11 * {@img Ext.grid.column.Column/Ext.grid.column.Column.png Ext.grid.column.Column grid column}
15 * Ext.create('Ext.data.Store', {
16 * storeId:'employeeStore',
17 * fields:['firstname', 'lastname', 'senority', 'dep', 'hired'],
19 * {firstname:"Michael", lastname:"Scott", senority:7, dep:"Manangement", hired:"01/10/2004"},
20 * {firstname:"Dwight", lastname:"Schrute", senority:2, dep:"Sales", hired:"04/01/2004"},
21 * {firstname:"Jim", lastname:"Halpert", senority:3, dep:"Sales", hired:"02/22/2006"},
22 * {firstname:"Kevin", lastname:"Malone", senority:4, dep:"Accounting", hired:"06/10/2007"},
23 * {firstname:"Angela", lastname:"Martin", senority:5, dep:"Accounting", hired:"10/21/2008"}
27 * Ext.create('Ext.grid.Panel', {
28 * title: 'Column Demo',
29 * store: Ext.data.StoreManager.lookup('employeeStore'),
31 * {text: 'First Name', dataIndex:'firstname'},
32 * {text: 'Last Name', dataIndex:'lastname'},
33 * {text: 'Hired Month', dataIndex:'hired', xtype:'datecolumn', format:'M'},
34 * {text: 'Deparment (Yrs)', xtype:'templatecolumn', tpl:'{dep} ({senority})'}
37 * renderTo: Ext.getBody()
40 * ## Convenience Subclasses
41 * There are several column subclasses that provide default rendering for various data types
43 * - {@link Ext.grid.column.Action}: Renders icons that can respond to click events inline
44 * - {@link Ext.grid.column.Boolean}: Renders for boolean values
45 * - {@link Ext.grid.column.Date}: Renders for date values
46 * - {@link Ext.grid.column.Number}: Renders for numeric values
47 * - {@link Ext.grid.column.Template}: Renders a value using an {@link Ext.XTemplate} using the record data
50 * The columns are laid out by a {@link Ext.layout.container.HBox} layout, so a column can either
51 * be given an explicit width value or a flex configuration. If no width is specified the grid will
52 * automatically the size the column to 100px. For column groups, the size is calculated by measuring
53 * the width of the child columns, so a width option should not be specified in that case.
56 * - {@link #text}: Sets the header text for the column
57 * - {@link #sortable}: Specifies whether the column can be sorted by clicking the header or using the column menu
58 * - {@link #hideable}: Specifies whether the column can be hidden using the column menu
59 * - {@link #menuDisabled}: Disables the column header menu
60 * - {@link #draggable}: Specifies whether the column header can be reordered by dragging
61 * - {@link #groupable}: Specifies whether the grid can be grouped by the column dataIndex. See also {@link Ext.grid.feature.Grouping}
64 * - {@link #dataIndex}: The dataIndex is the field in the underlying {@link Ext.data.Store} to use as the value for the column.
65 * - {@link #renderer}: Allows the underlying store value to be transformed before being displayed in the grid
69 Ext.define('Ext.grid.column.Column', {
70 extend: 'Ext.grid.header.Container',
71 alias: 'widget.gridcolumn',
72 requires: ['Ext.util.KeyNav'],
73 alternateClassName: 'Ext.grid.Column',
75 baseCls: Ext.baseCSSPrefix + 'column-header ' + Ext.baseCSSPrefix + 'unselectable',
77 // Not the standard, automatically applied overCls because we must filter out overs of child headers.
78 hoverCls: Ext.baseCSSPrefix + 'column-header-over',
84 possibleSortStates: ['ASC', 'DESC'],
87 '<div class="' + Ext.baseCSSPrefix + 'column-header-inner">' +
88 '<span class="' + Ext.baseCSSPrefix + 'column-header-text">' +
91 '<tpl if="!values.menuDisabled"><div class="' + Ext.baseCSSPrefix + 'column-header-trigger"></div></tpl>' +
94 <span id='Ext-grid.column.Column-cfg-columns'> /**
95 </span> * @cfg {Array} columns
96 * <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>
97 * <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
98 * if all sub columns are dragged out of a group, the group is destroyed.
101 <span id='Ext-grid.column.Column-cfg-dataIndex'> /**
102 </span> * @cfg {String} dataIndex <p><b>Required</b>. The name of the field in the
103 * grid's {@link Ext.data.Store}'s {@link Ext.data.Model} definition from
104 * which to draw the column's value.</p>
108 <span id='Ext-grid.column.Column-cfg-text'> /**
109 </span> * @cfg {String} text Optional. The header text to be used as innerHTML
110 * (html tags are accepted) to display in the Grid. <b>Note</b>: to
111 * have a clickable header with no text displayed you can use the
112 * default of <tt>'&#160;'</tt>.
116 <span id='Ext-grid.column.Column-cfg-sortable'> /**
117 </span> * @cfg {Boolean} sortable Optional. <tt>true</tt> if sorting is to be allowed on this column.
118 * Whether local/remote sorting is used is specified in <code>{@link Ext.data.Store#remoteSort}</code>.
122 <span id='Ext-grid.column.Column-cfg-groupable'> /**
123 </span> * @cfg {Boolean} groupable Optional. If the grid uses a {@link Ext.grid.feature.Grouping}, this option
124 * may be used to disable the header menu item to group by the column selected. By default,
125 * the header menu group option is enabled. Set to false to disable (but still show) the
126 * group option in the header menu for the column.
129 <span id='Ext-grid.column.Column-cfg-hideable'> /**
130 </span> * @cfg {Boolean} hideable Optional. Specify as <tt>false</tt> to prevent the user from hiding this column
131 * (defaults to true).
135 <span id='Ext-grid.column.Column-cfg-menuDisabled'> /**
136 </span> * @cfg {Boolean} menuDisabled
137 * True to disabled the column header menu containing sort/hide options. Defaults to false.
141 <span id='Ext-grid.column.Column-cfg-renderer'> /**
142 </span> * @cfg {Function} renderer
143 * <p>A renderer is an 'interceptor' method which can be used transform data (value, appearance, etc.) before it
144 * is rendered. Example:</p>
145 * <pre><code>{
146 renderer: function(value){
150 return value + ' people';
153 * </code></pre>
154 * @param {Mixed} value The data value for the current cell
155 * @param {Object} metaData A collection of metadata about the current cell; can be used or modified by
156 * the renderer. Recognized properties are: <tt>tdCls</tt>, <tt>tdAttr</tt>, and <tt>style</tt>.
157 * @param {Ext.data.Model} record The record for the current row
158 * @param {Number} rowIndex The index of the current row
159 * @param {Number} colIndex The index of the current column
160 * @param {Ext.data.Store} store The data store
161 * @param {Ext.view.View} view The current view
162 * @return {String} The HTML to be rendered
166 <span id='Ext-grid.column.Column-cfg-align'> /**
167 </span> * @cfg {String} align Sets the alignment of the header and rendered columns.
168 * Defaults to 'left'.
172 <span id='Ext-grid.column.Column-cfg-draggable'> /**
173 </span> * @cfg {Boolean} draggable Indicates whether or not the header can be drag and drop re-ordered.
178 // Header does not use the typical ComponentDraggable class and therefore we
179 // override this with an emptyFn. It is controlled at the HeaderDragZone.
180 initDraggable: Ext.emptyFn,
182 <span id='Ext-grid.column.Column-cfg-tdCls'> /**
183 </span> * @cfg {String} tdCls <p>Optional. A CSS class names to apply to the table cells for this column.</p>
186 <span id='Ext-grid.column.Column-property-triggerEl'> /**
187 </span> * @property {Ext.core.Element} triggerEl
190 <span id='Ext-grid.column.Column-property-textEl'> /**
191 </span> * @property {Ext.core.Element} textEl
194 <span id='Ext-grid.column.Column-property-isHeader'> /**
196 * Set in this class to identify, at runtime, instances which are not instances of the
197 * HeaderContainer base class, but are in fact, the subclass: Header.
201 initComponent: function() {
206 if (Ext.isDefined(me.header)) {
211 // Flexed Headers need to have a minWidth defined so that they can never be squeezed out of existence by the
212 // HeaderContainer's specialized Box layout, the ColumnLayout. The ColumnLayout's overridden calculateChildboxes
213 // method extends the available layout space to accommodate the "desiredWidth" of all the columns.
215 me.minWidth = me.minWidth || Ext.grid.plugin.HeaderResizer.prototype.minColWidth;
217 // Non-flexed Headers may never be squeezed in the event of a shortfall so
218 // always set their minWidth to their current width.
220 me.minWidth = me.width;
223 if (!me.triStateSort) {
224 me.possibleSortStates.length = 2;
227 // A group header; It contains items which are themselves Headers
228 if (Ext.isDefined(me.columns)) {
229 me.isGroupHeader = true;
233 Ext.Error.raise('Ext.grid.column.Column: Group header may not accept a dataIndex');
235 if ((me.width && me.width !== Ext.grid.header.Container.prototype.defaultWidth) || me.flex) {
236 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.');
240 // The headers become child items
241 me.items = me.columns;
246 // Acquire initial width from sub headers
247 for (i = 0, len = me.items.length; i < len; i++) {
248 me.width += me.items[i].width || Ext.grid.header.Container.prototype.defaultWidth;
250 if (me.items[i].flex) {
251 Ext.Error.raise('Ext.grid.column.Column: items of a grouped header do not support flexed values. Each item must explicitly define its width.');
255 me.minWidth = me.width;
257 me.cls = (me.cls||'') + ' ' + Ext.baseCSSPrefix + 'group-header';
263 Ext.applyIf(me.renderSelectors, {
264 titleContainer: '.' + Ext.baseCSSPrefix + 'column-header-inner',
265 triggerEl: '.' + Ext.baseCSSPrefix + 'column-header-trigger',
266 textEl: '.' + Ext.baseCSSPrefix + 'column-header-text'
269 // Initialize as a HeaderContainer
270 me.callParent(arguments);
273 onAdd: function(childHeader) {
274 childHeader.isSubHeader = true;
275 childHeader.addCls(Ext.baseCSSPrefix + 'group-sub-header');
278 onRemove: function(childHeader) {
279 childHeader.isSubHeader = false;
280 childHeader.removeCls(Ext.baseCSSPrefix + 'group-sub-header');
283 initRenderData: function() {
286 Ext.applyIf(me.renderData, {
288 menuDisabled: me.menuDisabled
290 return me.callParent(arguments);
293 // note that this should invalidate the menu cache
294 setText: function(text) {
297 this.textEl.update(text);
301 // Find the topmost HeaderContainer: An ancestor which is NOT a Header.
302 // Group Headers are themselves HeaderContainers
303 getOwnerHeaderCt: function() {
304 return this.up(':not([isHeader])');
307 <span id='Ext-grid.column.Column-method-getIndex'> /**
308 </span> * Returns the true grid column index assiciated with this Column only if this column is a base level Column.
309 * If it is a group column, it returns <code>false</code>
311 getIndex: function() {
312 return this.isGroupColumn ? false : this.getOwnerHeaderCt().getHeaderIndex(this);
315 afterRender: function() {
319 me.callParent(arguments);
321 el.addCls(Ext.baseCSSPrefix + 'column-header-align-' + me.align).addClsOnOver(me.overCls);
325 dblclick: me.onElDblClick,
329 // BrowserBug: Ie8 Strict Mode, this will break the focus for this browser,
330 // must be fixed when focus management will be implemented.
331 if (!Ext.isIE8 || !Ext.isStrict) {
332 me.mon(me.getFocusEl(), {
333 focus: me.onTitleMouseOver,
334 blur: me.onTitleMouseOut,
339 me.mon(me.titleContainer, {
340 mouseenter: me.onTitleMouseOver,
341 mouseleave: me.onTitleMouseOut,
345 me.keyNav = Ext.create('Ext.util.KeyNav', el, {
346 enter: me.onEnterKey,
352 setSize: function(width, height) {
354 headerCt = me.ownerCt,
355 ownerHeaderCt = me.getOwnerHeaderCt(),
358 oldWidth = me.getWidth(),
361 if (width !== oldWidth) {
363 // Bubble size changes upwards to group headers
364 if (headerCt.isGroupHeader) {
366 siblings = headerCt.items.items;
367 len = siblings.length;
369 // Size the owning group to the size of its sub headers
370 if (siblings[len - 1].rendered) {
372 for (i = 0; i < len; i++) {
373 newWidth += (siblings[i] === me) ? width : siblings[i].getWidth();
375 headerCt.minWidth = newWidth;
376 headerCt.setWidth(newWidth);
379 me.callParent(arguments);
383 afterComponentLayout: function(width, height) {
385 ownerHeaderCt = this.getOwnerHeaderCt();
387 me.callParent(arguments);
389 // Only changes at the base level inform the grid's HeaderContainer which will update the View
390 // Skip this if the width is null or undefined which will be the Box layout's initial pass through the child Components
391 // Skip this if it's the initial size setting in which case there is no ownerheaderCt yet - that is set afterRender
392 if (width && !me.isGroupHeader && ownerHeaderCt) {
393 ownerHeaderCt.onHeaderResize(me, width, true);
398 // After the container has laid out and stretched, it calls this to correctly pad the inner to center the text vertically
399 setPadding: function() {
402 lineHeight = parseInt(me.textEl.getStyle('line-height'), 10);
404 // Top title containing element must stretch to match height of sibling group headers
405 if (!me.isGroupHeader) {
406 headerHeight = me.el.getViewSize().height;
407 if (me.titleContainer.getHeight() < headerHeight) {
408 me.titleContainer.dom.style.height = headerHeight + 'px';
411 headerHeight = me.titleContainer.getViewSize().height;
413 // Vertically center the header text in potentially vertically stretched header
415 me.titleContainer.setStyle({
416 paddingTop: Math.max(((headerHeight - lineHeight) / 2), 0) + 'px'
420 // Only IE needs this
421 if (Ext.isIE && me.triggerEl) {
422 me.triggerEl.setHeight(headerHeight);
426 onDestroy: function() {
428 Ext.destroy(me.keyNav);
430 me.callParent(arguments);
433 onTitleMouseOver: function() {
434 this.titleContainer.addCls(this.hoverCls);
437 onTitleMouseOut: function() {
438 this.titleContainer.removeCls(this.hoverCls);
441 onDownKey: function(e) {
442 if (this.triggerEl) {
443 this.onElClick(e, this.triggerEl.dom || this.el.dom);
447 onEnterKey: function(e) {
448 this.onElClick(e, this.el.dom);
451 <span id='Ext-grid.column.Column-method-onElDblClick'> /**
457 onElDblClick: function(e, t) {
459 ownerCt = me.ownerCt;
460 if (ownerCt && Ext.Array.indexOf(ownerCt.items, me) !== 0 && me.isOnLeftEdge(e) ) {
461 ownerCt.expandToFit(me.previousSibling('gridcolumn'));
465 onElClick: function(e, t) {
467 // The grid's docked HeaderContainer.
469 ownerHeaderCt = me.getOwnerHeaderCt();
471 if (ownerHeaderCt && !ownerHeaderCt.ddLock) {
472 // Firefox doesn't check the current target in a within check.
473 // Therefore we check the target directly and then within (ancestors)
474 if (me.triggerEl && (e.target === me.triggerEl.dom || t === me.triggerEl.dom || e.within(me.triggerEl))) {
475 ownerHeaderCt.onHeaderTriggerClick(me, e, t);
476 // if its not on the left hand edge, sort
477 } else if (e.getKey() || (!me.isOnLeftEdge(e) && !me.isOnRightEdge(e))) {
478 me.toggleSortState();
479 ownerHeaderCt.onHeaderClick(me, e, t);
484 <span id='Ext-grid.column.Column-method-processEvent'> /**
486 * Process UI events from the view. The owning TablePanel calls this method, relaying events from the TableView
487 * @param {String} type Event type, eg 'click'
488 * @param {TableView} view TableView Component
489 * @param {HtmlElement} cell Cell HtmlElement the event took place within
490 * @param {Number} recordIndex Index of the associated Store Model (-1 if none)
491 * @param {Number} cellIndex Cell index within the row
492 * @param {EventObject} e Original event
494 processEvent: function(type, view, cell, recordIndex, cellIndex, e) {
495 return this.fireEvent.apply(this, arguments);
498 toggleSortState: function() {
504 idx = Ext.Array.indexOf(me.possibleSortStates, me.sortState);
506 nextIdx = (idx + 1) % me.possibleSortStates.length;
507 me.setSortState(me.possibleSortStates[nextIdx]);
511 doSort: function(state) {
512 var ds = this.up('tablepanel').store;
514 property: this.getSortParam(),
519 <span id='Ext-grid.column.Column-method-getSortParam'> /**
520 </span> * Returns the parameter to sort upon when sorting this header. By default
521 * this returns the dataIndex and will not need to be overriden in most cases.
523 getSortParam: function() {
524 return this.dataIndex;
527 //setSortState: function(state, updateUI) {
528 //setSortState: function(state, doSort) {
529 setSortState: function(state, skipClear, initial) {
531 colSortClsPrefix = Ext.baseCSSPrefix + 'column-header-sort-',
532 ascCls = colSortClsPrefix + 'ASC',
533 descCls = colSortClsPrefix + 'DESC',
534 nullCls = colSortClsPrefix + 'null',
535 ownerHeaderCt = me.getOwnerHeaderCt(),
536 oldSortState = me.sortState;
538 if (oldSortState !== state && me.getSortParam()) {
539 me.addCls(colSortClsPrefix + state);
540 // don't trigger a sort on the first time, we just want to update the UI
541 if (state && !initial) {
546 me.removeCls([ascCls, nullCls]);
549 me.removeCls([descCls, nullCls]);
552 me.removeCls([ascCls, descCls]);
555 if (ownerHeaderCt && !me.triStateSort && !skipClear) {
556 ownerHeaderCt.clearOtherSortStates(me);
558 me.sortState = state;
559 ownerHeaderCt.fireEvent('sortchange', ownerHeaderCt, me, state);
569 ownerHeaderCt = me.getOwnerHeaderCt();
571 // Hiding means setting to zero width, so cache the width
572 me.oldWidth = me.getWidth();
574 // Hiding a group header hides itself, and then informs the HeaderContainer about its sub headers (Suppressing header layout)
575 if (me.isGroupHeader) {
576 items = me.items.items;
577 me.callParent(arguments);
578 ownerHeaderCt.onHeaderHide(me);
579 for (i = 0, len = items.length; i < len; i++) {
580 items[i].hidden = true;
581 ownerHeaderCt.onHeaderHide(items[i], true);
586 // TODO: Work with Jamie to produce a scheme where we can show/hide/resize without triggering a layout cascade
587 lb = me.ownerCt.componentLayout.layoutBusy;
588 me.ownerCt.componentLayout.layoutBusy = true;
589 me.callParent(arguments);
590 me.ownerCt.componentLayout.layoutBusy = lb;
592 // Notify owning HeaderContainer
593 ownerHeaderCt.onHeaderHide(me);
595 if (me.ownerCt.isGroupHeader) {
596 // If we've just hidden the last header in a group, then hide the group
597 items = me.ownerCt.query('>:not([hidden])');
601 // Size the group down to accommodate fewer sub headers
603 for (i = 0, len = items.length; i < len; i++) {
604 newWidth += items[i].getWidth();
606 me.ownerCt.minWidth = newWidth;
607 me.ownerCt.setWidth(newWidth);
614 ownerCt = me.getOwnerHeaderCt(),
620 // TODO: Work with Jamie to produce a scheme where we can show/hide/resize without triggering a layout cascade
621 lb = me.ownerCt.componentLayout.layoutBusy;
622 me.ownerCt.componentLayout.layoutBusy = true;
623 me.callParent(arguments);
624 me.ownerCt.componentLayout.layoutBusy = lb;
626 // If a sub header, ensure that the group header is visible
627 if (me.isSubHeader) {
628 if (!me.ownerCt.isVisible()) {
633 // If we've just shown a group with all its sub headers hidden, then show all its sub headers
634 if (me.isGroupHeader && !me.query(':not([hidden])').length) {
635 items = me.query('>*');
636 for (i = 0, len = items.length; i < len; i++) {
641 // Resize the owning group to accommodate
642 if (me.ownerCt.isGroupHeader) {
643 items = me.ownerCt.query('>:not([hidden])');
644 for (i = 0, len = items.length; i < len; i++) {
645 newWidth += items[i].getWidth();
647 me.ownerCt.minWidth = newWidth;
648 me.ownerCt.setWidth(newWidth);
651 // Notify owning HeaderContainer
653 ownerCt.onHeaderShow(me);
657 getDesiredWidth: function() {
659 if (me.rendered && me.componentLayout && me.componentLayout.lastComponentSize) {
660 // headers always have either a width or a flex
661 // because HeaderContainer sets a defaults width
662 // therefore we can ignore the natural width
663 // we use the componentLayout's tracked width so that
664 // we can calculate the desired width when rendered
665 // but not visible because its being obscured by a layout
666 return me.componentLayout.lastComponentSize.width;
667 // Flexed but yet to be rendered this could be the case
668 // where a HeaderContainer and Headers are simply used as data
669 // structures and not rendered.
672 // this is going to be wrong, the defaultWidth
680 getCellSelector: function() {
681 return '.' + Ext.baseCSSPrefix + 'grid-cell-' + this.getItemId();
684 getCellInnerSelector: function() {
685 return this.getCellSelector() + ' .' + Ext.baseCSSPrefix + 'grid-cell-inner';
688 isOnLeftEdge: function(e) {
689 return (e.getXY()[0] - this.el.getLeft() <= this.handleWidth);
692 isOnRightEdge: function(e) {
693 return (this.el.getRight() - e.getXY()[0] <= this.handleWidth);
696 <span id='Ext-grid.column.Column-method-getEditor'> /**
697 </span> * Retrieves the editing field for editing associated with this header. Returns false if there
698 * is no field associated with the Header the method will return false. If the
699 * field has not been instantiated it will be created. Note: These methods only has an implementation
700 * if a Editing plugin has been enabled on the grid.
701 * @param record The {@link Ext.data.Model Model} instance being edited.
702 * @param {Mixed} defaultField An object representing a default field to be created
703 * @returns {Ext.form.field.Field} field
706 // intentionally omit getEditor and setEditor definitions bc we applyIf into columns
707 // when the editing plugin is injected
710 <span id='Ext-grid.column.Column-method-setEditor'> /**
711 </span> * Sets the form field to be used for editing. Note: This method only has an implementation
712 * if an Editing plugin has been enabled on the grid.
713 * @param {Mixed} field An object representing a field to be created. If no xtype is specified a 'textfield' is assumed.
716 });</pre></pre></body></html>