Author avatar

Gaurav Singhal

Create a Collapsible Table Row with React-Bootstrap

Gaurav Singhal

  • Nov 10, 2020
  • 9 Min read
  • 270 Views
  • Nov 10, 2020
  • 9 Min read
  • 270 Views
Web Development
Front End Web Development
Client-side Frameworks
React

Introduction

Tables and forms are essential elements used in almost all web apps. Every JavaScript-based framework or library uses different UI frameworks to design their forms and tables to render the record and user interactivity through forms.

Collapsible rows are a handy option when you want to show child properties or records to the end user, and they also can be useful to show statistics such as charts and reports. In this guide, you will learn how to implement a collapsible row using React Bootstrap and other third-party libraries.

Collapsible Row Using a React Bootstrap Custom Implementation

A popular UI framework for React is react-bootstrap, which provides various Bootstrap-backed elements and components for React apps. react-bootstrap contains the set of components and its API to configure components in your app, and it can also change its behavior or appearance based on business requirements.

To use react-bootstrap, install some libraries, as shown below.

1
2
npm install react-bootstrap
npm install bootstrap
shell

After installing both the above libraries, the next step is to import CSS to your app's parent component, such as App.js.

1
import "bootstrap/dist/css/bootstrap.min.css";
jsx

Now import Table from the library react-bootstrap, and also import collapse.js,as shown below.

1
2
3
4
5
6
7
import React, { Component } from "react";
// Table from react-bootstrap
import { Table } from "react-bootstrap";
// Bootstrap CSS
import "bootstrap/dist/css/bootstrap.min.css";
// To make rows collapsible
import "bootstrap/js/src/collapse.js";
jsx

The next step is to integrate the table and implement expandable rows. For a quick demonstration, create the table, and with each table row, create one child row to expand.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
render() {
    return (
        <>
            <Table striped bordered hover>
                <thead>
                    <tr>
                        <th>#</th>
                        <th>Name</th>
                        <th>Email</th>
                    </tr>
                </thead>
                <tbody>
                    <tr
                        data-toggle="collapse"
                        data-target=".multi-collapse1"
                        aria-controls="multiCollapseExample1"
                    >
                        <td>1</td>
                        <td>TEST 123</td>
                        <td>[email protected]</td>
                    </tr>
                    <tr class="collapse multi-collapse1" id="multiCollapseExample1">
                        <td>Child col 1</td>
                        <td>Child col 2</td>
                        <td>Child col 3</td>
                    </tr>
                    <tr
                        data-toggle="collapse"
                        data-target=".multi-collapse2"
                        aria-controls="multiCollapseExample2"
                    >
                        <td>2</td>
                        <td>TEST 456</td>
                        <td>[email protected]</td>
                    </tr>
                    <tr class="collapse multi-collapse2" id="multiCollapseExample2">
                        <td>Child col 1</td>
                        <td>Child col 2</td>
                        <td>Child col 3</td>
                    </tr>
                </tbody>
            </Table>
        </>
    );
}
jsx

In the above table, a few Bootstrap features are developed. Three different properties get attached along with each table row.

1
2
3
data-toggle="collapse"
data-target=".multi-collapse1"
aria-controls="multiCollapseExample1"
jsx

All the above properties define the collapsible content target and allow the row to be toggled using the property called data-toggle.

Once any of the table rows are clicked, their respective child rows should get expanded. However, you need to define a unique identification so that the specific children get expanded.

1
2
3
4
5
<tr class="collapse multi-collapse1" id="multiCollapseExample1">
    <td>Child col 1</td>
    <td>Child col 2</td>
    <td>Child col 3</td>
</tr>
jsx

In the above example, there are two classes defined with the class props, Collapse and Multi-collapse1, which means that once a user clicks the respective row, it is identified by the id props, and individual CSS is applied to either hide or show the error.

To implement completely working expandable rows using React Bootstrap, follow this code demonstration.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import React, { Component } from "react";
// Table from react-bootstrap
import { Table } from "react-bootstrap";
// Bootstrap CSS
import "bootstrap/dist/css/bootstrap.min.css";
// To make rows collapsible
import "bootstrap/js/src/collapse.js";

export class Example1 extends Component {
    render() {
        return (
            <>
                <Table striped bordered hover>
                    <thead>
                        <tr>
                            <th>#</th>
                            <th>Name</th>
                            <th>Email</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr
                            data-toggle="collapse"
                            data-target=".multi-collapse1"
                            aria-controls="multiCollapseExample1"
                        >
                            <td>1</td>
                            <td>TEST 123</td>
                            <td>[email protected]</td>
                        </tr>
                        <tr class="collapse multi-collapse1" id="multiCollapseExample1">
                            <td>Child col 1</td>
                            <td>Child col 2</td>
                            <td>Child col 3</td>
                        </tr>
                        <tr
                            data-toggle="collapse"
                            data-target=".multi-collapse2"
                            aria-controls="multiCollapseExample2"
                        >
                            <td>2</td>
                            <td>TEST 456</td>
                            <td>[email protected]</td>
                        </tr>
                        <tr class="collapse multi-collapse2" id="multiCollapseExample2">
                            <td>Child col 1</td>
                            <td>Child col 2</td>
                            <td>Child col 3</td>
                        </tr>
                    </tbody>
                </Table>
            </>
        );
    }
}

export default Example1;
jsx

This complete example contains a react-bootstrap table, and along with the table row, collapsible properties are defined. And respective child elements have unique identification to expand the specific rows.

Once you run the above example, the initial output will look like this.

Table without expanded records

Now click on the first row, and the child rows will be expanded, as shown below.

Expanded first table row

If you click on the second row to expand its child rows, it will look like this.

Second expanded table row

Using the custom table implementation will give you the above output to expand the rows. And to avoid the expanded feature's misbehavior, you can make it more dynamic and manageable.

Using Other Third-Party Libraries

The table's custom implementation may help to expand the row. But it comes with some limitations, including lack of time, resources, and efforts.

However, you can use other third-party libraries to make table rows expand with a more advanced user interface. There are other libraries you can use to get started with expanding a table:

  • react-bootstrap-table
  • react-bootstrap-table2
  • react-collapsing-table
  • Material-ui collapsible table

Conclusion

Tables are the building blocks of any web or mobile app used to show information; hence, they should be well configured to tackle any development challenges.

Libraries like react-bootstrap and react-bootstrap-table are great choices for using tables and making rows expandable. I hope you will be able to implement a collapsible feature after going through this guide.

0