看到我在使用add in ajax
时遇到问题 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})
this.refs.Name.value="";
this.refs.Address.value="";
this.refs.Department.value="";
// this.setState({ value: '' });
})
.catch(function(error) {
console.log(error);
})
但是当它添加新行时它没有id因为它不知道要放入什么ID ....但是当我刷新它已经有了一个id因为它被添加到数据库中而且它是我的sql中的autoincreament ..为什么 这是图片
答案 0 :(得分:1)
而不是将此数据放入state
变量:
const laman = {
'Employee_Name': name,
'Address': address,
'Department': department
}
var jsonReturnedValue = [...this.state.jsonReturnedValue];
jsonReturnedValue.push(laman);
从服务器返回数据并将该数据推送到具有id的state
变量中。
像这样:
.then((result)=> {
var jsonReturnedValue = [...this.state.jsonReturnedValue];
jsonReturnedValue.push(result);
this.setState({jsonReturnedValue})
.....
答案 1 :(得分:0)
您可以在上面添加的值+ 1
中手动添加IDprivate static Date timeFormat(String timeZone) throws ParseException {
SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
DateFormat gmtFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
gmtFormat.setTimeZone(TimeZone.getTimeZone(TimeZone.getDefault().getID()));
Date date = null;
try {
date = gmtFormat.parse(sdfDate.format(new Date()));
LOG.info("GMT format time and date ==>> "+date);
} catch (ParseException e) {
e.printStackTrace();
}
DateFormat pstFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
pstFormat.setTimeZone(TimeZone.getTimeZone(timeZone));
String timedd = pstFormat.format(date);
LOG.info("Return the new time based on timezone : "+pstFormat.format(date));
return gmtFormat.parse(timedd);
}