How can I display a nested div when the parent div's v-if condition is not met?

In my project, I am utilizing Laravel 6 in combination with Vue.js 2.

To iterate through users and showcase their information within div elements, I am using a for loop outlined below.

The Category names are stored in user.pivot.demo2, and the users are sorted by category utilizing lodash:

_.orderBy(this.category.users, ['pivot.demo2', 'pivot.demo3'])

I want to ensure that the div with the class of "row" appears only once for each category. By having the category name displayed only if it is the first in the list or does not match the previous one, all users are separated by category when displayed, with the respective category caption placed above their divs. While this setup works perfectly, I'm struggling with ensuring that the outer div with the class "row" is generated only once per category, then filled with the user divs with the class "col-lg-4."

In Laravel's Blade, achieving this would involve logic like:

@if(category != the-same-category)
<div class="row">
@endif
@if (category == the-same-category)
<div class="col-lg-4">{{ User.name }} </div>
@endif

@if(category != the-same-category)
</div>
@endif

This logic ensures that the div with the "row" class is displayed only once per category, while the inner divs with the "col-lg-4" class repeat on every loop. It also closes the div at the right conditions, resulting in each category having its own divs with the user inside a single row div. However, I have been unable to find a similar solution for Vue.js. Can anyone suggest a JavaScript or Vue.js approach to achieve this behavior?

Currently, my Vue code looks like this:

   <div class="outer-div text-center" v-for="(user, index) in the_category.users" :key="user.id">
  <div v-if="index === 0 || user.pivot.demo2 != the_category.users[index -1].pivot.demo2">
    <div class="text-center header-one">
      <h2>{{ user.pivot.demo2 }}</h2>
    </div>
    <hr />
    </div>

<div class="row d-flex justify-content-center"> // display once per category
  <div class="outer-profile-div col-md-4"> // display every loop
    <div class="user-name text-center">{{ user.name }}</div>
    <div class="user-title text-center">{{ user.title }}</div>     
  </div>
</div>

Ultimately, my aim is to showcase the ".outer-profile-div" elements next to each other, wrapping as typical bootstrap divs inside a ".row", all while maintaining responsiveness. I am open to utilizing CSS, JavaScript, Vue.js, or any other method that can help achieve the desired outcome.

Answer №1

If you're in need of a solution like this, I believe I have just the thing for you. This code allows you to maintain a parent element while only looping through the child elements. It grants you the ability to utilize conditional directives on the child elements by making the template tag the parent.

-This example is inspired by the vue-js documentation- Give it a try for your issue and let me know if it resolves it

<ul>
  <template v-for="item in items">
    <li>{{ item.msg }}</li>
    <li class="divider" role="presentation"></li>
  </template>
</ul>

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

Tips on managing a GET request sent using axios with special characters and accents

When constructing a web page using VUE.JS, a GET request is made with Axios. In the form fields, special characters and accents may be present depending on the user's input. For instance, if the surname entered in the form field is 'Ruíz' ...

Focus event in IE does not always work as expected after an ajax request is

Our current focus is on supporting IE8 exclusively. An ajax call retrieves data from the server, replaces the HTML in a container div with the response, and then attempts to focus on an element within the response. However, there seems to be inconsistenci ...

What is the best way to divide data prior to uploading it?

