Ext.grid.ColumnModel
Hierarchy
Ext.util.ObservableExt.grid.ColumnModelSubclasses
Files
After the data has been read into the client side cache (Store), the ColumnModel is used to configure how and what parts of that data will be displayed in the vertical slices (columns) of the grid. The Ext.grid.ColumnModel Class is the default implementation of a ColumnModel used by implentations of GridPanel.
Data is mapped into the store's records and then indexed into the ColumnModel using the dataIndex:
{data source} == mapping ==> {data store} == dataIndex ==> {ColumnModel}
Each Column in the grid's ColumnModel is configured with a dataIndex to specify how the data within each record in the store is indexed into the ColumnModel.
There are two ways to initialize the ColumnModel class:
Initialization Method 1: an Array
var colModel = new Ext.grid.ColumnModel([
{ header: "Ticker", width: 60, sortable: true},
{ header: "Company Name", width: 150, sortable: true, id: 'company'},
{ header: "Market Cap.", width: 100, sortable: true},
{ header: "$ Sales", width: 100, sortable: true, renderer: money},
{ header: "Employees", width: 100, sortable: true, resizable: false}
]);
The ColumnModel may be initialized with an Array of Ext.grid.Column column configuration objects to define the initial layout / display of the columns in the Grid. The order of each Ext.grid.Column column configuration object within the specified Array defines the initial order of the column display. A Column's display may be initially hidden using the hidden config property (and then shown using the column header menu). Fields that are not included in the ColumnModel will not be displayable at all.
How each column in the grid correlates (maps) to the Ext.data.Record field in the Store the column draws its data from is configured through the dataIndex. If the dataIndex is not explicitly defined (as shown in the example above) it will use the column configuration's index in the Array as the index.
See Ext.grid.Column for additional configuration options for each column.
Initialization Method 2: an Object
In order to use configuration options from Ext.grid.ColumnModel, an Object may be used to initialize the ColumnModel. The column configuration Array will be specified in the columns config property. The defaults config property can be used to apply defaults for all columns, e.g.:
var colModel = new Ext.grid.ColumnModel({
columns: [
{ header: "Ticker", width: 60, menuDisabled: false},
{ header: "Company Name", width: 150, id: 'company'},
{ header: "Market Cap."},
{ header: "$ Sales", renderer: money},
{ header: "Employees", resizable: false}
],
defaults: {
sortable: true,
menuDisabled: true,
width: 100
},
listeners: {
hiddenchange: function(cm, colIndex, hidden) {
saveConfig(colIndex, hidden);
}
}
});
In both examples above, the ability to apply a CSS class to all cells in a column (including the header) is demonstrated through the use of the id config option. This column could be styled by including the following css:
//add this css *after* the core css is loaded
.x-grid3-td-company {
color: red; // entire column will have red font
}
// modify the header row only, adding an icon to the column header
.x-grid3-hd-company {
background: transparent
url(../../resources/images/icons/silk/building.png)
no-repeat 3px 3px ! important;
padding-left:20px;
}
Note that the "Company Name" column could be specified as the Ext.grid.GridPanel.autoExpandColumn.
Available since: 1.1.0
Config options
An Array of object literals. The config options defined by Ext.grid.Column are the options which may appear in the object literal for each individual column definition.
Available since: Ext JS 3.4.0
(optional) Default sortable of columns which have no sortable specified (defaults to false). This property shall preferably be configured through the defaults config property.
Defaults to: false
Available since: Ext JS 3.4.0
(optional) The width of columns which have no width specified (defaults to 100). This property shall preferably be configured through the defaults config property.
Defaults to: 100
Available since: Ext JS 3.4.0
Object literal which will be used to apply Ext.grid.Column configuration options to all columns. Configuration options specified with individual column configs will supersede these defaults.
Available since: Ext JS 3.4.0
(optional)
A config object containing one or more event handlers to be added to this object during initialization. This should be a valid listeners config object as specified in the addListener example for attaching multiple handlers at once.
DOM events from ExtJs Components
While some ExtJs Component classes export selected DOM events (e.g. "click", "mouseover" etc), this
is usually only done when extra value can be added. For example the DataView's
click event passing the node clicked on. To access DOM
events directly from a Component's HTMLElement, listeners must be added to the Element after the Component
has been rendered. A plugin can simplify this step:
// Plugin is configured with a listeners config object.
// The Component is appended to the argument list of all handler functions.
Ext.DomObserver = Ext.extend(Object, {
constructor: function(config) {
this.listeners = config.listeners ? config.listeners : config;
},
// Component passes itself into plugin's init method
init: function(c) {
var p, l = this.listeners;
for (p in l) {
if (Ext.isFunction(l[p])) {
l[p] = this.createHandler(l[p], c);
} else {
l[p].fn = this.createHandler(l[p].fn, c);
}
}
// Add the listeners to the Element immediately following the render call
c.render = c.render.createSequence(function() {
var e = c.getEl();
if (e) {
e.on(l);
}
});
},
createHandler: function(fn, c) {
return function(e) {
fn.call(this, e, c);
};
}
});
var combo = new Ext.form.ComboBox({
// Collapse combo when its element is clicked on
plugins: [ new Ext.DomObserver({
click: function(evt, comp) {
comp.collapse();
}
})],
store: myStore,
typeAhead: true,
mode: 'local',
triggerAction: 'all'
});
Available since: 1.1.0
Properties
An Array of Column definition objects representing the configuration of this ColumnModel. See Ext.grid.Column for the configuration properties that may be specified.
Available since: 1.1.0
Methods
Available since: 1.1.0
Parameters
- config : Mixed
Specify either an Array of Ext.grid.Column configuration objects or specify a configuration Object (see introductory section discussion utilizing Initialization Method 2 above).
Returns
Adds the specified events to the list of events which this Observable may fire.
Available since: 1.1.0
Parameters
- o : Object|String
Either an object with event names as properties with a value of
trueor the first event name string if multiple event names are being passed as separate parameters. - Optional : string
. Event name if multiple event names are being passed as separate parameters. Usage:
this.addEvents('storeloaded', 'storecleared');
Appends an event handler to this object.
Available since: 1.1.0
Parameters
- eventName : String
The name of the event to listen for.
- handler : Function
The method the event invokes.
- scope : Object (optional)
The scope (
thisreference) in which the handler function is executed. If omitted, defaults to the object which fired the event. - options : Object (optional)
An object containing handler configuration. properties. This may contain any of the following properties:
- scope : ObjectThe scope (
thisreference) in which the handler function is executed. If omitted, defaults to the object which fired the event. - delay : NumberThe number of milliseconds to delay the invocation of the handler after the event fires.
- single : BooleanTrue to add a handler to handle just the next firing of the event, and then remove itself.
- buffer : NumberCauses the handler to be scheduled to run in an Ext.util.DelayedTask delayed by the specified number of milliseconds. If the event fires again within that time, the original handler is not invoked, but the new handler is scheduled in its place.
- target : ObservableOnly call the handler if the event was fired on the target Observable, not if the event was bubbled up from a child Observable.
Combining Options
Using the options argument, it is possible to combine different types of listeners:
A delayed, one-time listener.myDataView.on('click', this.onClick, this, { single: true, delay: 100 });Attaching multiple handlers in 1 call
The method also allows for a single argument to be passed which is a config object containing properties which specify multiple handlers.myGridPanel.on({ 'click' : { fn: this.onClick, scope: this, delay: 100 }, 'mouseover' : { fn: this.onMouseOver, scope: this }, 'mouseout' : { fn: this.onMouseOut, scope: this } });Or a shorthand syntax:
myGridPanel.on({ 'click' : this.onClick, 'mouseover' : this.onMouseOver, 'mouseout' : this.onMouseOut, scope: this }); - scope : Object
Destroys this column model by purging any event listeners. Destroys and dereferences all Columns.
Available since: 2.3.0
Enables events fired by this Observable to bubble up an owner hierarchy by calling
this.getBubbleTarget() if present. There is no implementation in the Observable base class.
This is commonly used by Ext.Components to bubble events to owner Containers. See Ext.Component.getBubbleTarget. The default implementation in Ext.Component returns the Component's immediate owner. But if a known target is required, this can be overridden to access the required target more quickly.
Example:
Ext.override(Ext.form.Field, {
// Add functionality to Field's initComponent to enable the change event to bubble
initComponent : Ext.form.Field.prototype.initComponent.createSequence(function() {
this.enableBubble('change');
}),
// We know that we want Field's events to bubble directly to the FormPanel.
getBubbleTarget : function() {
if (!this.formPanel) {
this.formPanel = this.findParentByType('form');
}
return this.formPanel;
}
});
var myForm = new Ext.formPanel({
title: 'User Details',
items: [{
...
}],
listeners: {
change: function() {
// Title goes red if form has been modified.
myForm.header.setStyle('color', 'red');
}
}
});
Available since: Ext JS 3.4.0
Parameters
Fires the specified event with the passed parameters (minus the event name).
An event may be set to bubble up an Observable parent hierarchy (See Ext.Component.getBubbleTarget) by calling enableBubble.
Available since: 1.1.0
Parameters
- eventName : String
The name of the event to fire.
- args : Object...
Variable number of parameters are passed to handlers.
Returns
- Boolean
returns false if any of the handlers return false otherwise it returns true.
Returns the editor defined for the cell/column.
Available since: 1.1.0
Parameters
Returns
Returns the column for a specified id.
Available since: 1.1.0
Parameters
- id : String
The column id
Returns
- Object
the column
Returns the number of columns.
Available since: 1.1.0
Parameters
- visibleOnly : Boolean
Optional. Pass as true to only include visible columns.
Returns
Returns the column configs that return true by the passed function that is called with (columnConfig, index)
// returns an array of column config objects for all hidden columns
var columns = grid.getColumnModel().getColumnsBy(function(c){
return c.hidden;
});
Available since: 1.1.0
Parameters
- fn : Function
A function which, when passed a Column object, must return
trueif the column is to be included in the returned Array. - scope : Object (optional)
The scope (
thisreference) in which the function is executed. Defaults to this ColumnModel.
Returns
- Array
result
Returns the rendering (formatting) function defined for the column.
Available since: 1.1.0
Parameters
- col : Number
The column index.
Returns
- Function
The function used to render the cell. See setRenderer.
Returns the total width of all columns.
Available since: 1.1.0
Parameters
- includeHidden : Boolean
True to include hidden column widths
Returns
Checks to see if this object has any listeners for a specified event
Available since: 1.1.0
Parameters
- eventName : String
The name of the event to check for
Returns
- Boolean
True if the event is being listened for, else false
Returns true if the cell is editable.
var store = new Ext.data.Store({...});
var colModel = new Ext.grid.ColumnModel({
columns: [...],
isCellEditable: function(col, row) {
var record = store.getAt(row);
if (record.get('readonly')) { // replace with your condition
return false;
}
return Ext.grid.ColumnModel.prototype.isCellEditable.call(this, col, row);
}
});
var grid = new Ext.grid.GridPanel({
store: store,
colModel: colModel,
...
});
Available since: 1.1.0
Parameters
Returns
- Boolean
Returns true if the specified column menu is disabled.
Available since: 2.3.0
Parameters
- col : Number
The column index
Returns
- Boolean
Returns true if the column can be resized
Available since: 1.1.0
Parameters
- colIndex : Object
Returns
- Boolean
Returns true if the specified column is sortable.
Available since: 1.1.0
Parameters
- col : Number
The column index
Returns
- Boolean
Appends an event handler to this object (shorthand for addListener.)
Available since: 1.1.0
Parameters
- eventName : String
The type of event to listen for
- handler : Function
The method the event invokes
- scope : Object (optional)
The scope (
thisreference) in which the handler function is executed. If omitted, defaults to the object which fired the event. - options : Object (optional)
An object containing handler configuration.
Removes all listeners for this object
Available since: 1.1.0
Relays selected events from the specified Observable as if the events were fired by this.
Available since: 2.3.0
Parameters
- o : Object
The Observable whose events this object is to relay.
- events : Array
Array of event names to relay.
Removes an event handler.
Available since: 1.1.0
Parameters
- eventName : String
The type of event the handler was associated with.
- handler : Function
The handler to remove. This must be a reference to the function passed into the addListener call.
- scope : Object (optional)
The scope originally specified for the handler.
Resume firing events. (see suspendEvents) If events were suspended using the queueSuspended parameter, then all events fired during event suspension will be sent to any listeners now.
Available since: 2.3.0
Sets the width for a column.
Available since: 1.1.0
Parameters
- col : Number
The column index
- width : Number
The new width
- suppressEvent : Boolean
True to suppress firing the
widthchangeevent. Defaults to false.
Reconfigures this column model according to the passed Array of column definition objects. For a description of the individual properties of a column definition object, see the Config Options.
Causes the configchange event to be fired. A GridPanel using this ColumnModel will listen for this event and refresh its UI automatically.
Available since: 2.3.0
Parameters
- config : Array
Array of Column definition objects.
- initial : Boolean
Specify true to bypass cleanup which deletes the totalWidth and destroys existing editors.
Sets if a column is editable.
Available since: 1.1.0
Parameters
- col : Number
The column index
- editable : Boolean
True if the column is editable
Sets the editor for a column and destroys the prior editor.
Available since: 1.1.0
Parameters
- col : Number
The column index
- editor : Object
The editor object
Sets if a column is hidden.
myGrid.getColumnModel().setHidden(0, true); // hide column 0 (0 = the first column).
Available since: 1.1.0
Parameters
- colIndex : Number
The column index
- hidden : Boolean
True if the column is hidden
Sets the rendering (formatting) function for a column. See Ext.util.Format for some default formatting functions.
Available since: 1.1.0
Parameters
- col : Number
The column index
- fn : Function
The function to use to process the cell's raw data to return HTML markup for the grid view. The render function is called with the following parameters:
- value : Object
The data value for the cell.
- metadata : Object
An object in which you may set the following attributes:
- css : String
A CSS class name to add to the cell's TD element.
- attr : String
An HTML attribute definition string to apply to the data container element within the table cell (e.g. 'style="color:red;"').
- css : String
- record : Ext.data.record
The Ext.data.Record from which the data was extracted.
- rowIndex : Number
Row index
- colIndex : Number
Column index
- store : Ext.data.Store
The Ext.data.Store object from which the Record was extracted.
- value : Object
Setup any saved state for the column, ensures that defaults are applied.
Available since: Ext JS 3.4.0
Parameters
- col : Object
- state : Object
Suspend the firing of all events. (see resumeEvents)
Available since: 2.3.0
Parameters
- queueSuspended : Boolean
Pass as true to queue up suspended events to be fired after the resumeEvents call instead of discarding all suspended events;
Removes an event handler (shorthand for removeListener.)
Available since: 1.1.0
Parameters
- eventName : String
The type of event the handler was associated with.
- handler : Function
The handler to remove. This must be a reference to the function passed into the addListener call.
- scope : Object (optional)
The scope originally specified for the handler.
Events
Fires when the configuration is changed
Available since: 2.3.0
Parameters
- this : ColumnModel
Fires when the width of a column is programmaticially changed using
setColumnWidth.
Note internal resizing suppresses the event from firing. See also
Ext.grid.GridPanel.columnresize.
Available since: 1.1.0