0

I have A json data from which I want to make a Navbar using vue .js, I am unable to loop the data through the Navbar

This is what I am trying to do with my JSON data currently it is static but I want to loop through the data I have

As my JSON data is dynamic , I am getting from backend, I have to make a navbar as per user login to show him / her whatever they are allowed for.

My JSON Data

    {
  "Dashboard": [
    {
      "type": "Dashboard",
      "link": "Dashboard.html"
    }
  ],
  "Graphical Reports": [
    {
      "type": "Outlet Sales Summary Pai chart",
      "link": "Outlet Sales Summary Pai chart.html"
    },
    {
      "type": "Payment mode wise Graph layout",
      "link": "Payment mode wise Graph layout.html"
    },
    {
      "type": "Outlet wise Sales Area Chart",
      "link": "AreaChart.html"
    },
    {
      "type": "Outlet wise Sales Line Chart",
      "link": "LineChart.html"
    },
    {
      "type": "Top 20 Sold Items",
      "link": "Top20Items.html"
    },
    {
      "type": "Outlet Wise Quantity And Amount",
      "link": "OutletWiseQuantityandAmount.html"
    }
  ],
  "Tabular Reports": [
    {
      "type": "Date wise Outlet wise Sales Summary",
      "link": "Date wise Outlet wise Sales Summary.html"
    },
    {
      "type": "Date wise OL Wise Counter wise Sales",
      "link": "Date wise OL Wise Counter wise Sales.html"
    },
    {
      "type": "Hourly wise Sales Bet Dates",
      "link": "Hourly wise Sales Bet Dates.html"
    },
    {
      "type": "Outlet wise Date wise NoOfBills",
      "link": "Date Wise Ol Wise Bill Count.html"
    },
    {
      "type": "Hourly sales Outlet wise Date wise",
      "link": "Hourly sales Outlet wise Date wise.html"
    },
    {
      "type": "Percentage Contribution Outlet wise",
      "link": "PercentageolWise.html"
    },
    {
      "type": "Outlet wise Item wise Sales With Date",
      "link": "MRPL Store Sales With Date.html"
    }
  ],
  "Drill down Reports": [
    {
      "type": "Sales Drilldown Counterwise Bet Dates",
      "link": "Sales Drilldown Counterwise Bet Dates.html"
    },
    {
      "type": "Sales Drilldown Billwise Bet Dates",
      "link": "Sales Drilldown Billwise Bet Dates.html"
    },
    {
      "type": "Sales Drilldown Itemwise for Date",
      "link": "Sales Drilldown Itemwise for Date.html"
    },
    {
      "type": "Linked Sales Report Bet Dates",
      "link": "LinkingReport.html"
    }
  ],
  "Masters": [
    {
      "type": "Item Master",
      "link": "ItemMaster.html"
    }
  ],
  "Setup": [
    {
      "type": "Change Password",
      "link": "ChangePassword.html"
    },
    {
      "type": "User Admin",
      "link": "UserAdmin.html"
    }
  ],
  "Transactions": [
    {
      "type": "Indent Request",
      "link": "IndentWithCategoryWiseFilter.html"
    }
  ]
}

new Vue({
  el: '#app',
  data() {
    return {
      navData: {
        "Setup": [{
            "menu": "Submenu-1"
          },
          {
            "route": "submenu-2"
          }
        ],
        "Report": [{
            "menue": "subreport-1"
          },
          {
            "route": "subreport-2"
          }
        ]
      }

    }
  },

})

Vue.config.productionTip = false;
Vue.config.devtools = false;
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
<div id=app>

  <nav class="navbar navbar-expand-sm bg-dark navbar-dark">
    <ul class="navbar-nav">
      <!-- Dropdown -->
      <li v-for="data in navData"class="nav-item dropdown">
        <a class="nav-link dropdown-toggle" href="#" id="navbardrop" data-toggle="dropdown">
        {{Object.keys(data)}}
      </a>
        <div class="dropdown-menu">
          <a class="dropdown-item" href="#">menu</a>

        </div>

      </li>
    </ul>
  </nav>
</div>

I have tried but, didn't how to go through this

1
  • what do you expect in output? Commented Jan 3, 2020 at 8:49

2 Answers 2

1

Try below snippet:

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
<div id=app>

  <nav class="navbar navbar-expand-sm bg-dark navbar-dark">
    <ul class="navbar-nav">
      <!-- Dropdown -->
      <li v-for="(data,menu) in navData"class="nav-item dropdown">
        <a class="nav-link dropdown-toggle" href="#" id="navbardrop" data-toggle="dropdown">
        {{ menu }}
      </a>
        <div class="dropdown-menu">
          <a class="dropdown-item" href="#">{{ data[0].menu }}</a>
        </div>
      </li>
    </ul>
  </nav>
</div>

<script>
new Vue({
  el: '#app',
  data() {
    return {
      navData: {
        "Setup": [{
            "menu": "Submenu-1"
          },
          {
            "route": "submenu-2"
          }
        ],
        "Report": [{
            "menu": "subreport-1"
          },
          {
            "route": "subreport-2"
          }
        ]
      }

    }
  },

})

Vue.config.productionTip = false;
Vue.config.devtools = false;
</script>

Sign up to request clarification or add additional context in comments.

Comments

0
var arr = [ {"id":"10", "class": "child-of-9"}, {"id":"11", "classd": "child-of-10"}];

for (var i = 0; i < arr.length; i++){
    var obj = arr[i];
    for (var key in obj){
        var attrName = key;
        var attrValue = obj[key];
    }
}

1 Comment

Please put your answer always in context instead of just pasting code. See here for more details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.