问题描述:
- 按钮1应仅显示/隐藏楼层1的数据。
- 按钮2应显示/隐藏2楼的数据。
- 按钮3应显示/隐藏楼层3的数据。
每个按钮都显示/隐藏所有内容。
我可以为每个JavaScript对象重复我的代码,这样就可以工作了--但是我想对引用的任何JSON对象的重复进行抽象。
我尝试了不同的方法,但放弃了,决定这样问。看到回复会帮助我“思考SVELTE”
https://svelte.dev/repl/6e498fa577674323b4a602b4b4b3acf2?version=3.53.1
<div class="parent-container">
{#each rooms as room}
<button on:click={showHide}>Floor number {room.floor} </button>
{#if showState}
<div class={showState}>
<h2>{room.floor}</h2>
{#each room.rooms as singleRoom}
<div>
<h2>{singleRoom.number}</h2>
</div>
{/each}
</div>
{/if}
{/each}
</div>
<script>
let showState = "hide";
function showHide(){
showState = (showState ==="hide") ? "show" : "hide";
}
let rooms =[
{
floor:1,
rooms:[{number:101},{number:102},{number:103}]
},
{
floor:2,
rooms:[{number:201},{number:202},{number:203}]
},
{
floor:3,
rooms:[{number:301},{number:302},{number:303}]
},
]
</script>
<style>
.hide{
display: none;
}
</style>
4条答案
按热度按时间9q78igpj1#
You currently only have one state for multiple objects. There are multiple ways of approaching this:
#each
into a separate component, that way each object will have its own separate stateE.g. storing the state in a separate array:
Here the index provided by the
#each
is used to access thevisiblity
array.piah890a2#
You said that you want to "think Svelte", so I think the most idiomatic option would be to extract the logic for each floor into a new
Floor
component and let each instance manages its own state.Honestly, I'm surprised no one else suggested this. Using this solution, you can have as many rooms as you want, and helps separate your code. You can do it like this:
Run it on the Svelte REPL
App.svelte
Floor.svelte
I also changed
showState
into a boolean. This makes it a bit easier to understand and follow along. Also, as @Corrl pointed out, it is better to use theclass:
directive instead of an if-statement.ryevplcw3#
我喜欢@ MichaelM的solution将地板逻辑和样式移动到它自己的地板组件中。这种组件分离的做法将使编辑或重用代码更容易。
然而,我也相信代码越少,编写代码就越容易。所以我建议下面这个稍微简单一些的解决方案(或者试试REPL here),它将可视性作为一个元素添加到楼层数据中,并允许数据驱动逻辑,而不是相反。如果你想了解更多,这个解决方案还利用了Svelte的内联处理程序功能。希望这对你有所帮助!
aiqt4smr4#
需要使用数组为每个房间设置showState,并使用属性存储楼层号,并在事件中使用
this.getAttribute('attr-data-floor')
获取楼层号: