vue使用element-ui创建多级菜单

1.代码是从项目直接搬下来的,其中的一些方法,你们可能用不到,一个el-table-column代表一列 ,第四个el-table-column中的一些v-if是用来判断每级创建不同的button按钮。

<!--当 row 中包含 children 字段时,被视为树形数据。渲染树形数据时,必须要指定 row-key。-->
            <!--通过指定 row 中的 hasChildren 字段来指定哪些行是包含子节点。children 与 hasChildren 都可以通过 tree-props 配置。-->
            <el-table :data="dat" style="width: 100%;" :row-key="getRowKey"
                :tree-props="{ children: 'children', hasChildren: 'haschildren' }">
                <el-table-column prop="name" label="名称">
                </el-table-column>
                <el-table-column label="序号" prop="num">
                </el-table-column>
                <el-table-column prop="createTime" label="时间">
                </el-table-column>
                <el-table-column label="操作" width="400">
                    <template slot-scope="scope">
                        <template v-if="scope.row.tree == 1">
                            <el-button type="primary" size="mini" icon="el-icon-edit"
                                @click="modify(scope.row.siteId, scope.row.name)">维护</el-button>
                            <el-button type="success" size="mini" icon="el-icon-plus"
                                @click="add(scope.row.siteId)">新增</el-button>
                        </template>
                        <template v-else-if="scope.row.tree == 2">
                            <el-button type="primary" size="mini" icon="el-icon-edit"
                                @click="modifys(scope.row.transformerId, scope.row.siteId, scope.row)">维护</el-button>
                            <el-button type="success" size="mini" icon="el-icon-plus"
                                @click="adds(scope.row.transformerId)">新增</el-button>
                            <el-button type="danger" size="mini" icon="el-icon-delete-solid"
                                @click="deleted(scope.row.transformerId)">删除</el-button>
                        </template>
                        <template v-else-if="scope.row.tree == 3">
                            <el-button type="primary" size="mini" icon="el-icon-edit"
                                @click="modifysd(scope.row.lineId, scope.row.name)">维护</el-button>
                            <el-button type="success" size="mini" icon="el-icon-plus"
                                @click="addsd(scope.row.id, scope.row.name)">创建线路图</el-button>
                            <el-button size="mini" type="warning"
                                @click="fault(scope.row.id, scope.row.ctNum, scope.row.systemNum, scope.row.name)">计算故障</el-button>
                            <el-button type="danger" size="mini" icon="el-icon-delete-solid"
                                @click="deletes(scope.row.lineId)">删除</el-button>
                        </template>
                    </template>
                </el-table-column>
            </el-table>

2.数据格式 很简单,:data='dat'为总数据,每个el-table-column中的prop代表每列的数据关键字,每层数据都要包含children,这样组件就会自动检测,继续往下层找相同的关键字。

 // 数据格式
      dat: {
        name: '临沂公司',
        num: '1',
        createTime:'2023-7-24',
        tree: 1,//根据这个关键字来判断每级创建不同的按钮
        children: [{
          name: '亚洲变电站',
          num: '1',
          createTime:'2023-7-24',
          tree: 2,//根据这个关键字来判断每级创建不同的按钮
          children: [{
            name: '太平洋线路',
            num: '1',
            createTime:'2023-7-24',
            tree: 3,//根据这个关键字来判断每级创建不同的按钮
          }]
        }]
      }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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` 方法进行处理。 通过这种方式,你可以实现多级的右键菜单。当右键点击目标元素时,菜单会在鼠标位置显示,并且可以处理菜单项的点击事件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值