Displaying SharePoint Data Into react-bootstrap-table-next

About
 
One common development scenario we as developers regularly implement is displaying SharePoint data into a tabular format with various functionality like sort, search, formatting etc. The most-used option was Bootstrap table and now with SPFx development, we can also show the list data on frontend webpart using react-bootstrap-table2.
 
Now react-bootstrap-table2 is available on NPM module, so it is also known as react-bootstrap-table-next.
 
In this article, we will see how to create a client Web Part, using SharePoint Framework and React JS that displays SharePoint List data into react-bootstrap-table. We will use PNP method to get list data.
 
First things first, let’s have a look into SharePoint List schema, from which we will pull the data. I have created a SharePoint list calling employee details with few dummy records.
 
 
Let’s start with our solution
 
Open node command prompt and create SPFx solution for web part using yeomen generator. Select React as a framework. Below is screen shot of my solution.
 
yo @microsoft/sharepoint
 
 
To verify if everything is okay with the solution, use gulp build command.
 
 
Add Pre-requisite
  1. To add react-bootstrap-table-next to solution, run the below command

    npm install react-bootstrap-table-next –save

  2. Add @pnp/sp to solution, using the below command

    npm install @pnp/sp

  3. Open the solution in VS code using command

    code .
Our solution folder for webpart will look like the below image:
 
 
Now let’s start with coding part,
 
Open the file DataIntoBsTable.tsx file and add below import statements
  1. //Import related to react-bootstrap-table-next    
  2. import BootstrapTable from 'react-bootstrap-table-next';    
  3. //Import from @pnp/sp    
  4. import { sp } from "@pnp/sp";    
  5. import "@pnp/sp/webs";    
  6. import "@pnp/sp/lists/web";    
  7. import "@pnp/sp/items/list";   
In Render function add the below code to load the css file:
  1. let cssURL = "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css";    
  2. SPComponentLoader.loadCss(cssURL);   
Create table schema
  1. const empTablecolumns = [     
  2.   {    
  3.       dataField: "EmployeeName",    
  4.       text: "Employee Name",    
  5.       headerStyle : {backgroundColor: '#81c784'},    
  6.       sort : true    
  7.   },    
  8.   {    
  9.       dataField: "MobileNumber",    
  10.       text: "Mobile Number"    
  11.   },    
  12.   {    
  13.       dataField: "Departments",    
  14.       text: "Departments"    
  15.   },    
  16.   {    
  17.       dataField: "JoiningDate",    
  18.       text: "Joining Date"    
  19.   }];  
Important points to take care of while deciding column Header:
  • dataField :It will be same like column name in list item response
  • text : It will be a name to show in the Table
  • headerStyle : to added any colour (in this I have added header color to employee name column)
  • sort : true (Ascending and Descending attributes).
There are various attributes like formatting, row based event and many more options available.
 
Code to get List Items and bind to table
 
Add a new interface to get list items
  1. export interface IShowEmployeeStates{    
  2.     employeeList :any[]    
  3.   }   
Add Function to get all the list Items
  1. public getEmployeeDetails = () =>{    
  2.   sp.web.lists.getByTitle("EmployeeDetails").items.getAll().    
  3.   then((results : any)=>{    
  4.     
  5.       this.setState({    
  6.         employeeList:results    
  7.       });    
  8.     
  9.   });    
  10. }  
After making all the changes, default class code looks like:
  1. export default class DataIntoBsTable extends React.Component<IDataIntoBsTableProps, IDataIntoBsTableStates> {    
  2. constructor(props: IDataIntoBsTableProps){    
  3.   super(props);    
  4.   this.state ={    
  5.     employeeList : []    
  6.   }    
  7. }    
  8. public getEmployeeDetails = () =>{    
  9.   sp.web.lists.getByTitle("EmployeeDetails").items.getAll().    
  10.   then((results : any)=>{    
  11.     
  12.       this.setState({    
  13.         employeeList:results    
  14.       });    
  15.     
  16.   });    
  17. }    
  18. public componentDidMount(){    
  19.   this.getEmployeeDetails();    
  20. }    
  21.   public render(): React.ReactElement<IDataIntoBsTableProps> {    
  22.     let cssURL = "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css";    
  23.     SPComponentLoader.loadCss(cssURL);    
  24.     return (    
  25.       <div className={ styles.dataIntoBsTable }>    
  26.         <div className={ styles.container }>    
  27.           <div className={ styles.row }>    
  28.             <div className={ styles.column }>    
  29.               <span className={ styles.title }>Welcome to SharePoint!</span>    
  30.               <p className={ styles.subTitle }>List items in react-bootstrap-table-next.</p>    
  31.             </div>    
  32.           </div>    
  33.           <BootstrapTable keyField='id' data={this.state.employeeList} columns={ empTablecolumns } headerClasses="header-class"/>    
  34.         </div>    
  35.       </div>    
  36.     );    
  37.   }    
  38. }  
Once done with the above changes run the gulp serve command and open the sharepoint site workbench. Once you add the webpart, it will show the list data into table.
 
 
Column Formatting
 
Next we want to format the employee name, making it bold and add some text color. To achieve that we need to add formatter to Employee Name column.
 
Now Employee Name column schema will look like below
  1. {    
  2.       dataField: "EmployeeName",    
  3.       text: "Employee Name",    
  4.       headerStyle : {backgroundColor: '#81c784'},    
  5.       sort :true,    
  6.       formatter: makeEmpColBold    
  7.   },  
Next add makeEmpColBold function as below and I have also added text color to the employee name.
  1. function makeEmpColBold(cell, row) {    
  2.   return (    
  3.     <span>    
  4.       <strong style={ { color: 'blue' } }> { cell }</strong>    
  5.     </span>    
  6.   );    
  7. }   
Now our web part will look like:
 
 
Adding Pagination to the table
 
First we need to install Paginator to our solution using below command.
 
npm install react-bootstrap-table2-paginator --save
 
 
Next import the below statement into the solution
 
import 'react-bootstrap-table2-paginator/dist/react-bootstrap-table2-paginator.min.css';
import paginationFactory from 'react-bootstrap-table2-paginator';
 
Add pagination option value in render method. Here I have sizePerPage as 3, we can get this value dynamically from webpart properties.
  1. const paginationOptions = {    
  2.   sizePerPage: 3,    
  3.         hideSizePerPage: true,    
  4.         hidePageListOnlyOnePage: true    
  5.       };    
In bootstrap table add the pagination option like below
  1. <BootstrapTable keyField='id'     
  2.           data={this.state.employeeList}    
  3.            columns={ empTablecolumns }     
  4.            pagination={paginationFactory(paginationOptions)}    
  5.            headerClasses="header-class"/>   
Now run the gulp serve and refresh the page. Now webpart will display 3 items per page,with pagination options.
 
 
Conclusion
 
So in this article we have seen loading SharePoint data into react-bootstrap-table-next, with a few options like sorting, formatting and pagination. Please share your feedback and any comments or queries.
 
I'm happy to try, learn and share with you.