mardi 23 mai 2017

How to reloads the table when clicked using AJAX in reactjs Put Method

I was wondering how will I reload only the table, not the whole page on reacts. I've tried using history.go(0); however, it reloads the whole page please check how can I reload it, if I was going to use forceUpdate, based on research you should avoid using it. Im trying do an AJAX but i dont know what to put where to put it... it is way different than php..

my code for the 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);

})

}

code for the render and the table

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>




Aucun commentaire:

Enregistrer un commentaire