DEV Community

loading...
Cover image for How to create a beautiful tables using bootstrap and Contrast on React.
Devwares

How to create a beautiful tables using bootstrap and Contrast on React.

Speedwares
Originally published at devwares.com on ・5 min read

How to create different tables, editable tables in React using bootstrap and contrast.

Tables are component with basic tables features. They let you aggregate a huge amount of data and present it in a clear and orderly way.

React tables provide additional benefits like responsiveness, and the possibility to manipulate the styles of the tables. You can enhance your tables by adding buttons, checkboxes, panels, and many other additional elements. You can also use an advanced data tables options like sort, search or pagination.

Today, we’ll be creating a table in react using a react library knows as Contrast. Contrast also known as CDBReact is a react library which is an Elegant UI kit with full bootstrap support that has reusable components for building mobile-first, responsive websites, and web apps.

Prerequisites

The tables would be built using React, Bootstrap, and CDBReact. You don’t need to have any previous knowledge of CDBReact but the following are necessary:

  • JavaScript Knowledge
  • Basic React Knowledge
  • Basic Bootstrap Knowledge
  • JavaScript Knowledge
  • NPM installed

The table we will be creating will look like the image below

Alt Text

Setup

First Check that you have node installed. To do this, run the following command in your terminal.

node -v
Enter fullscreen mode Exit fullscreen mode

This should show you the current version of node you have installed on your machine.
If you don’t have node.js installed, download it here.
Installing node also installs npm on your PC but you can still confirm using npm-v. Now that we have node installed, we can start up our React project by going to the directory of our choice and running

npx create-react-app table-app
Enter fullscreen mode Exit fullscreen mode

I named the project table-app but you can use whatever name of your choice.

Install CDBReact

Now, we have to install CDBReact in our project
Run the following command to install CBDReact

Code:

npm install --save cdbreact
Enter fullscreen mode Exit fullscreen mode

Or using Yarn

Code:

yarn add cdbreact
Enter fullscreen mode Exit fullscreen mode

Note that we don’t need to install bootstrap or add it anywhere in our project as CDBReact does that for us upon installation.

Table

There are different types of table available for you in contrast. This tables enable you to present your work in a concise and simple way. We are going to create a one of this table known as responsive table. First we create a file named table.js. then we write this code on them.

Code:

import React from "react";
import { CDBTable, CDBTableHeader, CDBTableBody, CDBContainer } from "cdbreact";
Enter fullscreen mode Exit fullscreen mode

in the code above, we imported the table components to help us create different tables. These components include:

  • CDBTable is the table itself
  • CDBTableHeader is used to create the header for the table
  • CDBTableBody is used to create the body of the table
  • CDBContainer is houses the entire table components.

Code:

const Table = () => {
  return (
    <CDBContainer>
          <CDBTable responsive>
            <CDBTableHeader>
              <tr>
                <th>#</th>
                <th>First</th>
                <th>Second</th>
                <th>Third</th>
                <th>Fourth</th>
                <th>Fifth</th>
                <th>Sixth</th>
                <th>Seventh</th>
                <th>Last</th>
                <th>Handle</th>
              </tr>
            </CDBTableHeader>
            <CDBTableBody>
              <tr>
                <td>1</td>
                <td>Name</td>
                <td>Name</td>
                <td>Name</td>
                <td>Name</td>
                <td>Name</td>
                <td>Name</td>
                <td>Name</td>
                <td>Name</td>
                <td>@email</td>
              </tr>
              <tr>
                <td>2</td>
                <td>Name</td>
                <td>Name</td>
                <td>Name</td>
                <td>Name</td>
                <td>Name</td>
                <td>Name</td>
                <td>Name</td>
                <td>Name</td>
                <td>@email</td>
              </tr>
              <tr>
                <td>3</td>
                <td>Name</td>
                <td>Name</td>
                <td>Name</td>
                <td>Name</td>
                <td>Name</td>
                <td>Name</td>
                <td>Name</td>
                <td>Name</td>
                <td>@email</td>
              </tr>
            </CDBTableBody>
          </CDBTable>
    </CDBContainer>
  );
};

