2

i'm new in reactjs and react bootstrap table. i want add react-loading in my react component before data show. because data show so long. but doesn't work.

this is my code

import React, { Component } from 'react';
import { BootstrapTable, TableHeaderColumn } from 'react-bootstrap-table';
import PageHeader from 'react-bootstrap/lib/PageHeader';
import database from './database';
import root from 'window-or-global';
import Loading from 'react-loading';

class User extends Component {
    constructor(props) {
    super(props);
    this.state = {
      text:'',
      products: []
    };

   this.userRef = database.ref('users');
  }

  componentDidMount() {
    this.userRef.on('value', this.gotData, this.errData); 
  }


  gotData = (data) => {
    let newProducts = []
    const userdata = data.val();
    const keys = Object.keys(userdata);
    for (let i = 0; i < keys.length; i++) {
      const k = keys[i];
      newProducts.push({
        name: userdata[k].nama, address: userdata[k].address, email: userdata[k].email
      });
    }
    this.setState({products : newProducts});
  }

  errData = (err) => {
   console.log(err);
 }

  handleClick = (rowKey) => {
    alert(this.refs.table.getPageByRowKey(rowKey));
  }

    render() {
    return (
      <div>
      <Loading type ='spinning-bubbles' color='#e3e3e3' />
          <div className="col-lg-12">
          <PageHeader>Members</PageHeader>
       </div>
        <BootstrapTable  
              ref='table'
            data={ this.state.products }
            pagination={ true }
            search={ true }>
          <TableHeaderColumn dataField='name' isKey={true} dataSort={true}>Name</TableHeaderColumn>
            <TableHeaderColumn dataField='address' dataSort={true}>Address</TableHeaderColumn>
            <TableHeaderColumn dataField='email'>Email</TableHeaderColumn>
          </BootstrapTable>
      </div>
    );
    }
}

export default User;

Best wishes! Wish for replying.

1 Answer 1

9

For that, first maintain a bool in state variable that will track whether data fetched or not, initial value of that bool will be false.

constructor(){
     super();
     this.state = {
        isDataFetched: false,
     }
}

Now there a property of React-Bootstrap-Table, option that is used to set the default values and customise the noDataText, you can use that, like this:

<BootstrapTable 
     ....
     options={tableOtions}>
</BootstrapTable>

Now define this tableOtions object and edit the noDataText property, like this:

let tableOtions = {
    noDataText: this._setTableOption(),
};

If data is fetched successfully then return some text, that will be displayed if data will be empty, otherwise return a loader:

_setTableOption(){ 
     if(this.state.isDataFetched){
           return "No expenses found";
      }else{
           return(
               <RefreshIndicator size={40} left={0} top={0} status="loading" style={{position: 'relative', margin: '0px auto'}}/>
           );
      }
}

Note: I used a Material-UI RefreshIndicator, you can replace that by any other loader also.

Reference of option object in React Bootstrap Table: http://allenfang.github.io/react-bootstrap-table/docs.html#noDataText

Update: Use this code, and replace the RefreshIndicator by Loader that you want to show:

import React, { Component } from 'react';
import { BootstrapTable, TableHeaderColumn } from 'react-bootstrap-table';
import PageHeader from 'react-bootstrap/lib/PageHeader';
import database from './database';
import root from 'window-or-global';
import Loading from 'react-loading';

class User extends Component {
    constructor(props) {
    super(props);
    this.state = {
      text:'',
      products: [],
      isDataFetched: false,
    };
    this.userRef = database.ref('users');
  }

  componentDidMount() {
    this.userRef.on('value', this.gotData, this.errData); 
  }


  gotData = (data) => {
    let newProducts = []
    const userdata = data.val();
    const keys = Object.keys(userdata);
    for (let i = 0; i < keys.length; i++) {
      const k = keys[i];
      newProducts.push({
        name: userdata[k].nama, address: userdata[k].address, email: userdata[k].email
      });
    }
    this.setState({products : newProducts, isDataFetched: true});
  }

  errData = (err) => {
    console.log(err);
  }

  handleClick = (rowKey) => {
    alert(this.refs.table.getPageByRowKey(rowKey));
  }

  _setTableOption(){ 
    if(this.state.isDataFetched){
      return "No expenses found";
    }else{
      return(
        <RefreshIndicator size={40} left={0} top={0} status="loading" style={{position: 'relative', margin: '0px auto'}}/>
      );
    }
  }

  render() {

    let tableOtions = {
        noDataText: this._setTableOption(),
    };

    return (
      <div>
        <Loading type ='spinning-bubbles' color='#e3e3e3' />
        <div className="col-lg-12">
          <PageHeader>Members</PageHeader>
        </div>
        <BootstrapTable  
          ref='table'
          data={ this.state.products }
          pagination={ true }
          search={ true }
          options={tableOtions}>
        >
          <TableHeaderColumn dataField='name' isKey={true} dataSort={true}>Name</TableHeaderColumn>
          <TableHeaderColumn dataField='address' dataSort={true}>Address</TableHeaderColumn>
          <TableHeaderColumn dataField='email'>Email</TableHeaderColumn>
        </BootstrapTable>
      </div>
    );
  }
}

export default User;
Sign up to request clarification or add additional context in comments.

2 Comments

please tell me where the code must be placed..:-( @Mayank Shukla
check the updated answer, use the same code, just replace the loader part, it will work :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.