1
,所以我就用React-Modal
這樣的對話 - 上渲染更改對象:一個對話框進行編輯 - 陣營模態
var React = require('react'),
ptypes = React.PropTypes;
var ReactDOM = require('react-dom');
var $ = require('jquery');
var VehiclePlantDialog = React.createClass({
PropTypes: {
vehiclePlant: ptypes.object
},
openModal: function (vehiclePlant) {
this.props.vehiclePlant = vehiclePlant;
console.log(vehiclePlant);
this.props.openModal();
},
render: function() {
console.log(this.props.vehiclePlant);
return (
<div className="modalLayout">
<form>
<input ref="inputText" />
<input type="submit" />
<button onClick={this.props.closeModal}>Cancel</button>
</form>
</div>
);
}
});
module.exports = VehiclePlantDialog;
也能正常工作,但現在當我這個勾到一個表中,我創建每個項目1個模態對不對?
錶行:
var React = require('react');
var ReactDOM = require('react-dom');
var Modal = require('react-modal');
var $ = require('jquery');
var VehiclePlantDialog = require('./VehiclePlantDialog.jsx');
const customStyles = {
content: {
top: '50%',
left: '50%',
right: 'auto',
bottom: 'auto',
marginRight: '-50%',
transform: 'translate(-50%, -50%)'
}
};
var VehiclePlantRow = React.createClass({
getInitialState: function() {
return { modalIsOpen: false };
},
openModal: function() {
console.log("set state open");
this.setState({ modalIsOpen: true });
},
afterOpenModal: function() {
},
closeModal: function() {
this.setState({ modalIsOpen: false });
},
componentWillMount: function() {
Modal.setAppElement('body');
},
render: function() {
return (
<tr key={this.props.plant.id}>
<td>{this.props.plant.code}</td>
<td>{this.props.plant.name}</td>
<td>{this.props.plant.createdAt}</td>
<td>{this.props.plant.expiresAt}</td>
<td>
{this.props.plant.isExport == 0 ? 'No' : 'Yes'}
</td>
<td>
<button onClick={this.openModal}>Edit</button>
<Modal
isOpen={this.state.modalIsOpen}
onAfterOpen={this.afterOpenModal}
onRequestClose={this.closeModal}
shouldCloseOnOverlayClick={false}
style={customStyles} >
<VehiclePlantDialog openModal={this.openModal} closeDialog={this.closeModal} plant={this.props.plant}/>
</Modal>
</td>
</tr>
);
}
});
module.exports = VehiclePlantRow;
我怎樣才能確保當我開口道莫代爾創建只有一個模態,改變對象?
剛剛完成這個,真棒男人謝謝你!它像魅力一樣工作。當你的解決方案的Sidenote調用來自道具的'bind'(而不是'this'本身)時,爲了安全起見,你需要傳遞'null'作爲第一個參數(React stuff)。 'this.props.onSelect.bind(null,this.props.plant)'。接受並提高您的答案。謝謝一堆!我花了一段時間。 – Tikkes