2017-04-11 76 views
1

我是reactjs中的新成員,並且反應引導表。我希望在數據顯示之前在我的反應組件中添加反應加載。因爲數據顯示這麼久。但不起作用。如何在反應引導表中添加反應加載

這是我的代碼

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

爲此,第一保持boolstate變量,將跟蹤是否取出或不數據,即bool的初始值將是false

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

現在有用來設置默認值和定製noDataTextReact-Bootstrap-Tableoption的屬性,你可以使用,像這樣:

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

現在定義這個tableOtionsobject和編輯noDataText屬性如下:

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

如果data是fet高等教育委員會成功則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對象React Bootstrap Tablehttp://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; 
+0

請告訴我在哪裏代碼必須放置.. :-(@Mayank Shukla – Queen

+0

檢查更新的答案,使用相同的代碼,只需更換加載程序部分,它將工作:) –

+0

非常感謝.. :-) @Mayank Shukla – Queen