Hiding Columns In cfrgids Column Context Menu

Word Count: 286
I have been showing you how to extend the cfgrid component lately by tapping into the Ext framework. Earlier I wrote a tutorial about creating context menus. Henry liked it but had one concern, lets look at his question.
How do u hide CFGRIDROWINDEX in header context menu -> Columns ? Without the ability to hide that, it is just not acceptable to use that feature as site user might find that confusing, especially when selectmode="edit"
Basically when you right click on the grids column header a menu will popup, in that menu their is a list of columns with check boxes next to the name so that you can hide/show columns in the grid. I can see why we need to hide the gridRowIndex. If someone has a better solution please let me know but I found a solution for this problem. Before the list of columns is shown we can loop over the list and search for the column we want to remove. Once we find the column simply use the menus remove method.
grid.view.colMenu.addListener("beforeshow", function(menu){         
         var grid = ColdFusion.Grid.getGridObject("ArtistGrid");
         var cm = grid.getColumnModel();
         var count = cm.getColumnCount();
         
         for(var i = 0; i < count; i++){
            if("CFGRIDROWINDEX" == cm.getDataIndex(i)) {
               menu.remove(menu.items["items"][i]);
            break;
            }
         }

      });

Comments

#1 Posted By: Henry Posted On: 3/7/08 4:45 PM
Thank you! The solution is quite elegant.

You're really one Ext guru!
#2 Posted By: Brian Posted On: 3/31/08 3:31 PM
Dan: works perfectly, thanks for sharing your code. Only one suggestion: keep your object scope to your local function and you don't need to get the grid object and column model again:

var grid = ColdFusion.Grid.getGridObject('maingrid');
var cm = grid.getColumnModel();
grid.view.colMenu.addListener("beforeshow", function(menu){
   //var grid = ColdFusion.Grid.getGridObject("maingrid");
   //var cm = grid.getColumnModel();
   var count = cm.getColumnCount();
   for(var i = 0; i < count; i++){
      if("CFGRIDROWINDEX" == cm.getDataIndex(i)) {
         menu.remove(menu.items["items"][i]);
         break;
      }
   }
},this);


The commented out lines are not needed because we keep the scope local to our current function, which already has the grid and cm variables defined as needed.
#3 Posted By: Henry Ho Posted On: 10/20/08 1:17 PM
There's one optimization we can use. CFGRIDROWINDEX seems to always be the last row. Although I cannot be sure, setting up the loop to loop down might be a better option.
#4 Posted By: Matt Posted On: 4/18/10 3:54 PM
I tried this code as in comment #2 and in the post itself using CF9. It won't work at all. I need to keep a column completely hidden and setting display=no alone just won't do b/c the user can still call it up in the context menu available for any column. I'm trying to hide field Autonum in my idGrid.

function init(){
var grid = ColdFusion.Grid.getGridObject('idGrid');
var cm = grid.getColumnModel();
grid.view.colMenu.addListener("beforeshow", function(menu){
//var grid = ColdFusion.Grid.getGridObject("maingrid");
//var cm = grid.getColumnModel();
var count = cm.getColumnCount();
for(var i = 0; i < count; i++){
if("Autonum" == cm.getDataIndex(i)) {
menu.remove(menu.items["items"][i]);
break;
}
}
},this);

and at the bottom of the page, before the </body>,
<cfset ajaxOnLoad("init")>

am I missing something?


Post Your Comment

Leave this field empty







Show Captcha

If you subscribe, any new posts to this thread will be sent to your email address.

Copyright © 2007 Dan Vega | BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.