Increment your ids automatically for bootstrap collapse components

To utilize Bootstrap's Collapse components effectively, it is necessary to have sections (or cards) with unique IDs assigned to allow for independent collapsing and expanding. My challenge lies in creating multiple sections, each representing a data point, using the "v-for" directive from vue.js. However, I am unsure of how to automatically generate unique IDs for these cards. Here is my attempt:

<div id="story-content" class="content">
        <div v-for="(subchapter, chapter) in story" class="card">
            <div class="card-header">
                <a href="'collapse' + generateNewId" class="card-link" data-toggle="collapse">
                    [[chapter]]
                </a>
            </div>
            <div id="'collapse' + getCurrentId" class="collapse show" data-parent="#story-content">
                <div class="card-body">
                    [[subchapter]]
                </div>
            </div>
        </div>

</div>

<script> 
    var story = {{story | safe}};

    var app = new Vue({
        delimiters: [ '[[', ']]'],
        el: '#portofolio',
        data: {
            story: story,
            uniqueId: 0,
        },
        methods: {
            generateNewId: function() {
                return uniqueId + 1;
            },
            getCurrentId: function() {
                return uniqueId;
            }
        } 
    })
</script>

Here is an example of the story data structure:

{
    "Chapter1": {
        "Subchapter1": {
            "side_note": "January - June 2019",
            "desc": "description here"
        },
        "Subchapter2": {
            "side_note": "January - June 2019",
            "desc": "description here"
        }
    },
    "Chapter2": {
        "Subchapter1": {
            "side_note": "",
            "desc": ""
        }
    }
}

P.S: I recently discovered the existence of bootstrap-vue, but since I wasn't aware of it during my initial learning phase, I opted not to use it. As a beginner in web development, I hope there is a solution to my unique ID generation issue without relying on bootstrap-vue, as it would require modifications to other components.

Answer №1

Bootstrap 4 utilizes jQuery for handling the Collapse component. One option to tackle this issue is to simply utilize the Chapter keys as the unique identifiers...

<div id="story-content" class="content">
    <div v-for="(subchapter, chapter) in story" class="card">
        <div class="card-header">
            <a :href="'#collapse' + chapter" :data-target="'#collapse' + chapter" class="card-link"
                data-toggle="collapse"> [[chapter]] </a>
        </div>
        <div :id="'collapse' + chapter" 
            class="collapse show" data-parent="#story-content">
            <div class="card-body"> [[subchapter]] </div>
        </div>
    </div>
</div>

Demo:

Nevertheless, using jQuery alongside Vue may not be the most optimal approach, so a more efficient method would involve recreating the collapse functionality in Vue as detailed here

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 utilizing the onmouseover function to emphasize a specific part of an SVG graphic?

I am attempting to create an interactive SVG map of Europe. Each country is represented by a path element, and I want the opacity of a country to change to 0.5 when it is hovered over. Despite trying to define a JavaScript function for this purpose, noth ...

Tips for preventing harmful messages in a chat room app

As I am working on a chat room website, users are able to input any content they want into the entry field and then send it to all online users. However, I have concerns about security - what if malicious individuals try to inject harmful HTML or JavaScrip ...

JQuery integration resulting in disappearance of Input-group-btn element

Allow me to explain my current project. I am in the process of developing a Modal that enables users to input a password There will be an option to toggle between showing or hiding the text in the password field using a button positioned on the right sid ...

Is there an issue with my JavaScript append method?

Within the following method, an object named o gets appended to a list of objects called qs. The section that is commented out seems to be causing issues, while the uncommented section is functional. What could possibly be wrong with the commented part? on ...

Place 4 divs within 2 divs, with one aligned to the left and the other to the right

I'm currently working on a small project using Angular 2 for an experiment, where I need to place 4 divs, each containing an image, within 2 divs. However, all the divs (with images) are stacked vertically instead of being placed next to each other, ...

