为控制器内的所有操作添加类到页眉导航

huangapple go评论74阅读模式
英文:

Add Class to Header Nav for all Actions within a Controller

问题

我有一个ASP.NET MVC网站,其中有一个标准的导航栏,显示在标题中指向特定控制器和操作的文本。当我导航到localhost/ 时,将会向 "Home" 添加 "text-white bg-primary" 类。当我导航到localhost/Entry 时,这些类将被添加到 "Entry"。

然而,当我导航到除这两个目录之外的任何其他目录(例如localhost/Home/Indexlocalhost/Home/GetJobslocalhost/Entry/Review)时,这些类不会被添加。我期望的功能是在 localhost/Home/* 下,"Home" 导航项也添加这些类,以及在 localhost/Entry/* 下,"Entry" 导航项也添加这些类。

		<nav class="navbar navbar-expand-md navbar-light bg-light header-box-shadow">
			<div class="collapse navbar-collapse" id="appNavigation">
				<div class="navbar-nav">
					<a class="nav-link" asp-action="Index" asp-controller="Home">Home</a>
					<a class="nav-link" asp-action="Index" asp-controller="Entry">Manual Entry</a>
				</div>
			</div>
		</nav>

	    <script type="text/javascript">
		    $("nav a.nav-link[href='" + location.pathname + "']").addClass("text-white bg-primary");
	    </script>
英文:

I have an ASP.NET MVC website which has a standard navbar that displays text in the header that points to a certain controller and action. When I navigate to localhost/ the text-white bg-primary classes are added to "Home". When I navigate to localhost/Entry the classes are added to "Entry".

However, when I navigate to anything other than those two directories (i.e. localhost/Home/Index, localhost/Home/GetJobs, localhost/Entry/Review) the classes are not added. My desired functionality is for the "Home" nav item to have the classes added to it for localhost/Home/* as well as for "Entry" .. localhost/Entry/*

		&lt;nav class=&quot;navbar navbar-expand-md navbar-light bg-light header-box-shadow&quot;&gt;
			&lt;div class=&quot;collapse navbar-collapse&quot; id=&quot;appNavigation&quot;&gt;
				&lt;div class=&quot;navbar-nav&quot;&gt;
					&lt;a class=&quot;nav-link&quot; asp-action=&quot;Index&quot; asp-controller=&quot;Home&quot;&gt;Home&lt;/a&gt;
					&lt;a class=&quot;nav-link&quot; asp-action=&quot;Index&quot; asp-controller=&quot;Entry&quot;&gt;Manual Entry&lt;/a&gt;
				&lt;/div&gt;
			&lt;/div&gt;
		&lt;/nav&gt;

	    &lt;script type=&quot;text/javascript&quot;&gt;
		    $(&quot;nav a.nav-link[href=&#39;&quot; + location.pathname + &quot;&#39;]&quot;).addClass(&quot;text-white bg-primary&quot;);
	    &lt;/script&gt;

答案1

得分: 0

以下是您要翻译的内容:

我能够使这个适用于我的用例 如果您的导航链接到一个控制器下的多个不同操作可能需要进行一些修改

<!-- language: lang-js -->

<script type="text/javascript">
    let elements = document.getElementsByClassName("nav-link");

    // shows current nav item for any page/action in the controller
    // e.g. /Home/Index, /Home/Search, etc. will change CSS of "Home" to show as the current nav item.
    for (let i = 0; i < elements.length; i++) {
        let element = elements[i];
        let href = element.attributes.getNamedItem("href").value.toString();

        // default page, "/" .. set href to default controller for proper matching
        if (href === "/") {
            // TODO: "get" default controller set by Endpoints Controller Route
            href = "/Home";
        }

        let path = location.pathname.split('/');
        path.pop();
        path = path.join('').toLowerCase();

        if (href.toLowerCase().includes(path)) {
            $(element).addClass("text-white bg-primary");

            // path is an empty string when at the default page "/" to avoid mismatches, stop the for loop from continuing
            if (path.length < 1) {
                break;
            }
        }
    }
</script>

<!-- end snippet -->

请注意,代码中的注释没有被翻译,只翻译了代码本身。

英文:

I was able to make this work for my use case. This would need some modification if your nav has links to multiple different actions under one controller.

<!-- language: lang-js -->

&lt;script type=&quot;text/javascript&quot;&gt;
	let elements = document.getElementsByClassName(&quot;nav-link&quot;);

	// shows current nav item for any page/action in the controller
	// e.g. /Home/Index, /Home/Search, etc. will change CSS of &quot;Home&quot; to show as the current nav item.
	for (let i = 0; i &lt; elements.length; i++) {
		let element = elements[i];
		let href = element.attributes.getNamedItem(&quot;href&quot;).value.toString();

		// default page, &quot;/&quot; .. set href to default controller for proper matching
		if (href === &quot;/&quot;) {
			// TODO: &quot;get&quot; default controller set by Endpoints Controller Route
			href = &quot;/Home&quot;;
		}

		let path = location.pathname.split(&#39;/&#39;);
		path.pop();
		path = path.join(&#39;&#39;).toLowerCase();

		if (href.toLowerCase().includes(path)) {
			$(element).addClass(&quot;text-white bg-primary&quot;);

			// path is an empty string when at the default page &quot;/&quot; to avoid mismatches, stop the for loop from continuing
			if (path.length &lt; 1) {
				break;
			}
		}
	}
&lt;/script&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月29日 22:16:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76581919.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定