如何在 react bootstrap table 中添加 react-loading

how add react-loading in react bootstrap table

我是 reactjs 的新手并且反应 bootstrap table。我想在数据显示之前在我的反应组件中添加反应加载。因为数据显示这么久。但不起作用。

这是我的代码

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;

祝您好运!求回复。

为此,首先在 state 变量中维护一个 bool 来跟踪是否已获取数据,该 bool 的初始值为 false

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

现在有React-Bootstrap-Table的属性,option用来设置默认值和自定义noDataText,你可以像这样使用它:

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

现在定义此 tableOtions object 并编辑 noDataText 属性,如下所示:

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

如果数据获取成功然后 return 一些文本,如果数据为空将显示,否则 return 加载器:

_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'}}/>
           );
      }
}

注意:我用了一个Material-UI RefreshIndicator,你也可以用任何其他加载器替换它。

React Bootstrap Tableoption 对象的引用http://allenfang.github.io/react-bootstrap-table/docs.html#noDataText

更新: 使用此代码,并将 RefreshIndicator 替换为要显示的 Loader

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;