HOW to Hide Context menu Item in a Particular Node Level ?

6 posts, 1 answers
  1. Eric
    Eric avatar
    37 posts
    Member since:
    Aug 2012

    Posted 05 Feb 2013 Link to this post

    I have a RadTreeView with a Context Menu which has 3 Iems

    <telerik:RadMenuItem Value="NewFolder" Text="New Folder
    "
    ImageUrl="~/TreeView/Img/Outlook/12.gif"  >
     
    </telerik:RadMenuItem>
     
    <telerik:RadMenuItem Value="RenameFolder" Text="Rename Folder
    "
    Enabled="false" ImageUrl="~/TreeView/Img/Outlook/rename.gif" PostBack="false" >
     
    </telerik:RadMenuItem>
     
    <telerik:RadMenuItem Value="DeleteFolder" Text="Delete Folder
    "
    ImageUrl="~/TreeView/Img/Outlook/7.gif" >
     
    </telerik:RadMenuItem>


    The RadTreeView has 3 diferent levels

    I want to HIDE the NewFolder Context Menu Option if I'm on Node Level 3 (when I right click on the node I don't want that option to show if I'm on Level 3), How do I do that, it doesn't matter if it's from Code Behind or Client or Both.

    Thank you.
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 05 Feb 2013 Link to this post

    Hi Eric

    This is a sample I have tried to hide a particular contextmenuitem for a particular node
    Try the following markup and JS and let me know your concern
    aspx:
    <telerik:RadTreeView ID="RadTreeView" runat="server" OnClientContextMenuShowing="OnClientContextMenuShowing">
        <Nodes>
            <telerik:RadTreeNode Text="New Folder">
            </telerik:RadTreeNode>
            <telerik:RadTreeNode Text="Rename Folder">
            </telerik:RadTreeNode>
            <telerik:RadTreeNode Text="Delete Folder">
            </telerik:RadTreeNode>
        </Nodes>
        <ContextMenus>
            <telerik:RadTreeViewContextMenu ID="RadTreeViewContextMenu1" runat="server">
                <Items>
                    <telerik:RadMenuItem Value="NewFolder" Text="New Folder">
                    </telerik:RadMenuItem>
                    <telerik:RadMenuItem Value="RenameFolder" Text="Rename Folder" Enabled="false" PostBack="false">
                    </telerik:RadMenuItem>
                    <telerik:RadMenuItem Value="DeleteFolder" Text="Delete Folder">
                    </telerik:RadMenuItem>
                </Items>
            </telerik:RadTreeViewContextMenu>
        </ContextMenus>
    </telerik:RadTreeView>
    JS:
    <script type="text/javascript">
    function OnClientContextMenuShowing(sender, args)
    {
        var menu = $find("<%= RadTreeViewContextMenu1.ClientID %>");
        var menuItem = menu.findItemByText("Delete Folder");
        if (args.get_node().get_text() == "Delete Folder")
        {
            menuItem.disable();
        }
    }
    </script>

    Thanks
    Princy
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Eric
    Eric avatar
    37 posts
    Member since:
    Aug 2012

    Posted 06 Feb 2013 Link to this post

    Thanks for the help Princy but that wasn't what I was looking for exactly,

    For example, Lets say I have the following TreeView

    Node 1                           (This Node is on Level 0 or root)
          Node 2                     (This Node is on Level 1)
                 Node 4              (This Node is on Level 2)
                 Node 5              (This Node is on Level 2)
          Node 3                     (This Node is on Level 1)

    Node 6                           (This Node is on Level 0 or root)
           Node 7                    (This Node is on Level 1)
           Node 8                    (This Node is on Level 1)

    When I Rick-Click Node 4 or Node 5 (Which are on Level 2)
    I want to Disable the "NewFolder" item of the Context Menu
    I ONLY want to Disable it on the Items that are Level 2 because
    I only want users to be able to create a Max of 3 Levels (0, 1, 2)
    So if I click on Node 2 (which is on Level 1) I want the NewFolder Option
    to be enabled.
  5. Eric
    Eric avatar
    37 posts
    Member since:
    Aug 2012

    Posted 07 Feb 2013 Link to this post

    Do you how can I access the Level and make a level comparison? I can't seem to find how to do that anywhere in the telerik documentation.
  6. Answer
    Kate
    Admin
    Kate avatar
    1898 posts

    Posted 08 Feb 2013 Link to this post

    Hi Eric,

    Please take a look at this on-line demo that I believe would be very helpful in implementing the desired functionality.

    Kind regards,
    Kate
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  7. Eric
    Eric avatar
    37 posts
    Member since:
    Aug 2012

    Posted 10 Feb 2013 Link to this post

    Hello Kate,

    Thanks for the link, I didn't find exactly what I wanted but it did lead me to the right direction

    using the (get_level()) function:

    function onClientContextMenuShowing(sender, args) {
        var treeNode = args.get_node();
        treeNode.set_selected(true);
        //enable/disable menu items
        setMenuItemsState(args.get_menu().get_items(), treeNode);
    }
     
    //this method disables the appropriate context menu items
    function setMenuItemsState(menuItems, treeNode) {
        for (var i = 0; i < menuItems.get_count(); i++) {
            var menuItem = menuItems.getItem(i);
            switch (menuItem.get_value()) {
                case "NewFolder":
                    if (treeNode.get_level() == "2")
                        menuItem.set_enabled(false);
                    else
                        menuItem.set_enabled(true);
     
                    break;
            }
        }
    }

Back to Top
UI for ASP.NET Ajax is Ready for VS 2017