element可折叠组件实现动态多级菜单(collapse)

该文章展示了一个Vue组件,利用el-collapse和v-for指令动态渲染多级目录结构。组件接受一个包含多级标题的数据列表,能够根据children属性展开或关闭子级目录。样式调整优化了内容的显示效果。
摘要由CSDN通过智能技术生成

效果图:在这里插入图片描述
在这里插入图片描述

<template>
  <el-collapse class="coll-wrap">
    <div v-for="(item, i) in dataList" :key="i">
      <el-collapse-item
        v-if="item.children.length"
        :title="item.title"
        :name="i"
        >
        <asideBarItem v-if="item.children.length" :dataList="item.children" />
      </el-collapse-item>
      <div class="item" v-else>{{ item.title }}</div>
    </div>
  </el-collapse>
</template>

<script>
  export default {
    name: "asideBarItem",
    props: {
      dataList: {
        type: Array,
        default: () => [
          {
            title: "这是1级目录",
            children: [
              {
                title: "1.1",
                children: [],
              },
              {
                title: "1.2",
                children: [
                  {
                    title: "1.2.1",
                    children: [
                      {
                        title: "1.2.1.1",
                        children: [],
                      },
                    ],
                  },
                ],
              },
            ],
          },
          {
            title: "这是2级目录",
            children: [],
          },
        ],
      },
    },
    data() {
      return {};
    },
    mounted() {},
  };
</script>

<style scoped>
  .coll-wrap /deep/ .el-collapse-item__content {
    padding-bottom: 0 !important;
  }
  .coll-wrap /deep/ .el-collapse-item__wrap {
    border: 0 !important;
  }
  .item{
    padding: 15px 0;
  }
</style>

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Element UI 中实现多级右键菜单可以通过自定义指令和事件监听来实现。下面是一个简单的示例: 首先,你需要创建一个自定义指令来监听鼠标右键事件,并在触发时显示菜单。可以在 `main.js` 文件中注册该指令: ```javascript import Vue from 'vue' import { Dropdown, DropdownMenu, DropdownItem } from 'element-ui' Vue.directive('contextmenu', { bind: function (el, binding, vnode) { el.addEventListener('contextmenu', function (event) { event.preventDefault() const dropdown = new Vue({ render: h => { return h(Dropdown, { props: { trigger: 'manual' }, on: { command: command => { vnode.context.$emit('command', command) dropdown.$destroy() } } }, [ h(DropdownMenu, null, binding.value.map(item => { return h(DropdownItem, { props: { command: item.command }, domProps: { innerHTML: item.label } }) })) ]) }, mounted() { this.$refs.dropdown.$el.style.position = 'fixed' this.$refs.dropdown.$el.style.left = `${event.clientX}px` this.$refs.dropdown.$el.style.top = `${event.clientY}px` this.$refs.dropdown.show() }, destroyed() { this.$refs.dropdown.hide() } }).$mount() document.body.appendChild(dropdown.$el) }) } }) ``` 然后,在你需要使用右键菜单的组件中,使用 `v-contextmenu` 指令来绑定右键菜单的内容和事件: ```html <template> <div v-contextmenu="contextMenuItems" @command="handleCommand"> Right click me </div> </template> <script> export default { data() { return { contextMenuItems: [ { label: 'Item 1', command: 'item1' }, { label: 'Item 2', command: 'item2' }, { label: 'Submenu', children: [ { label: 'Subitem 1', command: 'subitem1' }, { label: 'Subitem 2', command: 'subitem2' } ] } ] } }, methods: { handleCommand(command) { console.log('Command:', command) } } } </script> ``` 在这个例子中,右键菜单的内容由 `contextMenuItems` 数组定义,每个菜单项包括 `label` 和 `command` 两个属性。当菜单项被点击时,将会触发 `@command` 事件,并将对应的 `command` 参数传递给 `handleCommand` 方法进行处理。 通过这种方式,你可以实现多级的右键菜单。当右键点击目标元素时,菜单会在鼠标位置显示,并且可以处理菜单项的点击事件。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值