Search code examples
vue.jsmenudropdownbootstrap-vue

bootstrap-vue multi level drop down


I'm using BootstrapVue and I have a multi-level dropdown menu (with categories). This is an example from the official site:

https://bootstrap-vue.org/docs/components/dropdown

<b-dropdown id="dropdown-1" text="Dropdown Button" class="m-md-2">
  <b-dropdown-item>First Action</b-dropdown-item>
  <b-dropdown-item>Second Action</b-dropdown-item>
</b-dropdown>

But I don't know how to create a multi-level menu (I put dropdowns inside each other, but it doesn't work)! There's only a single-level dropdown example! How can I create a multi level one?

Thanks!


Solution

  • So as I mentioned in my comments you can wrap b-dropdown events and do something custom like this:

    window.onload = () => {
      new Vue({
        el: '#app',
        data() {
          return {
            name: 'BootstrapVue',
            isDropdown2Visible: false
          }
        },
        mounted: function () {
          this.$root.$on('bv::dropdown::show', bvEvent => {
            if(bvEvent.componentId === 'dropdown-2') {
              this.isDropdown2Visible = true;
            }
            })
          this.$root.$on('bv::dropdown::hide', bvEvent => {
            if(bvEvent.componentId === 'dropdown-2') {
              this.isDropdown2Visible = false;
            }
            if(this.isDropdown2Visible) {
              bvEvent.preventDefault()
            }
            })
        }
      })
    }
    body { padding: 1rem; }
    <link type="text/css" rel="stylesheet" href="https://unpkg.com/[email protected]/dist/css/bootstrap.min.css" />
    <link type="text/css" rel="stylesheet" href="https://unpkg.com/[email protected]/dist/bootstrap-vue.css" />
    
    <script src="https://unpkg.com/[email protected]/dist/vue.min.js"></script>
    <script src="https://unpkg.com/[email protected]/dist/bootstrap-vue.min.js"></script>
    
    <div id="app">
      <b-dropdown id="dropdown-1" text="Dropdown Button 1" class="m-md-2">
        <b-dropdown-item>First Action 1</b-dropdown-item>
        <b-dropdown-item>Second Action 1</b-dropdown-item>
        <b-dropdown id="dropdown-2" text="Dropdown Button 2" class="m-md-2">
            <b-dropdown-item>First Action 2</b-dropdown-item>
            <b-dropdown-item>Second Action 2</b-dropdown-item>
        </b-dropdown>
      </b-dropdown>
    </div>