0

i have table and in first three rows of table is hardcoded,also i have multiple inputs and when i enter data in input and when i clicked on submit button the data from inputs should goes into table row . here is my code

 this.state={
            Id:'',
            Name:'',
            birth:'',
            data:[
                {
                id:'1',
                name:'Muhammad Ali jinnah',
                dateofBirth:'1876'
                },
                {
                    id:'2',
                    name:'Allama Iqbal',
                    dateofBirth:'1877'
                },
                {
                    id:'3',
                    name:'Ahmad Bilal',
                    dateofBirth:'1992'
                }
            ]
        }
    }
    this.handleIDChange = this.handleIDChange.bind(this);
    this.handleNameChange = this.handleNameChange.bind(this);
    this.handleBirthChange = this.handleBirthChange.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
}
handleIDChange(event) {
    this.setState({Id: event.target.value});
  }
  handleNameChange(event) {
    this.setState({Name: event.target.value});
  }
  handleBirthChange(event) {
    this.setState({birth: event.target.value});
  }

  handleSubmit(event) {
    console.log('A ID:name and birth was submitted: ' + this.state.Id,this.state.Name,this.state.birth);
    event.preventDefault();
  }
 render() {
return (
  <div className="fun3">
    <BootstrapTable data={this.state.data}>
 <TableHeaderColumn isKey dataField='id' headerAlign="left" width="30"tdStyle={ {backgroundColor: ''}}>
          ID
   </TableHeaderColumn>
 <TableHeaderColumn dataField='name' dataAlign='center'headerAlign="center" width="60%" thStyle={ {fontWeight: 'lighter', marginLeft:'5px'}}>
        Name
</TableHeaderColumn>
 <TableHeaderColumn dataField='dateofBirth'dataAlign='center' headerAlign="right">
       Date of Birth
 </TableHeaderColumn>

    </BootstrapTable>
<br></br>
<form onSubmit={this.handleSubmit}>
<label>
 ID:
 <input type="text" name="Id"  onChange={this.handleIDChange} />
 </label>
 <label>
 Name:
 <input type="text" name="name"  onChange={this.handleNameChange}/>
   </label>
 <label>
 Date of Birth
  <input type="text" name="birth" onChange={this.handleBirthChange}/>
 </label>
<input type="submit" value="Submit" />
</form>
  </div>

this is my code i want to insert data after next to three rows..how i can do this ?

1

1 Answer 1

1
handleSubmit(event) {

  console.log('A ID:name and birth was submitted: ' + 
    this.state.Id,this.state.Name,this.state.birth);
  event.preventDefault();

  const { id, name, dateofBirth } = this.state;    
  const newObj = {
    id: id,
    name: name,
    dateofBirth: dateofBirth 
  };

  console.log("prevState");
  console.log(prevState);
  console.log(newState);
  console.log([...prevState.data, newObj]);
  this.setState(prevState => ({
    data: [...prevState.data, newObj ]
  }));

}
Sign up to request clarification or add additional context in comments.

11 Comments

can u explain it properly ??i cannt get it properly
I assume that in handleSubmit you have id, name, dateofBirth, in the state, right? Then the above code takes these values, creats newObj that has these values, and sets the states so that the array in the state gets the newObj as last array element...
const { id, name, dateofBirth } = this.state; const newObj = { id: id, name: name, dateofBirth: dateofBirth };the id,name and date of birth and the name,id,and dateofbirth in obects are same ??
you ask to create new object for id,name and dateofbirth??this object for that value which i get from input box ?
Yes. Just to have an object that will be used in the call to setState in my answer
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.