無法更改/更新來自數據庫的輸入值。試圖瞭解https://facebook.github.io/react/docs/forms.html,但沒有得到它。流星(REACT) - 無法更新輸入文本值
基本上代碼是關於以表格格式顯示角色集合數據,更新工具是模態輸入字段。
這是我UMRolesList.jsx
export default class UMRolesList extends TrackerReact(Component) {
rolesListData(){
return Meteor.roles.find({}).fetch();
}
constructor(){
super();
this.state = {
subscription : {
"rolesData" : Meteor.subscribe('rolefunction'),
}
}
}
render(){
return(
<section className="Content">
<div className="row reportWrapper">
<div className="col-md-10 col-lg-12 col-sm-12 col-xs-12 noLRPad">
<div className="col-lg-12 col-md-12 col-sm-12 col-xs-12 noLRPad">
<h1 className="reportTitleName">LIST OF ROLES</h1>
<hr/>
<UMaddRoles/>
<table className="table-responsive table table-striped table-hover myTable dataTable no-footer">
<thead className="table-head umtblhdr">
<tr className="hrTableHeader">
<th className="umHeader"> Role </th>
<th className="umHeader"> Action </th>
</tr>
</thead>
<tbody>
{ this.rolesListData().map((rolesData)=>{
return <UMadd_role key={rolesData._id} roleDataVales={rolesData}/>
})
}
</tbody>
</table>
</div>
</div>
</div>
</section>
);
}
}
這是我UMadd_role.jsx
export default class UMadd_role extends TrackerReact(Component) {
handleChange(event){
console.log("changing the text area to: " + event.target.value)
this.setState({value: event.target.value});
}
handleSubmit(event) {
alert('A name was submitted: ' + this.state.value);
event.preventDefault();
}
constructor(props) {
super(props);
this.state = {
roleName: props.roleDataVales.name,
};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
render(){
return(
<tr>
<td> {this.props.roleDataVales.name}</td>
<td>
<button className="editrole fa fa-pencil-square-o" data-toggle="modal" data-target={`#edit-${this.props.roleDataVales._id}`} ></button>
<div id={`edit-${this.props.roleDataVales._id}`} className="modal fade" role="dialog">
<div className="modal-dialog">
<div className="modal-content reportWrapper">
<div className="modal-header">
<button type="button" className="close" data-dismiss="modal">×</button>
<h4 className="modal-title">Edit Role</h4>
</div>
<div className="modal-body col-lg-12 col-md-12 col-sm-12 col-xs-12">
<form className="editroles">
<div className="form-group col-lg-5 col-md-4 col-xs-12 col-sm-12 paddingLeftz">
<label>Role Name*</label>
<input type="text" ref="roleName" className="form-control rolesField" name={`${this.props.roleDataVales._id}-Namerole`} value=value={`${this.state.roleName}`} onChange={this.handleChange.bind(this)} required/>
</div>
<div className="form-group col-lg-1 col-md-4 col-xs-12 col-sm-12 ">
<label> </label>
<button type="button" onClick={this.editRole.bind(this)} id={this.props.roleDataVales._id} className="btn btn-primary submit" data-dismiss="modal">Edit Role</button>
</div>
</form>
</div>
<div className="modal-footer">
<button type="button" className="btn btn-primary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</td>
</tr>
);
}
}
提前感謝!
「change」是否意味着輸入欄只顯示空白?像價值沒有達到那裏? – Casey
如果您編寫'value = {this.props.roleDataVales.name}',那麼該值將不會更新,除非組件重新顯示。 您可以分享您將代碼傳遞給此代碼段所屬組件的代碼嗎? –
請看看@my更新的代碼,並告訴我哪裏出錯了。 – Rashmi