Introduction

In this article we will learn how to use the React Bootstrap Table in React applications. I will also explain how we can implement paging, searching, and sorting in this Table.
Prerequisites
  • We should have the basic knowledge of React.js and Web API.
  • Visual Studio and Visual Studio Code IDE should be installed on your system.
  • SQL Server Management Studio
  • Basic knowledge of Bootstrap and HTML
Implementation Steps
  • Create a Database and Table
  • Create Asp.net Web API Project
  • Create React App
  • Install React-bootstrap-table2
  • Implement Sorting
  • Implement Searching
  • Implement Custom Pagination
  • Install Bootstrap
  • Install Axios

Create a table in the database

Open SQL Server Management Studio, create a database named "Employee", and in this database, create a table. Give that table a name like "Employee".
  1. CREATE TABLE [dbo].[Employee](
  2. [Id] [int] IDENTITY(1,1) NOT NULL,
  3. [Name] [varchar](50) NULL,
  4. [Age] [int] NULL,
  5. [Address] [varchar](50) NULL,
  6. [City] [varchar](50) NULL,
  7. [ContactNum] [varchar](50) NULL,
  8. [Salary] [decimal](18, 0) NULL,
  9. [Department] [varchar](50) NULL,
  10. CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED
  11. (
  12. [Id] ASC
  13. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  14. ) ON [PRIMARY]
  15. GO
Now add some demo data in this table.

Create a new Web API project

Open Visual Studio and create a new project.
React Bootstrap Table With Searching And Custom Pagination
Change the name to MatUITable.
React Bootstrap Table With Searching And Custom Pagination
Choose the template as Web API.
React Bootstrap Table With Searching And Custom Pagination
Right-click the Models folder from Solution Explorer and go to Add >> New Item >> data.
React Bootstrap Table With Searching And Custom Pagination
Click on the "ADO.NET Entity Data Model" option and click "Add".
React Bootstrap Table With Searching And Custom Pagination
Select EF Designer from the database and click the "Next" button.
React Bootstrap Table With Searching And Custom Pagination
Add the connection properties and select database name on the next page and click OK.
React Bootstrap Table With Searching And Custom Pagination
Check the "Table" checkbox. The internal options will be selected by default. Now, click the "Finish" button.
React Bootstrap Table With Searching And Custom Pagination
Now, our data model is successfully created.
Right-click on the Controllers folder and add a new controller. Name it as "Employee controller" and add the following namespace in the Employee controller.
using MatUITable.Models;
Now add a method to fetch data from database.
  1. [HttpGet]
  2. [Route("employee")]
  3. public object Getrecord()
  4. {
  5. var emp = DB.Employees.ToList();
  6. return emp;
  7. }
Complete Employee controller code
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Http;
  6. using System.Web.Http;
  7. using MatUITable.Models;
  8. namespace MatUITable.Controllers
  9. {
  10. [RoutePrefix("Api/Emp")]
  11. public class EmployeeController : ApiController
  12. {
  13. EmployeeEntities DB = new EmployeeEntities();
  14. [HttpGet]
  15. [Route("employee")]
  16. public object Getrecord()
  17. {
  18. var emp = DB.Employees.ToList();
  19. return emp;
  20. }
  21. }
  22. }
Now, let's enable CORS. Go to Tools, open NuGet Package Manager, search for CORS, and install the "Microsoft.Asp.Net.WebApi.Cors" package. Open Webapiconfig.cs and add the following lines.
  1. EnableCorsAttribute cors = new EnableCorsAttribute("*", "*", "*");
  2. config.EnableCors(cors);

Create ReactJS project

Now let's first create a React application with the following command.
  1. npx create-react-app matform
Install bootstrap by using following command
  1. npm install --save bootstrap
Now, open the index.js file and add Bootstrap reference.
  1. import 'bootstrap/dist/css/bootstrap.min.css';
Now Install the Axios library by using the following command. Learn more about Axios
  1. npm install --save axios
Install react-bootstrap-table2.
Install react bootstrap table by using following command,
  1. npm install react-bootstrap-table-next --save
Now, right click on "src" folder and add a new component named 'Bootstraptab.js'.
Now open Bootstraptab.js component and import required reference. Add the following code in this component.
  1. import React, { Component } from 'react'
  2. import BootstrapTable from 'react-bootstrap-table-next';
  3. import axios from 'axios';
  4. export class Bootstraptab extends Component {
  5. state = {
  6. employee: [],
  7. columns: [{
  8. dataField: 'Id',
  9. text: 'Id'
  10. },
  11. {
  12. dataField: 'Name',
  13. text: 'Name',
  14. sort:true
  15. }, {
  16. dataField: 'Age',
  17. text: 'Age',
  18. sort: true
  19. },
  20. {
  21. dataField: 'Address',
  22. text: 'Address',
  23. sort: true
  24. },
  25. {
  26. dataField: 'City',
  27. text: 'City',
  28. sort: true
  29. },
  30. {
  31. dataField: 'ContactNum',
  32. text: 'ContactNum',
  33. sort: true
  34. },
  35. {
  36. dataField: 'Salary',
  37. text: 'Salary',
  38. sort: true
  39. },
  40. {
  41. dataField: 'Department',
  42. text: 'Department',
  43. sort: true
  44. }]
  45. }
  46. componentDidMount() {
  47. axios.get('http://localhost:51760/Api/Emp/employee').then(response => {
  48. console.log(response.data);
  49. this.setState({
  50. employee: response.data
  51. });
  52. });
  53. }
  54. render() {
  55. return (
  56. <div className="container">
  57. <div class="row" className="hdr">
  58. <div class="col-sm-12 btn btn-info">
  59. React Bootstrap Table with Searching and Custom Pagination
  60. </div>
  61. </div>
  62. <div style={{ marginTop: 20 }}>
  63. <BootstrapTable
  64. striped
  65. hover
  66. keyField='id'
  67. data={ this.state.employee }
  68. columns={ this.state.columns } />
  69. </div>
  70. </div>
  71. )
  72. }
  73. }
  74. export default Bootstraptab
Run the project by using 'npm start' and check the result,
React Bootstrap Table With Searching And Custom Pagination
React Bootstrap Table With Searching And Custom Pagination
Click on the button to check sorting in table
Implement Searching
Install the following library to add searching in this table.
  1. npm install react-bootstrap-table2-filter --save
Now add the following code in this component.
  1. import React, { Component } from 'react'
  2. import BootstrapTable from 'react-bootstrap-table-next';
  3. import axios from 'axios';
  4. import filterFactory, { textFilter } from 'react-bootstrap-table2-filter';
  5. export class Bootstraptab extends Component {
  6. state = {
  7. employee: [],
  8. columns: [{
  9. dataField: 'Id',
  10. text: 'Id'
  11. },
  12. {
  13. dataField: 'Name',
  14. text: 'Name',
  15. filter: textFilter()
  16. }, {
  17. dataField: 'Age',
  18. text: 'Age',
  19. sort: true
  20. },
  21. {
  22. dataField: 'Address',
  23. text: 'Address',
  24. sort: true
  25. },
  26. {
  27. dataField: 'City',
  28. text: 'City',
  29. sort: true
  30. },
  31. {
  32. dataField: 'ContactNum',
  33. text: 'ContactNum',
  34. sort: true
  35. },
  36. {
  37. dataField: 'Salary',
  38. text: 'Salary',
  39. sort: true
  40. },
  41. {
  42. dataField: 'Department',
  43. text: 'Department',
  44. sort: true
  45. }]
  46. }
  47. componentDidMount() {
  48. axios.get('http://localhost:51760/Api/Emp/employee').then(response => {
  49. console.log(response.data);
  50. this.setState({
  51. employee: response.data
  52. });
  53. });
  54. }
  55. render() {
  56. return (
  57. <div className="container">
  58. <div class="row" className="hdr">
  59. <div class="col-sm-12 btn btn-info">
  60. React Bootstrap Table with Searching and Custom Pagination
  61. </div>
  62. </div>
  63. <div style={{ marginTop: 20 }}>
  64. <BootstrapTable
  65. striped
  66. hover
  67. keyField='id'
  68. data={ this.state.employee }
  69. columns={ this.state.columns }
  70. filter={ filterFactory() } />
  71. </div>
  72. </div>
  73. )
  74. }
  75. }
  76. export default Bootstraptab
Run the project by using 'npm start' and check the result,
React Bootstrap Table With Searching And Custom Pagination
React Bootstrap Table With Searching And Custom Pagination
Implement Pagination
Install the following library to add pagination in this table.
  1. npm install react-bootstrap-table2-paginator --save
Now add the following code in this component.
  1. import React, { Component } from 'react'
  2. import BootstrapTable from 'react-bootstrap-table-next';
  3. import axios from 'axios';
  4. import paginationFactory from 'react-bootstrap-table2-paginator';
  5. export class Bootstraptab extends Component {
  6. state = {
  7. employee: [],
  8. columns: [{
  9. dataField: 'Id',
  10. text: 'Id'
  11. },
  12. {
  13. dataField: 'Name',
  14. text: 'Name',
  15. }, {
  16. dataField: 'Age',
  17. text: 'Age',
  18. sort: true
  19. },
  20. {
  21. dataField: 'Address',
  22. text: 'Address',
  23. sort: true
  24. },
  25. {
  26. dataField: 'City',
  27. text: 'City',
  28. sort: true
  29. },
  30. {
  31. dataField: 'ContactNum',
  32. text: 'ContactNum',
  33. sort: true
  34. },
  35. {
  36. dataField: 'Salary',
  37. text: 'Salary',
  38. sort: true
  39. },
  40. {
  41. dataField: 'Department',
  42. text: 'Department',
  43. sort: true
  44. }]
  45. }
  46. componentDidMount() {
  47. axios.get('http://localhost:51760/Api/Emp/employee').then(response => {
  48. console.log(response.data);
  49. this.setState({
  50. employee: response.data
  51. });
  52. });
  53. }
  54. render() {
  55. return (
  56. <div className="container">
  57. <div class="row" className="hdr">
  58. <div class="col-sm-12 btn btn-info">
  59. React Bootstrap Table with Searching and Custom Pagination
  60. </div>
  61. </div>
  62. <div style={{ marginTop: 20 }}>
  63. <BootstrapTable
  64. striped
  65. hover
  66. keyField='id'
  67. data={ this.state.employee }
  68. columns={ this.state.columns }
  69. pagination={ paginationFactory() } />
  70. </div>
  71. </div>
  72. )
  73. }
  74. }
  75. export default Bootstraptab
Run the project by using 'npm start' and check the result
React Bootstrap Table With Searching And Custom Pagination
By default it shows 10 records per page, so let's create a function to add custom page size. Add the following code in this component and check.
  1. import React, { Component } from 'react'
  2. import BootstrapTable from 'react-bootstrap-table-next';
  3. import axios from 'axios';
  4. import paginationFactory from 'react-bootstrap-table2-paginator';
  5. export class Bootstraptab extends Component {
  6. state = {
  7. employee: [],
  8. columns: [{
  9. dataField: 'Id',
  10. text: 'Id'
  11. },
  12. {
  13. dataField: 'Name',
  14. text: 'Name',
  15. }, {
  16. dataField: 'Age',
  17. text: 'Age',
  18. sort: true
  19. },
  20. {
  21. dataField: 'Address',
  22. text: 'Address',
  23. sort: true
  24. },
  25. {
  26. dataField: 'City',
  27. text: 'City',
  28. sort: true
  29. },
  30. {
  31. dataField: 'ContactNum',
  32. text: 'ContactNum',
  33. sort: true
  34. },
  35. {
  36. dataField: 'Salary',
  37. text: 'Salary',
  38. sort: true
  39. },
  40. {
  41. dataField: 'Department',
  42. text: 'Department',
  43. sort: true
  44. }]
  45. }
  46. componentDidMount() {
  47. axios.get('http://localhost:51760/Api/Emp/employee').then(response => {
  48. console.log(response.data);
  49. this.setState({
  50. employee: response.data
  51. });
  52. });
  53. }
  54. render() {
  55. const options = {
  56. page: 2,
  57. sizePerPageList: [ {
  58. text: '5', value: 5
  59. }, {
  60. text: '10', value: 10
  61. }, {
  62. text: 'All', value: this.state.employee.length
  63. } ],
  64. sizePerPage: 5,
  65. pageStartIndex: 0,
  66. paginationSize: 3,
  67. prePage: 'Prev',
  68. nextPage: 'Next',
  69. firstPage: 'First',
  70. lastPage: 'Last',
  71. };
  72. return (
  73. <div className="container">
  74. <div class="row" className="hdr">
  75. <div class="col-sm-12 btn btn-info">
  76. React Bootstrap Table with Searching and Custom Pagination
  77. </div>
  78. </div>
  79. <div style={{ marginTop: 20 }}>
  80. <BootstrapTable
  81. striped
  82. hover
  83. keyField='id'
  84. data={ this.state.employee }
  85. columns={ this.state.columns }
  86. pagination={ paginationFactory(options) } />
  87. </div>
  88. </div>
  89. )
  90. }
  91. }
  92. export default Bootstraptab
Run the project by using 'npm start' and check the result,
React Bootstrap Table With Searching And Custom Pagination
Now create a new component Bootstraptab1.js and add the following code in this component
  1. import React, { Component } from 'react'
  2. import BootstrapTable from 'react-bootstrap-table-next';
  3. import axios from 'axios';
  4. import filterFactory, { textFilter } from 'react-bootstrap-table2-filter';
  5. import paginationFactory from 'react-bootstrap-table2-paginator';
  6. export class Bootstraptab1 extends Component {
  7. state = {
  8. products: [],
  9. columns: [{
  10. dataField: 'Id',
  11. text: 'Id'
  12. },
  13. {
  14. dataField: 'Name',
  15. text: 'Name',
  16. filter: textFilter()
  17. }, {
  18. dataField: 'Age',
  19. text: 'Age',
  20. sort: true
  21. },
  22. {
  23. dataField: 'Address',
  24. text: 'Address',
  25. sort: true
  26. },
  27. {
  28. dataField: 'City',
  29. text: 'City',
  30. sort: true
  31. },
  32. {
  33. dataField: 'ContactNum',
  34. text: 'ContactNum',
  35. sort: true
  36. },
  37. {
  38. dataField: 'Salary',
  39. text: 'Salary',
  40. sort: true
  41. },
  42. {
  43. dataField: 'Department',
  44. text: 'Department',
  45. sort: true
  46. }]
  47. }
  48. componentDidMount() {
  49. axios.get('http://localhost:51760/Api/Emp/employee').then(response => {
  50. console.log(response.data);
  51. this.setState({
  52. products: response.data
  53. });
  54. });
  55. }
  56. render() {
  57. const options = {
  58. page: 2,
  59. sizePerPageList: [ {
  60. text: '5', value: 5
  61. }, {
  62. text: '10', value: 10
  63. }, {
  64. text: 'All', value: this.state.products.length
  65. } ],
  66. sizePerPage: 5,
  67. pageStartIndex: 0,
  68. paginationSize: 3,
  69. prePage: 'Prev',
  70. nextPage: 'Next',
  71. firstPage: 'First',
  72. lastPage: 'Last',
  73. paginationPosition: 'top'
  74. };
  75. return (
  76. <div className="container">
  77. <div class="row" className="hdr">
  78. <div class="col-sm-12 btn btn-info">
  79. React Bootstrap Table with Searching and Custom Pagination
  80. </div>
  81. </div>
  82. <div className="container" style={{ marginTop: 50 }}>
  83. <BootstrapTable
  84. striped
  85. hover
  86. keyField='id'
  87. data={ this.state.products }
  88. columns={ this.state.columns }
  89. filter={ filterFactory() }
  90. pagination={ paginationFactory(options) }/>
  91. </div>
  92. </div>
  93. )
  94. }
  95. }
  96. export default Bootstraptab1
Now open app.js file and add the following code,
  1. import React from 'react';
  2. import logo from './logo.svg';
  3. import './App.css';
  4. import Bootstraptab1 from './Bootstraptab1';
  5. function App() {
  6. return (
  7. <div className="App">
  8. <Bootstraptab1/>
  9. </div>
  10. );
  11. }
  12. export default App;
Run the project by using 'npm start' and check the result,
React Bootstrap Table With Searching And Custom Pagination

Summary

In this article, we learned how we add React Bootstrap Table and show data in that table using Web API in ReactJS applications. We also Llarned how to implement sorting, searching and pagination in the table