Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Calculator

0

63
Views
How to close dropdown when it is rendered to other page in Vue

I have a cart dropdown as you can see below: enter image description here

So my problem is whenever I go to the next page or do something, it stays open. But I want it to be closed when I go to another page. For this my component is here:

<template>
    <div class="custom-select" :tabindex="tabindex" @blur="open = false">
        <div class="selected" :class="{ open: open }" @click="open = !open">
            {{ selected.name }}
        </div>
        <div class="items" :class="{ selectHide: !open }">
            <div v-if="defaultValue != ''">{{ defaultValue }}</div>
            <div v-for="(option, i) of options" :key="i"
                 @click=" selected = option; open = false; $emit('input', option);">
                {{ option.name }}
            </div>
        </div>
    </div>
</template>


<script>

export default {
    props: {
        options: {
            type: Array,
            required: true,
        },
        defaultValue: {
            type: String,
            required: false,
            default: "Choose an option",
        },
        tabindex: {
            type: Number,
            required: false,
            default: 0,
        },
    },
    data() {
        return {
            open: false,
            selected: this.setDefaultValue(),
        };
    },
    mounted() {
        this.$emit("input", this.selected);
    },
    methods: {
        setDefaultValue() {
            if (this.defaultValue == '' && this.options.length > 0) {
                return this.options[0];
            }
            return {name: this.defaultValue};
        }
    }
};
</script>

I am open to any kind of solution to solve this issue.

7 months ago · Juan Pablo Isaza
1 answers
Answer question

0

You can create a "click outside" directive, that executes a close menu function whenever you click outside the menu. Then you also call the close menu function when any action in the menu is clicked.

Example directive in main.js:

Vue.directive('click-outside', {
  bind: function (el, binding, vnode) {
    el.clickOutsideEvent = function (event) {
      // Check that click was outside the el and its children
      if (!(el === event.target || el.contains(event.target))) {
        // Call the specified method
        vnode.context[binding.expression](event)
      }
    }
    document.body.addEventListener('click', el.clickOutsideEvent)
    document.body.addEventListener('touchend', el.clickOutsideEvent)
  },
  unbind: function (el) {
    document.body.removeEventListener('click', el.clickOutsideEvent)
    document.body.removeEventListener('touchend', el.clickOutsideEvent)
  }
})

In your menu:

<div v-click-outside="closeMenu"></div>
7 months ago · Juan Pablo Isaza Report
Answer question
Find remote jobs