如何在reactjs

时间:2017-05-29 08:38:48

标签: javascript jquery ajax reactjs

我是reactjs的新手,我很困惑如何制作无限卷轴或“加载更多”功能。

所以这是我的componentDidMount

 componentDidMount() {
  $.ajax({
  url:'http://localhost:5118/api/employeedetails/getemployeedetails/',
  success:(data)=>{

    this.setState({
      jsonReturnedValue:data , isLoading: false
    })
  }
})

}

这是我加载表格的地方

renderItem(d, i) {
  return <tr key={i} >
    <td> {d.Employee_ID} </td>
    <td>{d.Employee_Name}</td>
    <td>{d.Address }</td> 
    <td><center><button className ="btn btn-info" onClick={this.handleOnclick.bind(this,  d.Employee_ID, d.Employee_Name, d.Address , d.Department , i)}   data-toggle="modal" data-target="#UpdateEmployee">Edit</button></center></td>
    <td><center><button className ='btn btn-danger'  onClick={this.handleOnclick2.bind (this,d.Employee_ID,d.Employee_Name,i)} data-toggle="modal" data-target="#DeleteEmployee"> Delete</button></center></td>
    </tr>
}

render() {
      if(this.state.isLoading) {
           return <span className="Loader">
                  <h1>
              <span>Loading</span>
              <span className="Loader-ellipsis" >
                <span className="Loader-ellipsisDot">.</span>
                <span className="Loader-ellipsisDot">.</span>
                <span className="Loader-ellipsisDot">.</span>
              </span>
            </h1>
        </span>
     }

    let {jsonReturnedValue} = this.state;
    const isEnabled = this.canBeSubmitted();


  return(
    <div>
    <div>

        <div className="container">   
          <h1> Listof Employees </h1>
            <button className ='btn btn-warning right ' data-toggle="modal" data-target="#AddEmployee"> Add an Employee</button>
             <table className= "table table-bordered" id="result"> 
                <tbody>
                 <tr>
                      <th>ID</th>
                      <th>Name</th>
                      <th>Address</th>
                      <th>Update</th>
                      <th>Delete</th>
                 </tr>
                   {/*  {this.state.tableLoading? <LoadingComponent/>: this.renderItem()}
        */} 
                    {jsonReturnedValue.map((d,i) => this.renderItem(d,i))} 

        </tbody>

            </table>
          </div>

2 个答案:

答案 0 :(得分:1)

    componentDidMount() 
  {
    let _this = this;
    $('#notificationMainDiv').on('scroll', function() {
      if($(this).scrollTop() + $(this).innerHeight() >= $(this [0].scrollHeight) 
      {
        if(_this.state.loadMore)
        {
          _this.retrieveNotifications();
        }
      }
    })
  }

到达div的底部后,将调用 retrieveMoreNotifications()。 当没有更多数据可检索时, loadMore 将设置为false。

答案 1 :(得分:0)

首先需要绑定onScroll eventListener,您可以在componentDidMount()

中执行此操作
componentDidMount() {
 .
 .
 window.addEventListener('scroll', this.onScroll);
}

然后,您需要在组件中定义onScroll方法,并检查用户是否已滚动到页面底部:

onScroll = () => {
  // write your code here to check if user has scrolled to the bottom of page
 if(true) { // scrollbar is at the bottom
  this.fetchMoreData();
 }
}

然后定义你的fetchMoreData函数来更新jsonReturnedValue的列表。

这只是一个蓝图。你需要自己照顾功能。

要检查滚动,请参阅此Check if a user has scrolled to the bottom

要在React中更多地了解事件侦听器,请参阅此内容 https://stackoverflow.com/a/41406239/3323709