Changing the IDs dynamically with JavaScript upon loading prevents Bootstrap Collapse from functioning correctly

I'm currently working on collapsing information from each row of a table. The rows are generated using the Odoo framework, so I had to iterate through the table and update the values accordingly to associate the data-target with the corresponding ID.

Even though the IDs in the console and after inspecting the elements appear to be the same, the collapse feature is not functioning as expected.

Could someone assist me in identifying the cause and possibly suggesting a solution? Thank you in advance.

Javascript

var table = document.getElementById("social_programs_table");
    var incr = 0;

    //i = 1, because the first row should be ignored
    for (var i = 1, row; row = table.rows[i]; i++) {
        //iterate through rows

        if (i % 2 != 0) {
            incr++;
            row.cells[5].firstChild.nextElementSibling.dataset.target = "target" + incr;
            console.log("Row Target ID: " + row.cells[5].firstChild.nextElementSibling.dataset.target);
        }
        else {
            row.id = "target" + incr;
            console.log("Row ID: " + row.id);
        }
}

Respective Table

 <table class="table table-striped" id="social_programs_table">
                            <thead>
                              <tr>
                                <th scope="col">Nome da Entidade</th>
                                <th scope="col">Designação do Projeto</th>
                                <th scope="col">Duração (meses)</th>
                                <th scope="col">População-alvo</th>
                                <th scope="col">Fonte de Financiamento</th>
                                <th scope="col">Saber Mais</th>
                              </tr>
                            </thead>
                            <tbody>
                                <!-- ? Loop -->
                                <t t-foreach="programs" t-as="obj">
                            <tr>
                                <th scope="row"><t t-esc="obj.beneficiary_entity" /></th>
                                <td><t t-esc="obj.name" /></td>
                                <td><t t-esc="obj.duration" /></td>
                                <td><t t-esc="obj.target_audience" /></td>
                                <td><t t-esc="obj.financial_source" /></td>
                                <td id="collapse_td">
                                    <button class="btn btn-primary" data-toggle="collapse" data-target="#target" id="collapse_btn">+</button>
                                </td>
                            </tr>

                            <tr class="collapse out" id="target">
                                <td>This text should be collapsed</td>
                            </tr>
                            </t>
                              <!-- ? End loop -->
                          </tbody>
</table>

Answer №1

Perhaps the issue is due to duplicate identifiers.

Consider using a different unique value as an identifier, such as t-att-id="obj.name" and assigning the data-target similarly like t-att-data-target="obj.name". Make sure to only use "name" if it is distinct, and refer to the following code snippet for guidance.

HTML CODE:

<table class="table table-bordered table-sm ">
    <thead class="thead-dark">
        <tr>
            <th>Column</th>
            <th>Column</th>
            <th>Column</th>
	    <th>Column</th>
        </tr>
    </thead>
    <tbody>
        <tr class="clickable" data-toggle="collapse" data-target="#group-of-rows-1" aria-expanded="false" aria-controls="group-of-rows-1">
            <td><i class="fa fa-folder"></i></td>
            <td>data</td>
            <td>data</td>
            <td>data</td>
        </tr>
    </tbody>
	<tbody id="group-of-rows-1" class="collapse">
	    <tr class="table-warning">
	        <td><i class="fa fa-folder-open"></i> child row</td>
	        <td>data 1</td>
	        <td>data 1</td>
	        <td>data 1</td>
	    </tr>
	    <tr class="table-warning">
	        <td><i class="fa fa-folder-open"></i> child row</td>
	        <td>data 1</td>
	        <td>data 1</td>
	        <td>data 1</td>
	    </tr>
	</tbody>
	<tbody>
	    <tr class="clickable" data-toggle="collapse" data-target="#group-of-rows-2" aria-expanded="false" aria-controls="group-of-rows-2">
	        <td><i class="fa fa-folder"></i></td>
	        <td>data</td>
	        <td>data</td>
	        <td>data</td>
	    </tr>
	</tbody>
	<tbody id="group-of-rows-2" class="collapse">
	    <tr class="table-warning">
	        <td><i class="fa fa-folder-open"></i> child row</td>
	        <td>data 2</td>
	        <td>data 2</td>
	        <td>data 2</td>
	    </tr>
	    <tr class="table-warning">
	        <td><i class="fa fa-folder-open"></i> child row</td>
	        <td>data 2</td>
	        <td>data 2</td>
	        <td>data 2</td>
	    </tr>
	</tbody>
</table>

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 are some strategies for increasing my website's mobile responsiveness?

I've recently completed my online portfolio, and it appears to be functioning properly on desktop computers. However, I'm encountering responsiveness issues when viewing it on other devices or smartphones. Even though I have included the necessar ...

Button functions properly after the second click

