Condor
14 Oct 2009, 10:43 PM
Here is a plugin to make a grid column autosize when you double-click on the column splitter (like MS Excel).
Ext.ns('Ext.ux.grid');
(function () {
var cursorRe = /^(?:col|e|w)-resize$/;
Ext.ux.grid.AutoSizeColumns = Ext.extend(Object, {
cellPadding: 8,
constructor: function (config) {
Ext.apply(this, config);
},
init: function (grid) {
var view = grid.getView();
view.onHeaderClick = view.onHeaderClick.createInterceptor(this.onHeaderClick);
grid.on('headerdblclick', this.onHeaderDblClick.createDelegate(view, [this.cellPadding], 3));
},
onHeaderClick: function (grid, colIndex) {
var el = this.getHeaderCell(colIndex);
if (cursorRe.test(el.style.cursor)) {
return false;
}
},
onHeaderDblClick: function (grid, colIndex, e, cellPadding) {
var el = this.getHeaderCell(colIndex), width, rowIndex, count;
if (!cursorRe.test(el.style.cursor)) {
return;
}
if (e.getXY()[0] - Ext.lib.Dom.getXY(el)[0] <= 5) {
colIndex--;
el = this.getHeaderCell(colIndex);
}
if (this.cm.isFixed(colIndex) || this.cm.isHidden(colIndex)) {
return;
}
el = el.firstChild;
el.style.width = '0px';
width = el.scrollWidth;
el.style.width = 'auto';
for (rowIndex = 0, count = this.ds.getCount(); rowIndex < count; rowIndex++) {
el = this.getCell(rowIndex, colIndex).firstChild;
el.style.width = '0px';
width = Math.max(width, el.scrollWidth);
el.style.width = 'auto';
}
this.onColumnSplitterMoved(colIndex, width + cellPadding);
}
});
})();
Ext.preg('autosizecolumns', Ext.ux.grid.AutoSizeColumns);
Example usage:
new Ext.Viewport({
layout: 'fit',
items: {
xtype: 'grid',
plugins: ['autosizecolumns'],
store: [
['903185-12349058-1239058-12390', '12347895109956-203698-180571'],
['190847-319085-90123456-1934', '19023784-1490-1571293814'],
['189-5123085-1905712348971', '1290587-1578019823017895610'],
['190-908-159812-570298123978502', '123-57-4517598169028374892']],
columns: [
{header: 'Field 1', dataIndex: 'field1', sortable: true},
{header: 'Field 2', dataIndex: 'field2', sortable: true}
]
}
});
Ext.ns('Ext.ux.grid');
(function () {
var cursorRe = /^(?:col|e|w)-resize$/;
Ext.ux.grid.AutoSizeColumns = Ext.extend(Object, {
cellPadding: 8,
constructor: function (config) {
Ext.apply(this, config);
},
init: function (grid) {
var view = grid.getView();
view.onHeaderClick = view.onHeaderClick.createInterceptor(this.onHeaderClick);
grid.on('headerdblclick', this.onHeaderDblClick.createDelegate(view, [this.cellPadding], 3));
},
onHeaderClick: function (grid, colIndex) {
var el = this.getHeaderCell(colIndex);
if (cursorRe.test(el.style.cursor)) {
return false;
}
},
onHeaderDblClick: function (grid, colIndex, e, cellPadding) {
var el = this.getHeaderCell(colIndex), width, rowIndex, count;
if (!cursorRe.test(el.style.cursor)) {
return;
}
if (e.getXY()[0] - Ext.lib.Dom.getXY(el)[0] <= 5) {
colIndex--;
el = this.getHeaderCell(colIndex);
}
if (this.cm.isFixed(colIndex) || this.cm.isHidden(colIndex)) {
return;
}
el = el.firstChild;
el.style.width = '0px';
width = el.scrollWidth;
el.style.width = 'auto';
for (rowIndex = 0, count = this.ds.getCount(); rowIndex < count; rowIndex++) {
el = this.getCell(rowIndex, colIndex).firstChild;
el.style.width = '0px';
width = Math.max(width, el.scrollWidth);
el.style.width = 'auto';
}
this.onColumnSplitterMoved(colIndex, width + cellPadding);
}
});
})();
Ext.preg('autosizecolumns', Ext.ux.grid.AutoSizeColumns);
Example usage:
new Ext.Viewport({
layout: 'fit',
items: {
xtype: 'grid',
plugins: ['autosizecolumns'],
store: [
['903185-12349058-1239058-12390', '12347895109956-203698-180571'],
['190847-319085-90123456-1934', '19023784-1490-1571293814'],
['189-5123085-1905712348971', '1290587-1578019823017895610'],
['190-908-159812-570298123978502', '123-57-4517598169028374892']],
columns: [
{header: 'Field 1', dataIndex: 'field1', sortable: true},
{header: 'Field 2', dataIndex: 'field2', sortable: true}
]
}
});