我想要創建在某個類中呈現的同一個html的倍數時遇到問題。例如,我有一個div可能是這樣的在ReactJs中添加多個div或者渲染
是[標題放在這裏,是一個輸入字段] [下拉] [文本區域]
[提交]
[添加其他字段]
在添加另一個字段時,我想克隆此視圖並能夠再次添加更多。
這是我到目前爲止有:
var UpdateForm = React.createClass({
handleSubmit : function(e) {
e.preventDefault();
var title = React.findDOMNode(this.refs.title).value.trim();
var date = React.findDOMNode(this.refs.date).value.trim();
var body = React.findDOMNode(this.refs.body).value.trim();
if(!title||!date || !body) {
return;
}
this.props.onSubmit({title:title, date : date, body : body});
React.findDOMNode(this.refs.title).value = '';
React.findDOMNode(this.refs.date).value = '';
React.findDOMNode(this.refs.body).value = '';
//React.findDOMNode(this.refs.sub).value = '';
},
render: function() {
return(
<div id = "This is what I want to duplicate on each button click">
<form className="form-horizontal" onSubmit={this.handleSubmit}>
<div class="form-control">
<label className="col-sm-0 control-label ">Title:</label>
<div class="col-sm-5">
<input className = "form-control" type = "text" placeholder="Title...." ref = "title"/>
</div>
</div>
<div class="form-control">
<label className="col-sm-0 control-label ">Date:</label>
<div class="col-sm-5">
<input className = "form-control" type = "text" placeholder="Date...." ref = "date"/>
</div>
</div>
<div className="form">
<label htmlFor="body" className="col-sm-0 control-label">Report Body:</label>
<div className="column">
<textarea className = "ckeditor" id = "ckedit" ref = "body"></textarea>
</div>
</div>
<div class="form-group">
<label className="col-sm-0 control-label"></label>
<div class="col-sm-5">
<input type = "submit" value="Save Changes" className = "btn btn-primary" />
</div>
</div>
</form>
<div className="btn-group">
<button type="button" className="btn btn-danger">Assign to</button>
<button type="button" className="btn btn-danger dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<span className="caret"></span>
<span className="sr-only">Toggle Dropdown</span>
</button>
<ul className="dropdown-menu">
{
this.props.forms.map(function(form){
return (
<li><a href ="#">{form}</a></li>
)})}
</ul>
<button className="btn btn-success btn-block" onClick={this.addInputField}>Add Subform</button>
</div>
</div>
);
}
});
我想我需要添加:
addDiv: function(e) {
e.preventDefault();
//have a array of divs?
//push a the same div into it?
//then set state of that array?
}
我知道的jQuery我可以只寫追加這個標記,每當我打了一個功能一個按鈕,但我不知道如何在這裏思考。
所以創建一個React視圖。現在你想要多個視圖?把另一個視圖置於頂部,當你想要另一個視圖時,創建子視圖的另一個實例。 – Cymen
我對這意味着什麼感到困惑。你是說,當我在另一個班級打電話時,只需再次打電話給他?例如,var MainClass = React.createClass({render(){//通用代碼在這裏 //然後一遍又一遍?)} –
Karan
你到底在問什麼?提交按鈕時是否要渲染額外的標記? – badAdviceGuy