JavaScript 动态生成 Bootstrap 5 导航栏
    • 下拉项。
  • huangapple go评论126阅读模式
    英文:

    Javascript to dyanamically spawn bootsrap 5 Navbar <li> and <ul dropdown item>

    问题

    export const navLinks = [
        {
          id: "manage",
          title: "Manage",
          dropdown: [
            "Add User",
            "Edit User"
          ]
        },
        {
          id: "transactions",
          title: "Transactions",
          dropdown: []
        },
        {
          id: "report",
          title: "Report Maintenance",
          dropdown: [
            "Admin Report",
            "User Report"
          ]
        },
        {
            id: "support",
            title: "Support",
            dropdown: []
        },
        {
            id: "logout",
            title: "Logout",
            dropdown: []
        },
      ];
    
    英文:

    I would like to ask for your help.

    I store text to constant.js file example below.

    export const navLinks = [
        {
          
          id: &quot;manage&quot;,
          title: &quot;Manage&quot;,
        },
        {
          id: &quot;transactions&quot;,
          title: &quot;Transactions&quot;,
        },
        {
          id: &quot;report&quot;,
          title: &quot;Report Maintenance&quot;,
        },
        {
            id: &quot;support&quot;,
            title: &quot;Support&quot;,
        },
        {
            id: &quot;logout&quot;,
            title: &quot;Logout&quot;,
        },
      ];
    

    right now I was able to dynamically load a text to &lt;li&gt; tag to the navbar using below code. also using bootsrap 5.

    &lt;ul class=&quot;navbar-nav ms-auto mb-2 mb-lg-0&quot;&gt;
      {navLinks.map((nav, index) =&gt;(
      &lt;li class=&quot;nav-item&quot; key=&quot;{nav.id}&quot;&gt;
        &lt;a class=&quot;nav-link active&quot; aria-current=&quot;page&quot; href=&quot;{`#${nav.id}`}&quot;&gt;
          {nav.title}
        &lt;/a&gt;
      &lt;/li&gt;
      ))}
    &lt;/ul&gt;
    

    JavaScript 动态生成 Bootstrap 5 导航栏 <li> 和 <ul> 下拉项。

    what I want is for some &lt;li&gt; there will be a dropdown menu but not all,

    for example, on Report Maintenance, there will be dropdown e.g(Admin Report, User Report) and on Manage the dropdown will be(Add User, Edit User).

    Is there a way to add the dropdown text to constant.js and spawn it dynamically on the correct &lt;li&gt; tag?

    Im expecting it to be like this.

    JavaScript 动态生成 Bootstrap 5 导航栏 <li> 和 <ul> 下拉项。

    答案1

    得分: 0

    以下是您要翻译的代码部分:

    你可以在constant.js中的具有下拉菜单的项目中添加一个`children`属性如下所示
    
        export const navLinks = [
        	{
        		id: "manage",
        		title: "Manage",
        	},
        	{
        		id: "transactions",
        		title: "Transactions",
        	},
        	{
        		id: "report",
        		title: "Report Maintenance",
        		children: [
        			{
        				id: 'report/admin',
        				title: 'Admin Report',
        			},
        			{
        				id: 'report/user',
        				title: 'User Report'
        			}
        		]
        	},
        	{
        		id: "support",
        		title: "Support",
        	},
        	{
        		id: "logout",
        		title: "Logout",
        	},
        ];
    然后像这样使用它
    
        <ul class="navbar-nav ms-auto">
          {navLinks.map((nav, index) => {
            if (!nav.children) {
              return (
                <li class="nav-item" key={nav.id}>
                  <a class="nav-link active" href={`#${nav.id}`}>
                    {nav.title}
                  </a>
                </li>
              );
            } else {
              return (
                <li class="nav-item dropdown" key={nav.id}>
                  <a class="nav-link active dropdown-toggle" href={`#${nav.id}`} data-bs-toggle="dropdown">
                    {nav.title}
                  </a>
                  <ul class="dropdown-menu dropdown-menu-dark">
                    {nav.children.map((dropdown, idx) => (
                      <li key={dropdown.id}>
                        <a class="dropdown-item" href={`#${dropdown.id}`}>
                          {dropdown.title}
                        </a>
                      </li>
                    ))}
                  </ul>
                </li>
              );
            }
          })}
        </ul>
    

    工作示例:https://codesandbox.io/s/thirsty-northcutt-vjbbt1?file=/src/App.js

    英文:

    You can add a children property to the items that has a dropdown in the constant.js like so:

    export const navLinks = [
    {
    id: &quot;manage&quot;,
    title: &quot;Manage&quot;,
    },
    {
    id: &quot;transactions&quot;,
    title: &quot;Transactions&quot;,
    },
    {
    id: &quot;report&quot;,
    title: &quot;Report Maintenance&quot;,
    children: [
    {
    id: &#39;report/admin&#39;,
    title: &#39;Admin Report&#39;,
    },
    {
    id: &#39;report/user&#39;,
    title: &#39;User Report&#39;
    }
    ]
    },
    {
    id: &quot;support&quot;,
    title: &quot;Support&quot;,
    },
    {
    id: &quot;logout&quot;,
    title: &quot;Logout&quot;,
    },
    ];
    

    Then use it like this

    &lt;ul class=&quot;navbar-nav ms-auto&quot;&gt;
    {navLinks.map((nav, index) =&gt; {
    if (!nav.children) {
    return (
    &lt;li class=&quot;nav-item&quot; key={nav.id}&gt;
    &lt;a class=&quot;nav-link active&quot; href={`#${nav.id}`}&gt;
    {nav.title}
    &lt;/a&gt;
    &lt;/li&gt;
    );
    } else {
    return (
    &lt;li class=&quot;nav-item dropdown&quot; key={nav.id}&gt;
    &lt;a class=&quot;nav-link active dropdown-toggle&quot; href={`#${nav.id}`} data-bs-toggle=&quot;dropdown&quot;&gt;
    {nav.title}
    &lt;/a&gt;
    &lt;ul class=&quot;dropdown-menu dropdown-menu-dark&quot;&gt;
    {nav.children.map((dropdown, idx) =&gt; (
    &lt;li key={dropdown.id}&gt;
    &lt;a class=&quot;dropdown-item&quot; href={`#${dropdown.id}`}&gt;
    {dropdown.title}
    &lt;/a&gt;
    &lt;/li&gt;
    ))}
    &lt;/ul&gt;
    &lt;/li&gt;
    );
    }
    })}
    &lt;/ul&gt;
    

    Working example: https://codesandbox.io/s/thirsty-northcutt-vjbbt1?file=/src/App.js

    huangapple
    • 本文由 发表于 2023年2月27日 16:13:32
    • 转载请务必保留本文链接:https://go.coder-hub.com/75578093.html
    匿名

    发表评论

    匿名网友

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

    确定