Answer the question
In order to leave comments, you need to log in
How to hide the dropdown list when selecting any of its items?
<template>
<div class="sidebar">
<button class="sidebar__button sidebar__button_info">данные о контакте</button>
<div class="sidebar__button_status" @click="openClick()">
{{ status}}
</div>
<ul class="sidebar__button_list_item">
<li class="sidebar__button sidebar__button_list"
v-for="item in items"
:key="item"
@click="changeClick(item)">
{{item}}
</li>
</ul>
</div>
</template>
<script>
export default {
name: 'sidebar',
data () {
return {
items: ['в сети', 'занят', 'отсутсвую', 'в самолете', 'в пути'],
status: 'статус'
}
},
methods: {
openClick: function () {
this.show = !this.show
const a = document.getElementsByClassName('sidebar__button_status')
if (this.show) {
a[0].classList.add('open-list')
} else {
a[0].classList.remove('open-list')
}
},
changeClick: function (item) {
this.status = item
}
}
}
</script>
Answer the question
In order to leave comments, you need to log in
The openClick method - who taught you such an abomination? Read the documentation on how to work with classes correctly , it is explained quite clearly.
data: () => ({
show: false,
items: [ 'в сети', 'занят', 'отсутствую', 'в самолете', 'в пути' ],
status: 'статус',
}),
methods: {
selectStatus(status) {
this.status = status;
this.show = false;
},
},
<div
@click="show = !show"
v-text="status"
></div>
<ul v-show="show">
<li
v-for="n in items"
v-text="n"
@click="selectStatus(n)"
></li>
</ul>
<template>
<div class="sidebar">
<button class="sidebar__button sidebar__button_info">данные о контакте</button>
<div class="sidebar__button_status" :class="{'open-list': show}" @click="show = !show">
{{ status}}
</div>
<ul class="sidebar__button_list_item">
<li class="sidebar__button sidebar__button_list"
v-for="item in items"
:key="item"
@click="status = item">
{{item}}
</li>
</ul>
</div>
</template>
<script>
const items = ['в сети', 'занят', 'отсутсвую', 'в самолете', 'в пути'];
export default {
name: 'sidebar',
data () {
return {
show: false, // для реактивности
items,
status: 'статус'
}
}
}
</script>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question