import { Input, Box, Text, Divider, Button } from '@chakra-ui/react'; import { useState } from 'react'; export default function GithubSearchApp() { const [username, setUsername] = useState(''); const [data, setData] = use ...

Incorporate a class into the fixed navigation menu using fullpage.js

I am attempting to modify the behavior of a sticky menu as the user scrolls down using fullpage.js. My goal is to have the #top-wrapper behave normally when the first section/page loads, and then add a class of is-fixed as you scroll down. When scrolling ...

Is there a way to eliminate the padding for the bootstrap jumbotron specifically on mobile devices?

Hello, I am currently utilizing the bootstrap jumbotron class and facing an issue - when the screen size drops below 500 pixels, I want to remove the padding that is automatically set by Bootstrap (padding: 2rem 1rem;). However, I'm struggling to over ...

AngularJS controllers and $scope are essential components in structuring and

As a newcomer to Angular, I've spent some time reading up on scopes and controllers, but I still feel like something isn't quite clicking for me. Let's take a look at this code snippet: var myApp = angular.module("myApp", []); myAp ...

Construct a connection between divs

Which element is necessary to generate black lines within the image ? [![enter image description here][1]][1] I have already coded but I'm seeking to produce lines similar to the image (between images). What's the process for achieving this? Kin ...

What are the solutions for resolving CORS issues with a local HTML file using three.js?

I have been attempting to create a sphere using three.js with an image texture, but no matter if it's a local image or an https image online, I always encounter the following error: Access to image at 'file:///C:/Users//.....//sun.jpg' fro ...

Is there a way to transmit data to another page upon clicking a button?

Greetings! As I work on developing an ASP.NET JavaScript application in SharePoint, I encounter a specific issue. My project consists of two pages: default.aspx and addnewitem.aspx. On the default.aspx page, there is a gridview with an edit button. Ideally ...

Jquery: Undefined Key/Value Declaration

I'm diving into the world of associative arrays for the first time and trying to access data using keys. While my array is being constructed successfully, I'm struggling to actually retrieve the data. As a result, the console.log statement at the ...

Image missing on payment stripe section

I successfully integrated the stripe checkout feature using node.js The value of product.photo is https://test.s3.amazonaws.com/2213131 const session = await stripe.checkout.sessions.create({ payment_method_types: ["card"], line_items: [{ name: p ...

Encountering a Lerna success error message: "No packages were detected containing the lifecycle script 'typecheck'."

I'm encountering an issue when trying to start lerna. The error message I receive is 'lerna success run No packages found with the lifecycle script 'typecheck'. How can I resolve this? Here's a snippet from my package.json file: { ...

Utilizing React forwardRef with a functional component

Looking at my code, I have defined an interface as follows: export interface INTERFACE1{ name?: string; label?: string; } Additionally, there is a function component implemented like this: export function FUNCTION1({ name, label }: INTERFACE1) { ...

issue with duplicating DOM element using function

My situation is unique from the one described in this post. The code mentioned there is not functioning as expected when clicking the clone button. I have even provided a video explanation of how that code works. Unfortunately, I haven't received any ...

Having trouble with Firebase continuously replacing old images with new ones whenever I upload them using JavaScript/jQuery

I am experiencing an issue with Firebase where it overrides my old pictures every time I upload a new picture. How can I configure it to save my old pictures as well? <!DOCTYPE html> <html> <head> <title>Firebase Storage< ...

Ensure that two div elements are always aligned, regardless of the circumstances

Is it possible to ensure that my two divs are always aligned, regardless of the content inside them? Sometimes the left div may contain images of varying sizes, while the right div may have text with different lengths. I initially tried setting a min-heigh ...

How to run a PHP script using JavaScript?

I am planning to execute a quick php script once users exit my website. Moreover, I am looking to transfer a variable from my javascript to php, but I am unsure how to integrate the php file and pass it a variable. Unfortunately, the php script is not runn ...

What is the best way to delete comments from the config.js file?

I'm looking for a way to remove comments from my config.js file, which is acting as a JSON file in my project. The config file has both single line comments like this: //comment goes here and multi-line comments like this: /* comments goes here */ ...

What is the best way to position two elements floated to the right in separate rows within a single row?

Having trouble with a web design issue. I am currently working on a website and trying to format an input field with a label so that the label sits on top of the input while floating to the right. However, I'm encountering difficulties as the elements ...

The jQuery script version 3.5.1 encountered an error at line 4055 where DataTables was unable to access the property "aDataSort" due to it

Hey there, I'm currently facing a small challenge while trying to incorporate Datatables within a bootstrap modal to display the results of a SQL Server query. The main requirement is to provide the option to export the data as an Excel file or in oth ...

Error: The function onSelect is not defined and cannot be executed by clicking on the HTML element with the id of "onclick" at line

I keep encountering this error message in my console: Uncaught ReferenceError: onSelect is not defined at HTMLAnchorElement.onclick (VM998 :14) This is the list data in my .html file: <ul class="nav navbar-nav"> <li& ...