我是React的新手,并在删除记录后如何操纵DOM。我有一些反应组件,但我关注的是删除我设置的rails api中的记录。我在Ajax onclick调用上删除了ok的记录,但是不知道使用React删除特定行的最佳方法。
表格中的数据
var ContactAll = React.createClass({
getInitialState: function() {
return {
contacts: []
}
},
componentDidMount: function() {
$.ajax({
dataType: "json",
url: '/all-contacts',
type: "GET",
context: this,
success: function(data, status, xhr) {
this.setState({ contacts: data });
}
});
},
render: function(){
if(this.state.contacts == 0) {
return (
<div>Loading</div>
)
} else {
var contactComponents = this.state.contacts.map(function(contact, i) {
var full_name = contact.first_name + ' ' + contact.last_name
return <tr key={i} className="contact">
<td>{ full_name}</td>
<td></td>
<td>{ contact.email_address }</td>
<td>{ contact.phone_number }</td>
<td>{ contact.company_name }</td>
<td><DeleteButton email={contact.email_address} /></td>
</tr>;
});
return <div>
<table>
<tbody>
<tr>
<th>Contact Name</th>
<th></th>
<th>Email Address</th>
<th>Phone Number</th>
<th>Company Name</th>
</tr>
{contactComponents};
</tbody>
</table>
</div>
}
}
});
在ContactAll组件中删除按钮
var DeleteButton = React.createClass({
onClick: function() {
var email = this.props.email;
$.ajax({
data: {email_address: email },
url: '/delete-contact',
dataType: 'html',
type: "POST",
success: function(data, status, xhr) {
$('.delete-success').slideDown(400);
setTimeout(function() { $(".delete-success").slideUp(400); }, 5000);
}
});
},
render: function(){
return(
<button onClick={() => { this.onClick(this.props.email) }}>Delete</button>
)
}
});
单击相应行的删除按钮时删除特定行的最佳方法是什么?
谢谢!
答案 0 :(得分:4)
创建一个删除函数,然后将数组的索引发送到该函数,并使用splice删除该数组条目。将对此deleteRow函数的引用传递给子组件,并从那里调用它,如this.props.deleteRow(this.props.index);
。查看完整代码
像
deleteRow: function(index) {
var contacts = [...this.state.contacts];
contacts.splice(index, 1);
this.setState({contacts});
},
var ContactAll = React.createClass({
getInitialState: function() {
return {
contacts: []
}
},
componentDidMount: function() {
$.ajax({
dataType: "json",
url: '/all-contacts',
type: "GET",
context: this,
success: function(data, status, xhr) {
this.setState({ contacts: data });
}
});
},
deleteRow: function(index) {
var contacts = [...this.state.contacts];
contacts.splice(index, 1);
this.setState({contacts});
},
render: function(){
if(this.state.contacts == 0) {
return (
<div>Loading</div>
)
} else {
var contactComponents = this.state.contacts.map(function(contact, i) {
var full_name = contact.first_name + ' ' + contact.last_name
return <tr key={i} className="contact">
<td>{ full_name}</td>
<td></td>
<td>{ contact.email_address }</td>
<td>{ contact.phone_number }</td>
<td>{ contact.company_name }</td>
<td><DeleteButton onClick ={this.deleteRow.bind(this)} index={i} email={contact.email_address} /></td>
</tr>;
}.bind(this));
return <div>
<table>
<tbody>
<tr>
<th>Contact Name</th>
<th></th>
<th>Email Address</th>
<th>Phone Number</th>
<th>Company Name</th>
</tr>
{contactComponents};
</tbody>
</table>
</div>
}
}
});
var DeleteButton = React.createClass({
onClick: function() {
var email = this.props.email;
this.props.deleteRow(this.props.index);
$.ajax({
data: {email_address: email },
url: '/delete-contact',
dataType: 'html',
type: "POST",
success: function(data, status, xhr) {
$('.delete-success').slideDown(400);
setTimeout(function() { $(".delete-success").slideUp(400); }, 5000);
}
});
},
render: function(){
return(
<button onClick={() => { this.onClick(this.props.email) }}>Delete</button>
)
}
});
&#13;