Building collapsible table cell components using ReactJS and Bootstrap 4

For each table row, I need to implement a feature that allows users to click on it and view specific information related to that row. After doing some research, I came across this thread: Twitter Bootstrap Use collapse.js on table cells [Almost Done]

I managed to partially integrate this solution into my code, however, I encountered an issue where only one hidden row is displayed at the bottom of the table instead of under each individual row from the grapevine array.

Below is an excerpt of my code:

          <div className="card-block">
              <Table hover bordered>
                  <thead>
                  <tr className="table-heading">
                      <th className="table-header">Entity</th>
                      <th className="table-header">Category</th>
                      <th className="table-header">Source</th>
                      <th className="table-header">Risk Score</th>
                  </tr>
                  </thead>
                  <tbody>
                  {this.state.gvEntries.map(grapevine => (
                    <tr data-toggle="collapse" data-target="#demo1" className="accordion-toggle">
                        <td>{grapevine.entity}</td>
                        <td>{grapevine.category}</td>
                        <td>{grapevine.source}</td>
                        <td>{grapevine.rscore}</td>
                    </tr>
                  ))}
                  <tr >
                      <td colSpan="6" className="hiddenRow"><div className="accordian-body collapse" id="demo1"> Demo1 </div></td>
                  </tr>
                  </tbody>
              </Table>
            </div>

If anyone can provide assistance with this problem, I would greatly appreciate it. Thank you!

Answer №1

When it comes to the hidden row, consider moving the tr inside the map function and utilizing variables for the data-target attribute and id (such as the index).

Here's an example:

map((grapevine, idx) => (
  <tr data-toggle="collapse" data-target={"#demo"+idx} className="accordion-toggle">
  ...
  <tr>
    <td colSpan="6" className="hiddenRow"><div className="accordian-body collapse" id={"#demo"+idx}> Demo1 </div></td>
  </tr>
)

Answer №2

data.map(sale => {
            return (
              <tbody key={sale.id}>
                <tr id={sale.id} onClick={handleCollapse}>
                  <td>
                    <a className="no">{sale.id}</a>
                  </td>
                  <td>{date.utcToLocal(sale.createdOn)}</td>
                </tr>
                { toggleDetails && 
                 <tr>
                  <td>Order Details go here</td>
                </tr> }
              </tbody>
            );
          })}

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

Is it possible to specify a parameter when passing a callback function?

As part of my web application, I am passing a callback function as a parameter within different functions. In certain cases, I need the callback to behave differently. Is it possible to pass a parameter to this callback? someMethod(callback); otherMethod ...

How can I implement a feature in Django where clicking on a form will automatically clear the initial text within it?

One common feature seen on websites is the username form box that initially displays "username" as a placeholder text. Upon clicking on the box, the word "username" disappears to allow users to input their own username. Appreciate it! ...

My CSS horizontal drop-down menu is causing the sub-navigation items to overlap each other

I'm having an issue where my sub navigation menu items are stacking on top of each other instead of displaying properly. Here is the code snippet causing the problem: /* STYLING FOR NAVIGATION MENU */ .nav-bar { width: 100%; height: 80px; ...

VueJS: Unable to access the 'name' property as it is undefined"

I'm at a loss trying to figure out a solution for this issue. My current task involves editing a pub schedule using an edit form: pubs/UpdateProfile.vue <template> <confirm title="Edit Pub" ok="Save pub" :show="show" v-on:save="sa ...

Tallying up the characters in a redux form prior to finalizing the submission

Is there a way to add a character counter below an input box, similar to how Twitter does with their input box? https://i.sstatic.net/lIugm.png I am currently using React and Redux with ReduxForm and have attempted to follow this guide Overview: We’re ...

Ace Code Editor: Turn off highlighted line beneath cursor

I am currently utilizing the Ace editor and would like to remove the shadowed line where the cursor is located. Here's an example Even after experimenting with different themes provided by Ace Mode Creator, the shadowed line still persists. Any sug ...

If the width of the table is set to 100% in the CSS, the legend in the Flot chart will automatically shift to the

When the CSS for the table is set to { width:100%}, the Flot chart's legend moves to the left side. Is there any way to maintain the table { width:100%} while also preventing this shift, considering that the CSS is applied site-wide? Here is a jsfid ...

tips for creating curved text using CSS

While working on a webpage design from a PSD file, I noticed some text that is slightly curved. https://i.sstatic.net/ns3tx.jpg https://i.sstatic.net/5AlIa.jpg https://i.sstatic.net/s380a.jpg If you look at the three images above, you'll see that ...

Delaying event listeners in Angular.js by using setTimeout within a factory or service

In order to delay the first iteration of the broadcast until the controller's $watchCollection is ready, I attempted using setTimeout() but it did not work as expected. Even trying with $timeout yielded the same result. What could be causing this issu ...

Leveraging JQuery to retrieve the string value from an onclick() event

Curious if there's a more efficient approach to tackle this issue, decided to seek input from the SO community... There's a third-party web page over which I have no control in terms of how it's displayed, but they do allow me to integrate ...

What is the reason behind the lack of tab focus in Material UI React Stepper?

Is there a method to enable tab focus on the steps of the material-UI react stepper control? Visit this link for more information. ...

Steps for removing an item from an array in MongoDB

Is it possible to remove an object from the mainList array? _id:ObjectId("5fafc5aec2b84c301845c55a"), username:"test", mainList:[ { _id:ObjectId("5fafdf1a5b49510c789af0ae"), n ...

The alignment of Bootstrap input fields is off center

@import url( 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css' ); <div class="row"> <div class="col-xs-12 text-center btn-group" data-toggle="buttons"> <label class="btn btn-primary active"> ...

Bootstrap for Twitter: How to Customize Text in the Navigation Bar

As per the twitter bootstrap guidelines, I am supposed to enclose text in a <p> tag for correct leading and color. However, when I try to do this within the navbar element or any of its sub-levels, the text does not inherit any of the styling propert ...

Verification Select Button Element

I am working on a Rails project and have implemented radio_button_tag. How can I add a validation to ensure that an option is chosen before submitting the form? <% @votes.each do |vote| %> <%= radio_button_tag "choice", "#{vote.id}" %> < ...

Strange Phenomenon: Website Appears Enlarged When Accessed through Server

Similar Question: Why does my website appear smaller on a live server than when deployed locally? After reviewing the answer to the similar question linked above, I still found myself facing the same issue with no resolution. My problem is that the webpa ...

How can I create multiple divs that look alike?

I've taken on the challenge of developing our own interpretation of Conway's "Game of Life" for a project. To represent my 20x20 grid, I decided to create nested divs - the whole grid is one div, each row is a div, and every cell within that is a ...

Issue with Bootstrap hover and toggle collapse menu functionality in a Ruby on Rails application

I am currently following this tutorial and have reached this particular part. However, I'm facing an issue where the mobile hamburger menu doesn't display the menu when clicked on mobile devices. Additionally, the hover effect is not working for ...

Developing a Navigation Bar Menu using JSON data and the power of Lodash

Seeking advice to enhance my code for creating a navbar menu from JSON data. Any suggestions on how to avoid calling getItems multiple times? Appreciate any feedback, thank you! var _ = require('lodash'); let menuConfig = [ { ID: 1 ...

Is it possible to specify the input language for a textbox or textarea in a web form?

I am currently working on developing a website in Urdu language, with features similar to that of a blogging platform. To ensure that all posts are written only in Urdu, I am exploring different options. One idea I have is to use a Javascript code that ca ...