我有一个小问题。我有一个嵌套的表。该表中的第一项应该删除。到目前为止,一切都很好。但是,如何才能在第一个元素上显示“删除用户”按钮?如何仅在父元素中显示按钮?
class UserList extends Component {
constructor(props) {
super(props);
this.state = {
openDetail: false
}
}
parentUserToggle (item, index) {
this.setState({
openDetail: !this.state.openDetail
})
}
render() {
const { userProfile, index } = this.props;
let parentUserItem = null;
if (userProfile.children instanceof Object) {
parentUserItem = userProfile.children.map((children, index) => {
return <UserList key={children.ID} index={index} userProfile={children} />
});
}
return (
<div className="table-row" key={index}>
<div className="col-md-1">
{
userProfile.children ?
<button
className="btn btn-primary"
onClick={this.parentUserToggle.bind(this, userProfile.children)}
>
{ !this.state.openDetail ? 'OPEN' : 'CLOSE' }
</button>
: null
}
</div>
<div className="col-md-3">{userProfile.Name}</div>
<div className="col-md-3">{userProfile.City}</div>
<div className="col-md-3">{userProfile.Phone}</div>
<div className="col-md-2">
{
userProfile || !parentUserItem ?
<button
className="btn btn-danger"
onClick={this.props.removeUser}
> Remove User </button>
: null
}
</div>
<div className={this.state.openDetail ? 'table-true' : 'table-false'}>
{
parentUserItem
}
</div>
</div>
);
}
}
谢谢支持。
您的代码不工作 –
您可以使用索引来检查是否这是第一要素。如果索引为0,则可以启用删除按钮。 –
是的是这个片段没有充满代码。 –