This is a migrated thread and some comments may be shown as answers.

In HeaderContextMenu, Only Show the Show/Hide Columns List

1 Answer 92 Views
Grid
This is a migrated thread and some comments may be shown as answers.
David
Top achievements
Rank 1
David asked on 04 Dec 2013, 02:55 PM
Current Setup
I have a RadGrid and only want to provide a header context menu with an option to show/hide columns.

HeaderContextMenus are enabled and disabled grouping such that the only menu item that appears is the 'Columns' menu item which I've renamed. Please see the attached screenshot headercontextmenu_singleitem.png to get an idea of how the context menu appears when right-clicked. I have a javascript method that is applied to the client event  OnHeaderMenuShowing. This is what I'm using to rename my 'Columns' column and do some other formatting.


What I Would Like to Do
In short I would like to only show the list of columns with checkboxes to show/hide, nothing more. I am checking in the javascript OnHeaderMenuShowing to see how many items I have in my main level menu. If only 1 item I want to only show the child popout menu which lists the columns with the checkboxes to show/hide columns.

if(args.get_menu().get_items()._array.length == 1) {
    // do something here when only 1 item
}

I have managed to simulate this by setting the groupSettings offsetX and offsetY for the 'Columns' menu item to cause it to move over the main menu and setting the menu clickToOpen to true. (see screenshot headercontextmenu_singleitem_exampleonlychildshowing.png) This is more a hack and unfortunately inconsistent. After I left click on the page and right click again the menu does not appear in the same place. Multiple left clicks (on other columns) works correctly. I also cannot seem to turn off the expand and collapse animations. 

Sample for moving the child menu when 1 item:
if (items._array.length == 1) {
   items._parent.set_clickToOpen(true);
   args.get_menu().get_items().getItem(0).open();
   args.get_menu().get_items().getItem(0)._groupSettings.set_offsetX(-188); // static numbers for now
   args.get_menu().get_items().getItem(0)._groupSettings.set_offsetY(-28);
}

Sample for setting animations:
args.get_menu().get_expandAnimation().set_type("None");
args.get_menu().get_collapseAnimation().set_type("None");

Thanks in advance for any suggestions.

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 05 Dec 2013, 12:38 PM
Hi David,

As a suggestion you can set the HeaderContextMenu options from code-behind as follows:

C#:
protected void Page_Load(object o, EventArgs e)
{
   RadGrid1.HeaderContextMenu.ItemCreated += new RadMenuEventHandler(HeaderContextMenu_ItemCreated);
}
 
void HeaderContextMenu_ItemCreated(object sender, RadMenuEventArgs e)
{       
   RadContextMenu menu = RadGrid1.HeaderContextMenu;
   foreach (RadMenuItem item in menu.Items)
   {
       if (item.IsSeparator)
       {
           item.Visible = false;
       }
   }
   switch ((e.Item.Text))
   {
       case "Clear Sorting":
           e.Item.Visible = false;
           break;
 
           break;
       case "Sort Ascending":
           e.Item.Visible = false;
           break;
 
           break;
       case "Sort Descending":
           e.Item.Visible = false;
           break;
 
       case "Group By":
           e.Item.Visible = false;
           break;
 
       case "Ungroup":
           e.Item.Visible = false;
           break;
   }
}

Thanks,
Princy
Tags
Grid
Asked by
David
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or