2

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

这是我的代码

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;

最良好的祝愿!希望回复。

4

1 回答 1

9

为此,首先维护一个boolin变量,该变量将state跟踪是否获取数据,其初始值为.boolfalse

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

现在有一个 的属性React-Bootstrap-Tableoption用于设置默认值和自定义noDataText,您可以使用它,如下所示:

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

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

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

如果成功获取数据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,您也可以用任何其他加载器替换它。

option对象参考httpReact Bootstrap Table : //allenfang.github.io/react-bootstrap-table/docs.html#noDataText

更新:使用此代码,并替换RefreshIndicatorLoader要显示的:

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;
于 2017-04-11T07:16:51.617 回答