export default Table;
Enter fullscreen mode Exit fullscreen mode

In the code above, we created the table with the components we imported and included some styling to them.

Now, we are going to render the component into our app.js.

Code:

import './App.css';
import Table from './table';
import Reactdom from "react-dom";

function App() {
  return (
    <div className="App">
      <Table />
    </div>
  );
}

Reactdom.render(<App />, document.getElementById('root'));
Enter fullscreen mode Exit fullscreen mode

Our page will look like the image below

Alt Text

Editable Table

With React Editable Bootstrap Table, you can add and remove rows and change text and information within cells. In-place editing on your website - based on JavaScript - is now easier and quicker.

The editable table we are going to create will look like the image below

Alt Text

You need to download the pro version of contrast to use the editable table in contrast. We can start up our React project by going to the directory of our choice and running

npx create-react-app editable-table-app
Enter fullscreen mode Exit fullscreen mode

I named the project Editabletable-app but you can use whatever name of your choice.

Install CDBReact-pro

Install the cdbreact-pro package in your project (we recommend adding the file to the root of the project.) by running

npm install --save ./path-to-the-cdbreact-pro-tgz-file
Enter fullscreen mode Exit fullscreen mode

Or using Yarn

yarn add ./path-to-the-cdbreact-pro-tgz-file
Enter fullscreen mode Exit fullscreen mode

Note that we don’t need to install bootstrap or add it anywhere in our project as CDBReact does that for us upon installation.

Code:

import React from "react";
import { CDBEditableTable, CDBCard, CDBCardBody, CDBContainer } from "cdbreact";
Enter fullscreen mode Exit fullscreen mode

in the code above we imported the components we need for building editable table. These components include

  • CDBEditableTable.
  • CDBCard
  • CDBCardBody

Code:


const EditableTable = () => {
  const columns = ["Fullname", "Age", "Company Name", "City", "Country"];

  const data = [
    ["Guerra Cortez", 45, "Insectus", "San Francisco", "USA"],
    ["Elisa Gallagher", 31, "Portica", "London", "United Kingdom"],
    ["Aurelia Vega", 30, "Deepends", "Madrid", "Spain"],
    ["Guadalupe House", 26, "Isotronic", "Berlin", "Germany"]
  ];

  return (
    <CDBContainer>
        <CDBCard>
          <CDBCardBody>
            <CDBEditableTable striped bordered data={data} columns={columns}/>
          </CDBCardBody>
        </CDBCard>
    </CDBContainer>
  );
};

export default EditableTable;
Enter fullscreen mode Exit fullscreen mode

In the code above, the components were used to build a card body and editable table and some styles were added to the components to enable it to be more presentable.

The next step is to render the components into the app.js

Code:

import './App.css';
import EditableTable from './Editabletable';

function App() {
  return (
    <Router>
      <div className="App">
        <EditableTable />
      </div>
    </Router>
  );
}

export default App;
Enter fullscreen mode Exit fullscreen mode

The code above shows the editabletable components in the root app which allows the application to be presented in the web.

This is how the page will look like now

Alt Text

Conclusion

The editable table can be built easily into any number of your project and you can use some other features available in the contrast pro version to make your table to your style and preference.

Resources

CDBReact Tables Docs

Link to code on github

Get Contrast Pro

Create Stunning Websites and Web Apps

Building different custom components in react for your web apps or websites can get very stressful. Thats why we decided to build contrast. We have put together a UI kit with over 10000+ components, 5 admin dashboards and 23 additional different pages template for building almost any type of web app or webpage into a single product called Contrast Pro. Try contrast pro!

Contrast Design Boostrap

Contrast React Bootstrap PRO is a Multipurpose pro template, UI kit to build your next landing, admin, SAAS, prelaunch, etc project with a clean, well documented, well crafted template and UI components. Learn more about Contrast Pro

Discussion (0)