When using a v-for within a v-if condition, the v-else directive does not function properly, resulting

Utilizing the Vue programming language and Element-ui framework, my code functions in a way where if an object contains something, it will be displayed. Otherwise, the menu button is disabled.

The expected output should look like:

a, b(disable), c, d, e

However, my current output is:

a, a(disable), b(disable), c(disable), d(disable), e(disable), c, d, e

If there is a flip between the v-if statement and v-else-if statement, the output changes to:

a(disable), b(disable), c(disable), d(disable), e(disable), b, a(disable), b(disable), c(disable), d(disable), e(disable), a(disable), b(disable), c(disable), d(disable), e(disable), a(disable), b(disable), c(disable), d(disable), e(disable)

Therefore, I suspect that when the if-else condition is true, the for loop displays items based on whether they meet the condition rather than displaying specific items that pass the condition.

In cases where an array contains something, both the array and its child are displayed.

<el-menu
     :default-active="activeIndex"
     mode="horizontal"
     @select="handleSelect">

     <el-submenu
          v-if="item.subCategories && item.subCategories.length <= 0"
          v-for="(item, index) in category.categories" :index="(index+1).toString()"
          :key="item.parentCategory.categoryId">

          <template slot="title">
               {{ item.parentCategory.categoryName}}
          </template>

          <el-menu-item
               index="(index+1).toString()-(i+1).toString()"
               v-for="(child, i) in item.subCategories"
               :key="child.categoryId"
               @click="searchEventByCategory(item.parentCategory.categoryId, child.categoryId)">
                    {{ child.categoryName }}
          </el-menu-item>
     </el-submenu>

When an array contains nothing, the menu cannot be clicked and does not have any children.

     <el-submenu
          v-else-if="item.subCategories && item.subCategories.length > 0"
          disabled
          v-for="(item, index) in category.categories" :index="(index+1).toString()"
          :key="item.parentCategory.categoryId">

          <template slot="title">
               {{ item.parentCategory.categoryName}}
          </template>

          <el-menu-item>
          </el-menu-item>

     </el-submenu>
</el-menu>

Lastly, I am wondering if it is possible for objects that contain nothing to display as independent menu items (el-menu-item) instead of being part of a submenu (el-submenu) with second-level menus.

Please assist me with this issue! Thank you.

Answer №1

If you want to dynamically set the 'disabled' property in Vue, you can utilize v-bind:disabled or the shorthand :disabled. This approach eliminates the need for redundant for loops or v-if conditions.

<el-menu
     :default-active="activeIndex"
     mode="horizontal"
     @select="handleSelect">

     <el-submenu
          v-for="(item, index) in category.categories" 
          :index="(index+1).toString()"
          :key="item.parentCategory.categoryId"
          :disabled="item.subCategories && item.subCategories.length <= 0"> // sets disabled condition dynamically when true

          <template slot="title">
               {{ item.parentCategory.categoryName }}
          </template>

          // If there are no subcategories, this loop will not render anything.
          <el-menu-item
                v-for="(child, i) in item.subCategories"              
                :index="(index+1).toString()-(i+1).toString()"
                :key="child.categoryId"
                @click="searchEventByCategory(
                item.parentCategory.categoryId,child.categoryId
                )">
                    {{ child.categoryName }}
          </el-menu-item>
     </el-submenu>
</el-menu>

It's recommended to avoid using v-if and v-for together on the same element.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Exploring the Reach and Sequence of AJAX Callbacks

This particular piece of code aims to achieve three main tasks: 1) validate the online status of users, 2) retrieve their information from a slightly different URL, and 3) present both sets of data in HTML format. The implementation appears functional bu ...

Sorting nested table rows in vueJS is an essential feature to enhance

I am working with a json object list (carriers) that looks like this: https://i.stack.imgur.com/0FAKw.png Within my *.vue file, I am rendering this using the following code: <tr v-for="carrier in this.carriers"> <td>{{ carrier.id ...

The component cannot be created using shallowMount because vm.$refs['VTU_COMPONENT'] is not defined

Not sure if it's a bug or if I'm making a mistake. I attempted to use shallowMount to mount my main App component, but it doesn't seem to work. The error message I receive is: Cannot set properties of undefined (setting 'hasOwnProper ...

Using Node.js for HTML redirections involves creating routes and setting

