Steps for generating tab panels and their respective content using a v-for loop

I am currently utilizing Vue 3 in combination with Bootstrap 5.

My objective is to incorporate multiple Tabpanels within a v-for. Each of these Tabs should feature distinct content.

To achieve this, I attempted placing my Tabs and their respective Content inside a v-for. However, the desired outcome is not being achieved at the moment (I cannot view the Content) and I am unable to pinpoint the issue. Additionally, I am uncertain whether two v-fors are necessary or if it is feasible to encapsulate everything within one.

Any assistance provided would be greatly appreciated.

<ul class="mt-3 nav nav-pills" id="all_tabs" role="tablist">
  <div v-for="item in input" :key="item.id">
    <li class="ms-1 nav-item" role="presentation">
      <button
        class="nav-link active"
        :id="item.id"
        data-bs-toggle="tab"
        :data-bs-target="'#tab' + item.id"
        type="button"
        role="tab"
        :aria-controls="'tab' + item.id"
      >
        {{ item.name }}
      </button>
    </li>
  </div>
</ul>
<div class="tab-content mt-3" id="all_tabs">
  <div v-for="item in input" :key="item.id">
    <div class="tab-pane fade" :id="'tab' + item.id" role="tabpanel">
      <span>{{item.name}}</span>
    </div>
  </div>
</div>

Just for clarification on how the input is generated, here is what I do in the mounted hook:

data() {
  return {
    array: [1,2],
    input: [],
  }
}

mounted() {
  this.array.map((i) => {
    this.input.push({
      id: i, 
      name: "Input" + i,
    })
  })
}

Answer №1

Below is the code I used to make it function properly:

Note: I opted for <script setup> as I am more familiar with it :) - your Options-API method will yield the same results.

script

<script setup>
import { ref } from 'vue';

const input = ref([]);
const array = ref([1,2]);

array.value.forEach((i) => {
    input.value.push({
        id: i,
        name: "Input" + i,
    });
});
</script>

template (crucial part):

<ul class="nav nav-pills mb-3" id="pills-tab" role="tablist">
    <li v-for="(item, index) of input" class="nav-item" role="presentation">
        <button class="nav-link"
                :class="index === 0 ? 'active': null"
                :id="`${item.name}-tab`"
                data-bs-toggle="pill"
                :data-bs-target="`#${item.name}`"
                type="button"
                role="tab"
                :aria-controls="item.name"
                :aria-selected="index === 0"
        >
            {{ item.name }}
        </button>
    </li>
</ul>
<div class="tab-content" id="pills-tabContent">
    <div v-for="(item, index) of input"
         class="tab-pane fade"
         :class="index === 0  ? 'show active': null"
         :id="item.name" role="tabpanel"
         aria-labelledby="pills-home-tab"
    >
        {{ item.name }}
    </div>
</div>

main.js

import 'bootstrap/dist/js/bootstrap.bundle.min.js';
import 'bootstrap/dist/css/bootstrap.min.css';

Furthermore, keep in mind that map serves a different purpose than forEach - in this scenario, using forEach would be suitable.

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

Using v-model in Vue 3 will result in modifications to the table class in Bootstrap 5

Below is a snippet of the code I wrote: <table class="table table-striped"> <tr class="table-dark"> <th>#</th> <th>Column 1</th> <th colspan="3">Column 2</th> </tr> <tr ...

Grouping various event listeners within a v-for loop

My Desired Outcome In my Vue component, I am displaying a list of actions using v-for. Each action should have a corresponding @click event handler that triggers the action method within the component. I need help declaring these actions in my data() fun ...

Height Issue with Nested Columns in Bootstrap 5

My nested columns are giving me trouble with their height. I want them to have equal heights on both big and small screens. Currently, the sizes look perfect on the largest and smallest viewports, but not on medium-sized ones. Check out examples on codepen ...

Steps for removing the bottom border for the last child in the Material UI Box Component

I have a Box Component where ListItems are wrapped. For each child, I have a border-bottom of 1px solid class set for the Box component, but I don't want it for the last child. How can I achieve that? {data.map((item, i) => { return ...

A missing piece in the Blazor Visual Studio sample template: the absence of bootstrap.js and references to essential .js