I am currently working on updating a function that sends data to a server, and I need to modify it so that it can upload the data in chunks. The original implementation of the function is as follows: private async updateDatasource(variableName: strin ...

New in the world of JavaScript: A method that replicates the functionality of jQuery's

Take a look at this JSFiddle example: https://jsfiddle.net/1a6j4es1/1/ $('table').on('click', 'td', function() { $(this).css('background', 'green'); }); How can you rewrite this code using plain JavaS ...

When the LI element is clicked, it triggers the display of another LI element without affecting the rest of the

New to the web development scene and trying to figure out how to create a collapsible text feature using HTML, JavaScript, and JQuery. I've got the styling down but struggling with the scripting part. Here's an example of what I'm trying to ...

Obtain user information upon the first page visit using Nuxt middleware

I'm currently facing an issue with Firebase Auth integration in my Nuxt application, specifically concerning protected routes. The middleware I have set up in Nuxt handles redirection based on the user's login status during page navigation. The ...

What is the best way to make changes to the DOM when the state undergoes a

I've programmed the box container to adjust dynamically based on input changes. For instance, if I entered 1, it will generate one box. However, if I modify the input to 2, it mistakenly creates 3 boxes instead of just 2. import React from 'rea ...

Update image attributes (such as src, alt, etc.) after a successful AJAX request (and also help me troubleshoot my AJAX

The image link I am currently using is: echo "<br/><div class='right' id='post" . $id . "'> <img id='thumb' src='/images/like.png' title='Like it?' alt='Like button' onC ...

When attempting to insert data retrieved from axios into a table within a React component, the data is coming back as

Hi there! Currently, I'm in the process of developing an application that retrieves data from an API and my goal is to display it in a Material UI Table within a React environment. Strange issue I'm encountering: when I use console.log to check ...

Responsive height using Material Design Lite

I have a website using MDL, with a prominent header centered on the page. To enhance visibility, I added a background box behind the text. To view the CodePen example, click here. The challenge is to ensure that when the window is resized, such as on a m ...

Has anyone figured out the issue with Uplodify? It suddenly ceased to function

For some time now, I've been using uplodify without any issues. Unfortunately, as of yesterday, it suddenly stopped working. I'm at a loss as to what might have caused this sudden change. Could someone please offer me some assistance? I've ...

Ways to showcase outcomes from identical column with varying data

In this code snippet, I am fetching data from my database where there are two columns - title_heading and title. id title_heading title 1 HTML Tutorial Home 2 HTML Tutorial Overview 3 HTML References Tags 4 HTML Ref ...

It is not possible to delete a class from an element once it has been added

Issue can be replicated by visiting the following URL: Click on the hamburger icon to open the navigation menu Click on "Services" Click "< Services" within the submenu to attempt to go back For some reason, the removeClass method is not removing the ...

Utilizing the splice method across multiple instances of a string

When facing a string like "This website is blocked by administrator. Please get the admin permissions. You will be allowed only if permission is granted" that needs to be split into three lines for better readability, one solution is using the splice metho ...

Enhance your Vue PWA by utilizing ServiceWorker to efficiently cache remote media assets fetched from an array of URLs

In my PWA development project, I am looking to provide users with the option to download and cache all media assets used in the application. However, the default behavior of PWAs only caches assets when they are requested during app navigation. My goal is ...

Restart the calling process using NodeJS command

Is there a way to automatically restart the calling process in case certain events occur while querying a database? I want the process to start over if specific conditions are met. ...

Can someone help with the issue where the CSS field position changes when the "X" icon appears, as mentioned in the title? I am looking for a solution to fix this problem

When trying to add an additional field on a page, the "X" icon appears inline with the field and causes the other fields to adjust their position. However, I want the new field to remain in the same position as the title. How can I achieve this while keepi ...

Doing server side function calls from the client in NodeJs

I recently embarked on a journey to learn web development and am eager to make server-side data changes when invoking client functions. Take a look at my sample server setup: const fs = require('fs'); const path = require('path'); con ...

"Enhance Your HTA Webpage with a Hover Zoom Effect on CSS Tables

Greetings and thank you for taking the time to read my message. I am relatively new to this, so please bear with me if things seem a bit disorganized. I have constructed a table and am attempting to implement a hover effect to zoom in on a specific cell w ...

Is there a way to dim and deactivate a button after it has been clicked once?

Hello, I am attempting to disable and grey out a button after it has been clicked once in order to prevent the user from clicking it again until they refresh the page. I hope my question is clear enough and that you can understand me. Below is the HTML cod ...