Easily refresh multiple select options by using the ajax updater function in prototype

After carefully reviewing the documentation for Ajax.Updater(), I noticed that the first argument to the constructor should be container (String | Element) – The DOM element whose contents will be updated as a result of the Ajax request. This can eith ...

Utilizing Async / Await in the created lifecycle hook - Vue2

I recently installed the vue-element-loading package and added its component to my page.vue: <vue-element-loading :active="isActive" :is-full-screen="true"/> After adding a variable to my data: data () { return { isActive: false, } } I th ...

Having trouble with the react event handler for the renderedValue component in Material UI?

I am facing an issue while trying to utilize the onDelete event handler within the chip component using Material UI in the code snippet below. Upon clicking on the chip, it triggers the Select behavior which opens a dropdown menu. Is there a way to modif ...

Creating a responsive div class that is centered

Struggling to center my <div class="loginfeld"> while keeping it responsive. <html> <body> <div class ="loginfeld"> <div class="container"> <div class="col-lg-4"></div> ...

A step-by-step guide to accessing Chrome performance metrics using JavaScript

By utilizing Chrome Dev Tools, I am able to perform the following actions: Begin by opening Chrome Dev Tools (simply right click on any page in Chrome and select "inspect") Navigate to the "performance" tab Click on the record button Interact with a butt ...

Mapping Longitude and Latitude with TopoJSON and D3

Currently utilizing the UK Geo JSON found at this link to generate a UK SVG Map. The goal is to plot longitude and latitude points onto this map. The GeometryCollection place is being added to the map in the following manner: data.objects.places = { ...

Create a basic search functionality in an Express Node.js application

Recently, I decided to work on a project to enhance my coding skills. I wanted to create a simple search functionality where the URL would be "/search/(parameter here)" and display products whose names match the parameter. After writing a middleware for t ...

trigger a function when the iframe is altered

I am currently working on creating a function that can process credit card payments using an iframe. The challenge I am facing at the moment is dealing with error messages appearing in a less than ideal manner. Photo: I have been attempting to capture the ...

Learn the process of covering the entire screen with a video

I'm attempting to achieve this: IMG Below is the code snippet I've been using: <div class="container" id="containervideo"> <div id="video"> <div class="box iframe-box"> <div class="container"> ...

Waiting for the listener script to finish its task in an Ajax call and then informing the user

I have developed a unique program that allows users to submit test cases from a specific webpage (main.php). The webpage triggers an ajax request to insert user data into MySQL using the file insert.php, with the value done=0. Subsequently, there is a list ...

What's the best way to enable touch scrolling for items within a div container?

I am in the process of creating a code snippet that will allow users to scroll through items within a div, similar to a spinning wheel or slot machine. While I am aware of an existing solution for iPhone and iPod, I want to develop a simpler, more streamli ...

Guidelines for choosing input using jQuery

I am looking to retrieve the value of an input using jQuery. Specifically, I need to extract the value of a hidden input with name="picture" when the user clicks on the حذف این بخش button by triggering the function deleteDefaultSection( ...

The JavaScript exec() RegExp method retrieves a single item

Possible Duplicate: Question about regex exec returning only the first match "x1y2z3".replace(/[0-9]/g,"a") This code snippet returns "xayaza" as expected. /[0-9]/g.exec("x1y2z3") However, it only returns an array containing one item: ["1"]. S ...

Secure User Validation Using ExpressJS and CouchDB

I am currently working on implementing a CouchDB User login into my express app using a frontend form and storing the login information in the session. Here is what I have done so far: In my app.js file: var express = require('express'); var co ...

React Material-UI select component malfunctions when I enclose the menu item within a Tooltip wrapper

Here is a snippet of my code: return( <FormControl sx={{ m: 1, minWidth: 80 }}> <InputLabel id="demo-simple-select-autowidth-label">Age</InputLabel> <Select labelId="demo-simple-select-autowidt ...