After setting up a Blazor (server side) project in Visual Studio, I noticed the following code in _Host.cshtml: <link rel="stylesheet" href="css/bootstrap/bootstrap.min.css" /> <script src="_framework/blazor.server.js" ...

Error with Webdriver/FXDriver utils.js leading to Firefox unresponsive script issue

While running browser tests with Watir webdriver and FXDriver, everything seems to be functioning well except for one test that loads a lightbox containing a large amount of HTML. When this lightbox opens, Firefox displays a popup indicating that Utils.js ...

Processing a JSON array of objects in AngularJS

When using Angular's fromJson function to parse a JSON string, I encountered an issue. If the JSON is a simple array like "[1, 2]", the code works fine. However, I need to work with an array of dictionaries instead. var str = "[{'title' ...

Pattern to identify a 32-character string comprising a mix of letters and numbers

I am in search of a JavaScript regex pattern that can identify strings with the following format... loYm9vYzE6Z-aaj5lL_Og539wFer0KfD pxeGxvYzE6o97T7OD2mu_qowJdqR7NRc gwaXhuYzE6l3r1wh5ZdSkJvtK6uSw11d These strings are always 32 characters long and conta ...

What types of elements can a v-bind:is directive display?

One of the challenges I am facing involves a component that registers over 15 external components. Within the template, there is a dynamic component implemented as follows: <template> <component :is="config.name" /> </template> ...

Is there a way to prevent my smartchatbox from covering my fixed top navbar?

Click here for more information I am seeking assistance. Your help is greatly appreciated. The question's heading reveals all you need to know. All you have to do is resize your browser, scroll down, and the answer will become apparent. ...

Create a page turning effect in React that simulates scrolling a certain amount at a time

Is there a way to disable default scrolling and have the page automatically scroll to the next item? For example, if I have element2 below element1, how can I set it up so that when I scroll down once, the page scrolls directly to the position of element2 ...

What is the best way to halt a window.setInterval function in JavaScript?

I have a JavaScript function that runs every 2000ms. I need to find a way to pause this function so that the user can interact with other elements on the page without interruptions. Is there a way to achieve this? Below is the code for the function: win ...

Adding an additional style based on a condition within the style binding in Vue.js can be accomplished by using a ternary

Is there a way to include an additional style conditionally within the existing inline binding? :style="{ 'background-color': bgColor, color: fColor }" I want to add a borderBottom:2px style based on the isInCheckout condition without ...

Deactivate DropDownList within Update Panel with JQuery

Below is the Update Panel that I am working on: <asp:ScriptManager ID="ScriptManager1" runat="server" /> <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server"> <Triggers> <asp:AsyncPostBackTrigger ControlID="d ...

How to Disable a Dynamically Generated <li> Element Using jQuery on Click Event

Script Query: Creating <li> Elements Dynamically echo '<div class="pagination"><ul>' . "\n"; // Previous Post Link. if ( get_previous_posts_link() ) { printf( '<li>%s</li>' . "\n", get_previ ...

I am attempting to access data through an ajax function, but it is not functioning correctly

When working with asp.net webform, I encountered an issue while trying to call data using an ajax call. Although a similar function on another page works without errors, on this particular page, I am facing an error. The error I am getting is pageCountInt ...

Troubles with concealing dropdown menu when hovering

I have noticed that this issue has been raised before, but none of the solutions provided seem to fix my problem specifically. The submenu in my menu is not functioning as intended. It should be displayed when hovered over and hidden when not being hovere ...

Navigating Google GeoChart Tooltips using Google Spreadsheet Data

My objective is to create a custom GeoChart for the company's website that will display data specific to each state in the US region based on team member assignments. The GeoChart should color states according to which team member helps that state&apo ...

The replace() function is failing to replace the provided inputs

Supposedly, when a user types in certain profanity and submits it, the word is supposed to be replaced with a censored version. Unfortunately, this feature is not working as expected. The word appears uncensored. Do you think implementing if/else stateme ...

Detecting unutilized space in a collection of divs with varying sizes using JavaScript and CSS

To better understand my issue, I created a StackBlitz demo: https://stackblitz.com/edit/angular-aqmahw?file=src/app/tiles-example.css Screenshot My tiles can have four different widths (25%, 50%, 75%, 100%). The tiles must fit on only two lines, so if a ...