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

What is the method for extracting a list of properties from an array of objects, excluding any items that contain a particular value?

I have an array of objects, and I want to retrieve a list with a specific property from those objects. However, the values in the list should only include objects that have another property set to a certain value. To clarify, consider the following example ...

Ways to determine the completion of the compilation process in Angular using the $compile service

Imagine having a popup directive that inherits a string template for the content it should display from the $scope. scope: { template: '=popInfo'//<div another directive></div> } This template string may even include another dire ...

Using Laravel with Vue and <router-link> does not update the content as expected

For my project, I am using Laravel with Vue. On the main page, I have listed all articles and when a user clicks on a specific article, a new page opens displaying that article with other prominent articles shown on the side like this: .https://i.stack.img ...

Unveiling the Power of Angular in Handling Date and Time

Recently, I encountered an issue with the following code snippet: <ng-template>{{date:'MM/dd/yyyy h:mm:ss a Z'}}</ng-template>. This code displays a date and time in one long line. My goal is to insert a line break between the date an ...

Reactjs Router.push function does not behave as intended

I'm currently working with Reactjs and Next.js. I am experiencing an issue where the correct data is only displayed after refreshing the page instead of upon clicking, as intended. To solve this problem, I have attempted to use "router.push", but unfo ...

How can I ensure that my CSS code functions seamlessly across all versions of Internet Explorer from IE6 to the latest release?

I need a solution for getting this CSS code to function properly across all versions of Internet Explorer, from IE6 to the most recent version. body:first-child * { margin-top:0 !important; } body:last-child * { margin-bottom:0 !important; } Any ...

customizable path settings for sprites using css

Currently, I have a situation where I am using many sprites in my CSS file, and all the image locations are hardcoded within the CSS itself. Now, I am considering moving all these images to a CDN. The problem is that it's quite cumbersome to change th ...

Tick the box to activate the ability to uncheck multiple boxes when there are already a certain number of boxes checked

I am currently developing a multiple-choice form in my MVC5 app and I am struggling to find a way to disable unchecked boxes once a specific number of boxes have been checked. For example, if 3 out of 5 boxes are checked, the remaining 2 should be disabled ...

Adjust the checkmark color of MUI Checkbox

When using Material UI's MUI library for React, I encountered an issue with the checkbox checkmark color. By default, the checkmark takes on the background color of the container element, which is great for light backgrounds but not ideal for dark one ...

Tips on utilizing the enter key to add my <li> element to the list?

Currently, my to-do list setup requires users to click a button in order to add a new list item. I am looking to enhance the user experience by allowing them to simply hit "enter" on their keyboard to achieve the same result. Here is the JavaScript code I ...

Center the image within the div by setting its position to absolute

<div class='img-box'> <img /> //position absolute <img /> //position absolute <img /> //position absolute <img /> //position absolute I am struggling to center the images within this div because of their absolute p ...

Is there a way to prevent Prettier from automatically inserting parentheses for a single argument in an arrow function?

Currently, I've integrated Prettier into my workflow, but I've encountered an issue with arrow functions in JavaScript. For example: arg => console.log(arg) However, Prettier automatically formats it as: (arg) => console.log(arg) This for ...

Integrating external JavaScript libraries into Ionic using the correct process

For the last few months, I have been utilizing jQuery Mobile for a hybrid app. Now, I am interested in exploring Ionic and Angular.js, so I am attempting to reconstruct it. My current JQM application relies on xml2json.js, but I do not have any experience ...

How to effectively reach multiple platforms using the Vue CLI plugin Electron Builder

I am in the process of developing my Vue Electron app and using Electron Builder to target both macOS and Windows with a portable release. However, I have encountered an issue where after clicking on the electron:build option in Vue UI, it only generates ...

How can I access the marker's on-screen location in react-native-maps?

Looking to create a unique custom tooltip with a semi-transparent background that can overlay a map. The process involves drawing the MapView first, then upon pressing a marker on top of the MapView, an overlay with a background color of "#00000033" is dra ...

Node and browser compatible JavaScript logging library for effective logging experience across platforms

Our team is utilizing Visionmedias debug library, as it seamlessly functions on both browsers and Node.js environments. We are in search of a more reliable alternative to debug that offers the same level of functionality (error, warn, info, etc) for both ...

What is the best way to generate a "JSON diff" that can be displayed in the JavaScript console?

When working on my Angular project, I frequently encounter the need to compare JSONs in my Karma/Jasmine tests. It would be incredibly useful to have a console output showing what has been added and removed when comparing two structures. For example, ident ...

Having trouble getting a JS file to work with the Laravel Inertia.js and Vue.js3 template

Just getting started with inertia.js and facing an issue. My CSS styles are being applied, but my JavaScript file isn't functioning as expected. I'm working on an admin dashboard using Laravel 9 and vue.js-3 with a custom HTML template. Interes ...

Getting a boolean response from an asynchronous SQLite query in Express

I am currently developing a middleware that verifies the validity of a session (meaning it has a logged-in user attached). For this purpose, I am utilizing sqlite3 for node.js. Since I am not very familiar with JavaScript, I am facing some challenges figu ...

Understanding surface orientations of moving objects

I am experimenting with creating a software-based vertex animation for a mesh. It's like a vertex shader, but done in software rather than hardware. Essentially, I am applying a transformation matrix to each vertex of the mesh. While the mesh itself ...