Fixing the Column Menu in Ext.grid.GridView (2)
March 2nd, 2009 by Lloyd, under ExtJS, JavaScript.
If you use the Ext.grid.GridPanel or descendants you might have come across this. If you have multiple columns and for some reason decide to make all your columns unhideable then you will notice how the Columns menu item does not go away, regardless of the fact all your columns are no longer manageable in that way.
I spent 5 minutes Friday pondering on this issue and wrote a fix for the Ext.grid.GridView, which is the component actually responsible for rendering the menu. The crux of the fix is:
if(g.enableColumnHide !== false){
if (this.cm.config.length > 0) {
// Build count of columns that do not have menu disabled
var colCount = 0;
for (var i = 0; i < this.cm.config.length; i++) {
if (this.cm.config[i].menuDisabled == false) colCount++;
}
// Build count of columns that are hideable
var hideCount = 0;
for (var i = 0; i < this.cm.config.length; i++) {
if (this.cm.config[i].hideable == false) hideCount++;
}
//
if (hideCount < colCount) {
this.hmenu.add('-',
{id:"columns", text: this.columnsText, menu: this.colMenu, iconCls: 'x-cols-icon'}
);
}
}
}
To prove it works here is two screenshots of before and after the fix:
| Before | After |
And that’s basically it. Below you’ll find the full code to override the Ext.grid.GridView.
Ext.override(Ext.grid.GridView,{
renderUI : function(){
var header = this.renderHeaders();
var body = this.templates.body.apply({rows:''});
var html = this.templates.master.apply({
body: body,
header: header
});
var g = this.grid;
g.getGridEl().dom.innerHTML = html;
this.initElements();
// get mousedowns early
Ext.fly(this.innerHd).on("click", this.handleHdDown, this);
this.mainHd.on("mouseover", this.handleHdOver, this);
this.mainHd.on("mouseout", this.handleHdOut, this);
this.mainHd.on("mousemove", this.handleHdMove, this);
this.scroller.on('scroll', this.syncScroll, this);
if(g.enableColumnResize !== false){
this.splitZone = new Ext.grid.GridView.SplitDragZone(g, this.mainHd.dom);
}
if(g.enableColumnMove){
this.columnDrag = new Ext.grid.GridView.ColumnDragZone(g, this.innerHd);
this.columnDrop = new Ext.grid.HeaderDropZone(g, this.mainHd.dom);
}
if(g.enableHdMenu !== false){
if(g.enableColumnHide !== false){
this.colMenu = new Ext.menu.Menu({id:g.id + "-hcols-menu"});
this.colMenu.on("beforeshow", this.beforeColMenuShow, this);
this.colMenu.on("itemclick", this.handleHdMenuClick, this);
}
this.hmenu = new Ext.menu.Menu({id: g.id + "-hctx"});
this.hmenu.add(
{id:"asc", text: this.sortAscText, cls: "xg-hmenu-sort-asc"},
{id:"desc", text: this.sortDescText, cls: "xg-hmenu-sort-desc"}
);
if(g.enableColumnHide !== false){
if (this.cm.config.length > 0) {
// Build count of columns that do not have menu disabled
var colCount = 0;
for (var i = 0; i < this.cm.config.length; i++) {
if (this.cm.config[i].menuDisabled == false) colCount++;
}
// Build count of columns that are hideable
var hideCount = 0;
for (var i = 0; i < this.cm.config.length; i++) {
if (this.cm.config[i].hideable == false) hideCount++;
}
//
if (hideCount < colCount) {
this.hmenu.add('-',
{id:"columns", text: this.columnsText, menu: this.colMenu, iconCls: 'x-cols-icon'}
);
}
}
}
this.hmenu.on("itemclick", this.handleHdMenuClick, this);
//g.on("headercontextmenu", this.handleHdCtx, this);
}
if(g.trackMouseOver){
this.mainBody.on("mouseover", this.onRowOver, this);
this.mainBody.on("mouseout", this.onRowOut, this);
}
if(g.enableDragDrop || g.enableDrag){
this.dragZone = new Ext.grid.GridDragZone(g, {
ddGroup : g.ddGroup || 'GridDD'
});
}
this.updateHeaderSortState();
}
});
Animal on June 10th, 2009
Have you submitted this as a bug report? Looks like it should be doing exactly that.