I am currently attempting to connect my Node.js API with my HTML pages. For the most part, everything is functioning correctly, but I have encountered some confusion along the way. Is there a more efficient method for redirecting an HTML page? Typically, ...

Inverting Arrays Recursively with JavaScript

Currently, we are delving into functional JavaScript in my programming course with a particular assignment. However, I seem to be struggling to make the code work as intended. I would greatly appreciate any advice on how to improve this piece of code. Ever ...

Restarting a JavaScript function upon switching views in Vue.js

I am new to working with Vue.js and I have a Laravel app that utilizes it. One issue I am facing is that when the homepage is loading, all elements like owl carousel and rev slider are initialized. However, if I navigate to other routes such as contact or ...

Why is it important to link the image source with the URL in jQuery?

$("<img src="+url+">").appendTo($("body")); The complete data is stored in the url variable, and then it is added to the body element. What is the reason for using the image source to set the url here? ...

Javascript error when attempting to add leading zeros

Is there a way to utilize JavaScript or JQuery in order to prepend a zero to this script? for (im=1;im<=31;im++){ days[im]=everyDay[im]; } ...

Navigating the complexities of managing group radio buttons in React JS within a functional component

I'm looking to implement a functionality where only one radio button can be selected at a time within a group. I've searched online for class component code, but I'm in need of a solution using React hooks. Can someone help me with an onChan ...

Utilize JSON data in d3.js for creating a scatterplot visualization

I have recently started working with d3.js and I'm facing some challenges. From what I understand, the second parameter passed in d3.json() is used to process data from a JSON file. Here is my code: d3.json(base_url() + "data/data.json", clean_data) ...

Express in Node.js is designed in a way that error handling can only be done using the

Currently utilizing Node.js Express for developing HTTP REST APIs. The methods call a service that returns a Promise in the example below: function retrieveAllApps(request, response) { appService.getAllApps(request.query.$expand).then(function (apps) ...

Is having an excessive amount of reactive data in Vue.js detrimental to app speed and overall performance?

Our team is currently in the process of developing a highly intricate single page application similar to Excel using vue.js. This project consists of over 10,000 components, with each cell functioning as an individual component containing approximately 100 ...

Utilizing CSS-in-JS to eliminate arrow buttons from a TextField

I'm currently developing a webpage using React and Material-UI. One of the components I have is a TextField element, and I need to remove the arrow buttons that typically appear on number input fields. If I were utilizing CSS, I could easily achieve t ...

What is the most effective method for locating and modifying the initial instance of an element within a group?

In my Javascript/Typescript collection, I have the following items: [ {"order":1,"step":"abc:","status":true}, {"order":2,"step":"xyz","status":true}, {"order":3,"step":"dec","status":false}, {"order":4,"step":"pqr","status":false}, {"order":5,"step":" ...

Struggling with JavaScript conditional statements

Currently, I am in the process of creating a login and registration panel using jQuery and PHP. Essentially, if there are any errors during registration, it sets certain form errors, redirects back to the registration page, the JavaScript identifies these ...

"Encountering an issue with Laravel Vue JS where it is unable to locate the specified

This is my category component <tr v-for="(categoryList, index) in getCategoryList" :key="categoryList.id"> <td>{{index+1}}</td> <td>{{categoryList.cat_name}}</td> <td> ...

Generate a hyperlink that directs to a page and triggers the expansion of an accordion section

I am currently working with a Bootstrap 5 accordion component: <div class="accordion" id="accordionIndexPage"> <div class="accordion-item"> <h2 class="accordion-header" id="flush-headingOne& ...

Modifying an object property by replacing it with a different value stored in the same object - JavaScript

Consider the object below: { id: 1, name: 'jdoe', currentDayHours: null, totalHours: [{ task: 'cleaning', hours: 10}, { task: 'reading', hours: 2 }]} A function is needed to update the currentDayHours based on the task param ...

Is it feasible to replicate an error in TypeScript with certain modifications?

Currently, I'm facing a challenge where I need to utilize Sentry.captureException with an error that I have received. However, before doing so, I am attempting to modify the error message. Despite spending considerable time searching for a solution, I ...

Vietnamese special characters are not supported in JSON on Windows Phone 8.1 when using IBM Mobile First

In my IBM mobile first application, I am facing an issue with JSON response containing Vietnamese characters (e.g. Tôi là một nhân vật đặc biệt) on Windows 8.1 Phone. The character encoding I am using is UTF-8. resourceRequest.send($scope.dat ...