vuejs3element-plus

How to open all the el-menu-item in Element-plus using a button


<template>
  <el-button @click="expandMenu">Expand </el-button>
  <el-menu
    default-active="2"
    class="el-menu-vertical-demo"
    :default-openeds="menu"
  >
    <el-sub-menu index="1">
      <template #title>
        <el-icon><location /></el-icon>
        <span>Navigator One</span>
      </template>
      <el-menu-item-group>
        <template #title><span>Group One</span></template>
        <el-menu-item index="1-1">item one</el-menu-item>
        <el-menu-item index="1-2">item two</el-menu-item>
      </el-menu-item-group>
      <el-menu-item-group title="Group Two">
        <el-menu-item index="1-3">item three</el-menu-item>
      </el-menu-item-group>
      <el-sub-menu index="1-4">
        <template #title><span>item four</span></template>
        <el-menu-item index="1-4-1">item one</el-menu-item>
      </el-sub-menu>
    </el-sub-menu>
    <el-menu-item index="2">
      <el-icon><icon-menu /></el-icon>
      <template #title>Navigator Two</template>
    </el-menu-item>
    <el-menu-item index="3" disabled>
      <el-icon><document /></el-icon>
      <template #title>Navigator Three</template>
    </el-menu-item>
    <el-menu-item index="4">
      <el-icon><setting /></el-icon>
      <template #title>Navigator Four</template>
    </el-menu-item>
  </el-menu>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import {
  Document,
  Menu as IconMenu,
  Location,
  Setting,
} from '@element-plus/icons-vue'
const menu = ref([])
const isCollapse = ref(true)
const expandMenu = () => {
  if(menu.value.length) {
    menu.value = []
  } else {
    menu.value = ['1', '2', '3', '4']
  }
}
</script>

<style>
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
}
</style>

I'm trying to expand all the menus using a button but it only works when I go to another route and return to this route. For example, this route is localhost:8080/example, when I click the expand button, nothing happens, when I go to another route localhost:8080/sample and return to localhost:8080/example all of the menus will open.

I assume this is cause by the default-openeds as it only works on the first load. Is there any way I can expand this?


Solution

  • default-opened is used only for the first mount and it can't be dynamically changed after that.

    Please use open and close functions of the menu.

    See example below

    <template>
      <el-button @click="expandMenu">Expand </el-button>
      <el-menu
        default-active="2"
        class="el-menu-vertical-demo"
        :default-openeds="menu"
         ref="mainMenu"
      >
        <el-sub-menu index="1">
          <template #title>
            <el-icon><location /></el-icon>
            <span>Navigator One</span>
          </template>
          <el-menu-item-group>
            <template #title><span>Group One</span></template>
            <el-menu-item index="1-1">item one</el-menu-item>
            <el-menu-item index="1-2">item two</el-menu-item>
          </el-menu-item-group>
          <el-menu-item-group title="Group Two">
            <el-menu-item index="1-3">item three</el-menu-item>
          </el-menu-item-group>
          <el-sub-menu index="1-4">
            <template #title><span>item four</span></template>
            <el-menu-item index="1-4-1">item one</el-menu-item>
          </el-sub-menu>
        </el-sub-menu>
        <el-menu-item index="2">
          <el-icon><icon-menu /></el-icon>
          <template #title>Navigator Two</template>
        </el-menu-item>
        <el-menu-item index="3" disabled>
          <el-icon><document /></el-icon>
          <template #title>Navigator Three</template>
        </el-menu-item>
        <el-menu-item index="4">
          <el-icon><setting /></el-icon>
          <template #title>Navigator Four</template>
        </el-menu-item>
      </el-menu>
    </template>
    
    <script lang="ts" setup>
    import { ref } from 'vue';
    import {
      Document,
      Menu as IconMenu,
      Location,
      Setting,
    } from '@element-plus/icons-vue';
    
    const menu = ref([]);
    const mainMenu = ref(null);
    
    const expandMenu = () => {
      if (menu.value.length) {
        menu.value = [];
        mainMenu.value.close('1');
        mainMenu.value.close('2');
        mainMenu.value.close('3');
        mainMenu.value.close('4');
      } else {
        mainMenu.value.open('1');
        mainMenu.value.open('2');
        mainMenu.value.open('3');
        mainMenu.value.open('4');
        menu.value = ['1', '2', '3', '4'];
      }
    };
    </script>
    
    <style>
    .el-menu-vertical-demo:not(.el-menu--collapse) {
      width: 200px;
      min-height: 400px;
    }
    </style>