How to disable the click (remove hyperlink) from certain menu items?

Apr 19, 2010 at 1:58 PM
Edited Apr 22, 2010 at 7:40 PM

How to disable the click (remove hyporlink) from certain menu items?

I've the following sitemap definition:

 

<siteMap xmlns="http://mvcsitemap.codeplex.com/schemas/MvcSiteMap-File-1.0" enableLocalization="false">
<mvcSiteMapNode title="NotUsed" action="NotUsed">
<mvcSiteMapNode title="Home">
</mvcSiteMapNode>
<mvcSiteMapNode title="User" controller="Product" action="Index">
<mvcSiteMapNode title="List Products" action="List" />
<mvcSiteMapNode title="Add Products" action="Add" />
</mvcSiteMapNode>
</mvcSiteMapNode>
</siteMap>

 

 

This is rendered as:

Home

User
    -> List Products
    -> Add Products

 

Requirements:

1) I don't want the "User" menu link clickable, only 'List Products' & 'Add Products' should be clickable.
2) 'Home' should be clickable because it contains no sub-items

 

How to do this ???

Apr 21, 2010 at 2:46 AM

Hi Stefh,

My first thoughts on this would be to disable and restyle the link using , say, JQuery. I find this trickier than it shoud be due to their currently being no way to add an 'id' attribute to the link (a) elements. Though still doable via JQuery slector API. There is curently atopic on the 'id' attribuet issue here: http://mvcsitemap.codeplex.com/Thread/View.aspx?ThreadId=208883

But another way, maybe a better way actually, is to alter the sitemap node at runtime. Though I am yet to try this for affecting URL's:

SiteMap.CurrentNode.ParentNode.URL = "#";

Though  assuming this worked (not sure about the duplicate URL's being allowed), it would still be clickable, though 'go nowhere'. Not sure what happenes if you set the URL to "" (empty string)  -may be worth a try as it may omitt the href altogether??.

In your case you may be able to loop through all nodes at the top level and affect the URL that way.

Also, back to the javascript way, I wonder if doing:

SiteMap.CurrentNode.ParentNode.URL = "removeMe";

and then using JQuery to find all href = "removeMe" and restyle / manipulate that way.

I am presently struggling with similar issues, and certianly not the expert in such matters! So just some thoughts to share with you really.

Cheers,
Andles

 

 

Apr 22, 2010 at 2:06 PM

I think that the trick with "#" can also be defined in the sitemap definition file:

<mvcSiteMapNode title="User" controller="Product" url="#User">
<mvcSiteMapNode title="List Products" action="List" />
<mvcSiteMapNode title="Add Products" action="Add" />
</mvcSiteMapNode>

<
mvcSiteMapNode title="Test" controller="Test " url="#Test">
<mvcSiteMapNode title="List Test" action="List" />
<mvcSiteMapNode title="Add Test" action="Add" />
</
mvcSiteMapNode>

I this way the there are no duplicate urls.
But with this workaround, they are still clickable.

Maybe a nicer solution would be adding a new parameter : 'clickable':

<mvcSiteMapNode title="User" controller="Product" clickable="false">
<mvcSiteMapNode title="List Products" action="List" />
<mvcSiteMapNode title="Add Products" action="Add" />
</mvcSiteMapNode>

In the SiteMapHelper.cs, this line

 

sb.Append(string.Format("<a href=\"{0}\"{1}>{2}</a>", rootNode.Url, extraAttributes, rootNode.Title));

 

should be changed into:

 

if (rootNode.clickable)
    sb.Append(string.Format("<a href=\"{0}\"{1}>{2}</a>", rootNode.Url, extraAttributes, rootNode.Title));
else
    sb.Append(rootNode.Title);


Maybe an idea ???

--stef

Apr 24, 2010 at 1:18 PM

Interesting.

I would like to see some kind of grouping structure for labelling sets of URL's. So maybe top level nodes could have an optional "definesGroup" attribute - the html helper may, for example, render this with a html span with a pre-defined class you could affect.

Or possibly a new element like this:

<mvcSiteMapNodeGroup  label="My Label">
<!-- regular nodes in here -->
<mvcSiteMapNodeGroup/>

Cheers,
Andles

Coordinator
Apr 27, 2010 at 6:56 AM

Can you add this as a feature request?