zombeerose
13 Jul 2011, 10:20 AM
Here is a simple Ext 4.0 grid column for displaying a progress bar. Values should be any number between 0 and 1 (e.g. 0.5), which is just like Ext.window.MessageBox#updateProgress
/**
* @class Ext.ux.grid.column.Progress
* @extends Ext.grid.Column
* <p>
* A Grid column type which renders a numeric value as a progress bar.
* </p>
* <p>
* <b>Notes:</b><ul>
* <li>Compatible with Ext 4.0</li>
* </ul>
* </p>
* Example usage:
* <pre><code>
var grid = new Ext.grid.Panel({
columns: [{
dataIndex: 'progress'
,xtype: 'progresscolumn'
},{
...
]}
...
});
* </code></pre>
* <p>The column can be at any index in the columns array, and a grid can have any number of progress columns.</p>
* @author Phil Crawford
* @license Licensed under the terms of the Open Source <a href="http://www.gnu.org/licenses/lgpl.html">LGPL 3.0 license</a>. Commercial use is permitted to the extent that the code/component(s) do NOT become part of another Open Source or Commercially licensed development library or toolkit without explicit permission.
* @version 0.1 (June 30, 2011)
* @constructor
* @param {Object} config
*/
Ext.define('Ext.ux.grid.column.Progress', {
extend: 'Ext.grid.column.Column'
,alias: 'widget.progresscolumn'
,cls: 'x-progress-column'
/**
* @cfg {String} progressCls
*/
,progressCls: 'x-progress'
/**
* @cfg {String} progressText
*/
,progressText: '{0} %'
/**
* @private
* @param {Object} config
*/
,constructor: function(config){
var me = this
,cfg = Ext.apply({}, config)
,cls = me.progressCls;
me.callParent([cfg]);
// Renderer closure iterates through items creating an <img> element for each and tagging with an identifying
// class name x-action-col-{n}
me.renderer = function(v, meta) {
var text, newWidth;
newWidth = Math.floor(v * me.getWidth(true)); //me = column
// Allow a configured renderer to create initial value (And set the other values in the "metadata" argument!)
v = Ext.isFunction(cfg.renderer) ? cfg.renderer.apply(this, arguments)||v : v; //this = renderer scope
text = Ext.String.format(me.progressText,Math.round(v*100));
meta.tdCls += ' ' + cls + ' ' + cls + '-' + me.ui;
v = '<div class="' + cls + '-text ' + cls + '-text-back">' +
'<div>' + text + '</div>' +
'</div>' +
'<div class="' + cls + '-bar" style="width: '+ newWidth + 'px;">' +
'<div class="' + cls + '-text">' +
'<div>' + text + '</div>' +
'</div>' +
'</div>'
return v;
};
}//eof constructor
/**
* @private
*/
,destroy: function() {
delete this.renderer;
return this.callParent(arguments);
}//eof destroy
}); //eo extend
//end of file
CSS:
/* = Progress Column
----------------------------------------------- */
.x-grid-row .x-progress .x-grid-cell-inner {
padding: 0;
}
/**
* @class Ext.ux.grid.column.Progress
* @extends Ext.grid.Column
* <p>
* A Grid column type which renders a numeric value as a progress bar.
* </p>
* <p>
* <b>Notes:</b><ul>
* <li>Compatible with Ext 4.0</li>
* </ul>
* </p>
* Example usage:
* <pre><code>
var grid = new Ext.grid.Panel({
columns: [{
dataIndex: 'progress'
,xtype: 'progresscolumn'
},{
...
]}
...
});
* </code></pre>
* <p>The column can be at any index in the columns array, and a grid can have any number of progress columns.</p>
* @author Phil Crawford
* @license Licensed under the terms of the Open Source <a href="http://www.gnu.org/licenses/lgpl.html">LGPL 3.0 license</a>. Commercial use is permitted to the extent that the code/component(s) do NOT become part of another Open Source or Commercially licensed development library or toolkit without explicit permission.
* @version 0.1 (June 30, 2011)
* @constructor
* @param {Object} config
*/
Ext.define('Ext.ux.grid.column.Progress', {
extend: 'Ext.grid.column.Column'
,alias: 'widget.progresscolumn'
,cls: 'x-progress-column'
/**
* @cfg {String} progressCls
*/
,progressCls: 'x-progress'
/**
* @cfg {String} progressText
*/
,progressText: '{0} %'
/**
* @private
* @param {Object} config
*/
,constructor: function(config){
var me = this
,cfg = Ext.apply({}, config)
,cls = me.progressCls;
me.callParent([cfg]);
// Renderer closure iterates through items creating an <img> element for each and tagging with an identifying
// class name x-action-col-{n}
me.renderer = function(v, meta) {
var text, newWidth;
newWidth = Math.floor(v * me.getWidth(true)); //me = column
// Allow a configured renderer to create initial value (And set the other values in the "metadata" argument!)
v = Ext.isFunction(cfg.renderer) ? cfg.renderer.apply(this, arguments)||v : v; //this = renderer scope
text = Ext.String.format(me.progressText,Math.round(v*100));
meta.tdCls += ' ' + cls + ' ' + cls + '-' + me.ui;
v = '<div class="' + cls + '-text ' + cls + '-text-back">' +
'<div>' + text + '</div>' +
'</div>' +
'<div class="' + cls + '-bar" style="width: '+ newWidth + 'px;">' +
'<div class="' + cls + '-text">' +
'<div>' + text + '</div>' +
'</div>' +
'</div>'
return v;
};
}//eof constructor
/**
* @private
*/
,destroy: function() {
delete this.renderer;
return this.callParent(arguments);
}//eof destroy
}); //eo extend
//end of file
CSS:
/* = Progress Column
----------------------------------------------- */
.x-grid-row .x-progress .x-grid-cell-inner {
padding: 0;
}