我有一個小問題。我有一個嵌套的表。該表中的第一項應該刪除。到目前爲止,一切都很好。但是,如何才能在第一個元素上顯示「刪除用戶」按鈕?如何僅在父元素中顯示按鈕?
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,則可以啓用刪除按鈕。 –
是的是這個片段沒有充滿代碼。 –