[英]How to hide an element from a list if clicked twice without toggling in a vue nuxt application
I have got a clickable list in a Vue/Nuxt application.我在 Vue/Nuxt 应用程序中有一个可点击的列表。 When one item is selected, a little tick mark appears.
选择一个项目时,会出现一个小刻度线标记。 I would like to be able to unselect an item (the tick mark to disappear) if the item is clicked again.
如果再次单击该项目,我希望能够取消选择该项目(要消失的刻度线)。 If I click on another item, I would like this item to be selected and the previously selected item to unselect (only one item can be selected).
如果我点击另一个项目,我希望这个项目被选中并取消选择之前选择的项目(只能选择一个项目)。 So far, if I try to select another item, I need to click twice because the first click will only unselect the first selected item and the second click will select the new item.
到目前为止,如果我尝试选择另一个项目,我需要单击两次,因为第一次单击只会取消选择第一个选定的项目,第二次单击将选择新项目。 Any idea ??
任何的想法 ??
<template>
<div
v-for="(item, itemIndex) in list"
:key="itemIndex"
@click="onClick(itemIndex)"
>
<div>
<div v-if="activeIndex == itemIndex && selected === true">
<TickMark />
</div>
<Item />
</div>
</div>
</template>
<script>
export default {
props: {
questionModules: {
required: true,
type: Array,
},
},
data() {
return {
activeIndex: null,
selected: false,
}
},
methods: {
onClick (index) {
this.activeIndex = index
this.selected = !this.selected
},
},
}
</script>
because you don't need to change positions or sort the list - keeping the selected index is just fine, do it like this:因为您不需要更改位置或对列表进行排序 - 保留所选索引就可以了,请这样做:
<template>
<section
class="items-list">
<template v-for="(item, itemIndex) in list"
:key="itemIndex" >
<TickMark v-if="activeIndex === itemIndex
@click="selectItem(itemIndex)" /> // by clicking on the mark - it will toggle the selection
<Item />
</template>
</section>
</template>
<script>
export default {
props: {
questionModules: {
required: true,
type: Array,
},
},
data() {
return {
activeIndex: null
}
},
methods: {
selectItem (index) {
this.activeIndex = index
},
},
}
</script>
I've changed the architecture of the DOM so it will be without all the un-necessary elements我已经改变了 DOM 的架构,所以它将没有所有不必要的元素
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.