[CLOSED] MenuItem hRef - Update attribute

  1. #1

    [CLOSED] MenuItem hRef - Update attribute

    I am looking to update a menuItem hRef in a contextMenu based on the item selected. I know hRef is a config attribute and I need to write a new function to do this. I looked at the suggestion in MenuItem Href Dynamic Update which was related to v1.

    var setHref = function (menuItem, url) {
                menuItem.el.set({
                    href : url
                });
            };
    I am having issue with the line:
    menuItem.el.set({href : url});
    The error is "Cannot read property 'set' of undefined."
    Last edited by Daniil; Oct 14, 2014 at 2:16 PM. Reason: [CLOSED]
  2. #2
    Hi Chris,

    I guess you have not changed "MenuItem1" to "App.MenuItem1", haven't you?

    This example appears to be working.

    Example
    <%@ Page Language="C#" %>
     
    <!DOCTYPE html>
    
    <html>
    <head runat="server">
        <title>Ext.NET v2 Example</title>
     
        <script type="text/javascript">
            var setHref = function (menuItem, url) {
                menuItem.setText(url);
                menuItem.itemEl.set({
                    href : url
                });
            };
        </script>
    </head>
    <body>
        <form runat="server">
            <ext:ResourceManager runat="server" />
    
            <ext:Menu runat="server" Floating="false">
                <Items>
                    <ext:MenuItem
                        ID="MenuItem1"
                        runat="server"
                        Text="Ext.NET"
                        Href="http://ext.net"
                        HrefTarget="_blank" />
                </Items>
            </ext:Menu>
    
            <ext:Button runat="server" Text="Set Href" Handler="setHref(App.MenuItem1, 'http://forums.ext.net');" />
        </form>
    </body>
    </html>
    Last edited by Daniil; Oct 14, 2014 at 12:58 PM.
  3. #3
    I am using App.MenuItem1.

    I also found the following thread on Sencha - Dynamically updating Href for MenuItem, but still could not get it to work.

    Is it because my menuItem is part of a contextMenu?

    If it is not obvious I can produce a small sample.
  4. #4
    Yes, it is better to use "itemEl" instead of "el". Thank you for the Sencha thread link.

    As for the context menu, I guess you might call the setHref function before the context menu is rendered. It means the menu items are not rendered and don't have "itemEl".

    Please try (I've not tested):
    var setHref = function (menuItem, url) {
        menuItem.href = url;
    
        if (menuItem.rendered) {
            menuItem.itemEl.set({
                href : url
            });
        }
    };
  5. #5
    Please closed. I did need to add the check for rendered, since it was a context menu.
  6. #6
    So my final implementation is based off of the Sencha recommendation with your modification:

    Ext.menu.Item.override({
       setHref: function (href, target) {
          this.href = href;
          this.hrefTarget = target || this.hrefTarget;
    
          // If not rendered (e.g., ContextMenu items) no need to reset DOM
          if (this.rendered) {
             this.itemEl.set({
                href : this.href || '#',
             });
    
             if (this.hrefTarget)
                this.itemEl.set({
                   target : this.hrefTarget
                });
          }
    
       }
    });
    What are your thoughts?
    Last edited by cwolcott; Oct 14, 2014 at 2:24 PM.
  7. #7
    Looks good.

Similar Threads

  1. MenuItem Href Dinamic Update
    By threewonders in forum 1.x Help
    Replies: 8
    Last Post: Oct 14, 2014, 12:51 PM
  2. bind to grid view with <a href>
    By dx7 in forum 2.x Help
    Replies: 2
    Last Post: Oct 23, 2012, 9:02 AM
  3. Why # is coming from treepanel node href?
    By eldhose in forum 1.x Help
    Replies: 6
    Last Post: Feb 03, 2011, 5:37 PM
  4. How to get parent menuItem when click menuItem?
    By zhangsir199 in forum 1.x Help
    Replies: 2
    Last Post: Jan 21, 2011, 2:58 AM
  5. Replies: 3
    Last Post: Sep 13, 2008, 10:09 AM

Posting Permissions