You can try adding an attribute to the data to store _isOpen
. From there, you can set all of the _isOpen values to false and set the proper one to true when clicked.This solution will probably require you to deep copy the data when you pull it though.
html:
<template>
<table>
<template for:each={accounts} for:item="account" for:index="idx">
<tr key={account.id} onclick={handleClick} data-index={idx}><td>{account.name}</td></tr>
<template if:true={account._isOpen}>
<tr key={account.id}><td>More</td></tr>
</template>
</template>
</table>
</template>
javascript:
handleClick(evt){
this.accounts.map(account=>{
account._isOpen = false;
});
this.accounts[parseInt(evt.currentTarget.dataset.index, 10)]._isOpen = true;
}
You may also just want to consider making a new component to use for the children.