【问题标题】:How to reloads the table when clicked using AJAX in reactjs Put Method [duplicate]在reactjs Put方法中使用AJAX单击时如何重新加载表[重复]
【发布时间】:2017-10-23 03:52:33
【问题描述】:

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

我的 onclick 代码

handleUpdate(id, name, address,department){ 

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

  jsonReturnedValue[data].Department = department,
  jsonReturnedValue[data].Employee_name = name,
  jsonReturnedValue[data].Address = address
  this.setState({jsonReturnedValue})
})
.catch(function(error) {
  console.log(error);

})

}

渲染和表格的代码

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">Update Employee</h4>
            </div>
            <form > 
              <div className="container"> 
              <div className="modal-body"> 
              <table> 
              <tbody> 
              <tr> 
              <td>Name</td> 
              <td> 
              <input  type="text" 
                      name="Employee_Name"
                      value={this.state.Employee_Name} 
                      required
                      onChange={(e) => this.handleChange(e, "Employee_Name")}/> 
              </td> 
              </tr> 
              <tr> 
              <td>Address</td> 
              <td> 
              <input  type="text" 
                      name="Address" 
                      value={this.state.Address} 
                      required
                      onChange={(e) => this.handleChange(e, "Address")}/> 
              </td> 
              </tr> 
              <tr> 
              <td>Department</td> 
              <td> 
              <input  type="text" 
                      name='Department' 
                      value={this.state.Department} 
                      required
                      onChange={(e) => this.handleChange(e, "Department")}/> 

              </td> 
              </tr> 
              </tbody> 
              </table> 
              </div> 
              </div> 
              <div className="modal-footer"> 
              <input type="button" className="btn btn-info"   disabled={!isEnabled}  onClick = { this.handleUpdate.bind(this,  this.state.Employee_ID , this.state.Employee_Name ,this.state.Address ,this.state.Department)} value =" Update Employee" data-dismiss="modal"/> 
              <button type="button" className="btn btn-default" data-dismiss="modal">Close</button> 
              </div> 
              </form>
            </div>
          </div>
    </div>

【问题讨论】:

  • 这似乎是您之前的一个问题的两个重复项中的第二个,正如我所指出的,您在其他地方也重复了您的问题。

标签: javascript asp.net api reactjs web


【解决方案1】:

jsonReturnedValue 应该使用index 而不是object。利用findIndex获取数组中带有Employee_ID = id的对象的索引

handleUpdate(id, name, address,department){ 

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

    })

}

【讨论】:

  • 当我调试它时,代码正在读取值但它不会改变表,除非我刷新它
  • 如果你设置状态,它应该更新视图
  • 我不知道发生了什么...
  • 有时会更新,有时不会,,
  • 不应该这样
猜你喜欢
  • 1970-01-01
  • 2017-10-22
  • 2019-07-20
  • 2021-07-01
  • 2017-05-21
  • 2013-11-28
  • 2011-11-07
  • 1970-01-01
  • 2021-12-30
相关资源
最近更新 更多