【问题标题】:How to reloads the table when clicked using AJAX in reactjs在reactjs中使用AJAX单击时如何重新加载表格
【发布时间】:2017-05-23 07:11:15
【问题描述】:

我想知道如何只重新加载表格,而不是整个页面的反应。我试过使用history.go(0); 但是,它会重新加载整个页面,请检查如何重新加载它,如果我要使用 forceUpdate,根据研究,你应该避免使用它。我正在尝试做一个 AJAX,但我不知道该放什么放在哪里...它与 php 不同..

我的 onclick 代码

 handleSubmit( name, address,department){

 const laman = {
      'Employee_Name': name,
      'Address': address,
      'Department': department
    }
    return fetch('http://localhost:5118/api/employeedetails/PostEmployeeDetail?', {
        method: 'POST',
        headers: {
        'Content-Type': 'application/json'
        },
        body: JSON.stringify(laman)
    })
    .then(function(response) {
      console.log(response)
      return response.json();
    })
    .then(function (result) {
      history.go(0);
    })
    .catch(function(error) {
      console.log(error);

    })
}

这是 onclick 按钮和表格本身的代码

render() {
     const isEnabled = this.canBeSubmitted();
    let {jsonReturnedValue} = this.state;
  return(
    <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>
                    {jsonReturnedValue.map((d,i) => this.renderItem(d,i))}
                </tbody>
            </table>
          </div>

      {/*Updating*/}

    <div className="modal fade" id="UpdateEmployee" role="dialog">
           <div className="modal-dialog">
             <div className="modal-content">
                <div className="modal-header">
                   <button type="button" className="close" data-dismiss="modal">&times;</button>
                  <h4 className="modal-title">ADD  Employee</h4>
            </div>
<form>

        <div className="container">
          <div className="modal-body">
              <table> 
              <tbody>
              <tr>
              <td>Name</td>
              <td>
              <input type="text"
                    ref="Employee_Name"
                    onChange={(e) => this.handleChange(e, "Employee_Name")}
                    required
                    /> 
              </td>
              </tr>
              <tr>
              <td>Address</td>
              <td>
               <input type="text"
                     ref="Address"
                    onChange={(e) => this.handleChange(e, "Address")}
                    required
                     />
              </td>
              </tr>
              <tr>
              <td>Department</td>
              <td>
               <input type="text"

                      onChange={(e) => this.handleChange(e, "Department")}
                      required
              /> 
              </td>
              </tr>
              </tbody>
              </table>

          </div>
          </div>
          <div className="modal-footer">
            <input type="button" className="btn btn-info"disabled={!isEnabled} 
                    onClick = { this.handleSubmit.bind(
                                this, this.state.Employee_Name,
                                this.state.Address,
                                this.state.Department)
                                 }
                               value =" Add Employee"
                               data-dismiss="modal"/>
            <button type="button" className="btn btn-default" data-dismiss="modal" >Close</button>
          </div>

            </form>

PS:它也在同一页中..我不知道我该怎么称呼它的原因

【问题讨论】:

  • 我建议将 React JS 与 Redux 一起使用。你不会有那种问题。或者你可以在你的组件中使用状态。
  • 你能展示一下你是如何渲染表格中的数据的吗?
  • 好的,已编辑
  • 这似乎是您上一个问题的两个重复项中的第一个,正如我所指出的,您在其他地方也重复了您的问题。

标签: javascript asp.net api reactjs web


【解决方案1】:

由于您是基于state jsonReturnedValue 构建表格,因此除了将新添加的值附加到此状态之外,您不需要做任何额外的事情,因为 React 将自动处理仅更新更改

handleSubmit函数改为

handleSubmit( name, address,department){

 const laman = {
      'Employee_Name': name,
      'Address': address,
      'Department': department
    }
    return fetch('http://localhost:5118/api/employeedetails/PostEmployeeDetail?', {
        method: 'POST',
        headers: {
        'Content-Type': 'application/json'
        },
        body: JSON.stringify(laman)
    })
    .then(function(response) {
      console.log(response)
      return response.json();
    })
    .then((result)=> {
       var jsonReturnedValue = [...this.state.jsonReturnedValue];
       jsonReturnedValue.push(laman);
       this.setState({jsonReturnedValue})
    })
    .catch(function(error) {
      console.log(error);

    })
}

【讨论】:

  • 很高兴有帮助:)
  • 你帮助了我的灵魂先生 :) 再次感谢你^^
  • 在编辑中,您必须找到要编辑的数组项,更改其值并设置为状态
  • 先生如何设置我点击的行是数组?抱歉我的问题太多了,我太陌生了
  • 将点击项的索引传递给编辑功能,然后var jsonReturnedValue = [...this.state.jsonReturnedValue]; jsonReturnedValue[index].Department = "new value" ... etc; this.setState({jsonReturnedValue})
猜你喜欢
  • 2017-10-22
  • 2017-10-23
  • 2019-07-20
  • 1970-01-01
  • 2021-07-01
  • 2013-11-28
  • 1970-01-01
  • 1970-01-01
  • 2021-12-30
相关资源
最近更新 更多