jdk1.8+vue elementui實(shí)現(xiàn)多級(jí)菜單功能
前言:在學(xué)習(xí)谷粒商城的時(shí)候,在做分類維護(hù)樹形菜單維護(hù)的功能中,項(xiàng)目中只講了菜單三級(jí)樹怎么實(shí)現(xiàn),想拓展一下多級(jí)菜單,功能已實(shí)現(xiàn),記錄一下,有不對(duì)的地方歡迎指正。
一、后端部分
使用Jdk1.8的新特性Stream和lamada表達(dá)式,數(shù)據(jù)庫的框架使用苞米豆的mybatis plus,話不多說,上代碼
1. 新建ManyTree類,可封裝成工具類
import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import java.util.List;
import java.util.Map;
public class ManyTree {
private List<CategoryEntity> rootList; // 根節(jié)點(diǎn)對(duì)象存放到這里
private List<CategoryEntity> bodyList; // 其他節(jié)點(diǎn)存放到這里,可以包含根節(jié)點(diǎn)
public ManyTree(List<CategoryEntity> rootList, List<CategoryEntity> bodyList) {
this.rootList = rootList;
this.bodyList = bodyList;
}
public List<CategoryEntity> getTree() { // 調(diào)用的方法入口
if (bodyList != null && !bodyList.isEmpty()) {
// 聲明一個(gè)map,用來過濾已操作過的數(shù)據(jù)
Map<String, String> map = Maps.newHashMapWithExpectedSize(bodyList.size());
rootList.forEach(beanTree -> getChild(beanTree, map));
return rootList;
}
return null;
}
public void getChild(CategoryEntity beanTree, Map<String, String> map) {
List<CategoryEntity> childList = Lists.newArrayList();
bodyList.stream().filter(c -> !map.containsKey(c.getCatId())).filter(c -> c.getParentCid().equals(beanTree.getCatId()))
.forEach(c -> {
map.put(String.valueOf(c.getCatId()), String.valueOf(c.getParentCid()));
getChild(c, map);
childList.add(c);
});
beanTree.setChildren(childList);
}
}
2. 新建實(shí)體CategoryEntity,這里用了lombok,idea安裝lombok插件,項(xiàng)目添加lombok的依賴,詳細(xì)自行百度
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import java.util.List;
import lombok.Data;
/**
* 商品分類
*
*/
@Data
@TableName("pms_category")
public class CategoryEntity implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 主鍵id
*/
@TableId
private Long catId;
/**
* 菜單名稱
*/
private String name;
/**
* 父級(jí)菜單ID
*/
private Long parentCid;
/**
* 層級(jí),1 2 3層
*/
private Integer catLevel;
/**
* 展示狀態(tài),可用作邏輯刪除
*/
private Integer showStatus;
/**
* 排序字段
*/
private Integer sort;
/**
* 展示圖標(biāo)
*/
private String icon;
private String productUnit;
private Integer productCount;
//這個(gè)注解的含義是在數(shù)據(jù)庫表中不存在
/**
* 用于裝載子菜單children
*/
@TableField(exist=false)
private List<CategoryEntity> children;
}
3. 業(yè)務(wù)層新建service,這里只貼service實(shí)現(xiàn)層的代碼
/**
* 遞歸查詢樹形菜單數(shù)據(jù)邏輯已經(jīng)抽取出來,
* 這里只需要傳入兩個(gè)數(shù)據(jù)集合即可:1、所有菜單數(shù)據(jù),包括根節(jié)點(diǎn)以及子節(jié)點(diǎn) 2、所有一級(jí)菜單數(shù)據(jù)
* @return
*/
@Override
public List<CategoryEntity> getAllTree() {
//使用mybatis-plus自帶的baseMapper.selectList方法查詢出所有
List<CategoryEntity> bodyList = baseMapper.selectList(null);
//使用xml查詢出所有一級(jí)菜單
List<CategoryEntity> rootList = categoryDao.getRootTree();
ManyTree utils = new ManyTree(rootList, bodyList);
List<CategoryEntity> result = utils.getTree();
return result;
}
二、前端部分
1. Category.vue
<template>
<div class>
<el-tree
:data="menus"
:props="defaultProps"
:expand-on-click-node="false"
node-key="catId"
ref="menuTree"
:show-checkbox="showCheckbox"
>
<span class="custom-tree-node" slot-scope="{ node, data }">
<span>{{ node.label }}</span>
<span>
<el-button type="text" size="mini" @click="() => append(data)">增加</el-button>
<el-button type="text" size="mini" @click="() => edit(data)">修改</el-button>
<el-button
v-if="node.childNodes.length==0"
type="text"
size="mini"
@click="() => remove(node, data)"
>刪除</el-button>
</span>
</span>
</el-tree>
</div>
</template>
<script>
//這里可以導(dǎo)入其他文件(比如:組件,工具js,第三方插件js,json文件,圖片文件等等)
//例如:import 《組件名稱》 from '《組件路徑》';
export default {
//import引入的組件需要注入到對(duì)象中才能使用
components: {},
data() {
//這里存放數(shù)據(jù)
return {
//菜單欄數(shù)據(jù)
menus: [],
defaultProps: {
//與后端實(shí)體中封裝的子節(jié)點(diǎn)名稱對(duì)應(yīng)
children: "children",
label: "name"
},
showCheckbox:true
};
},
//監(jiān)聽屬性 類似于data概念
computed: {},
//監(jiān)控data中的數(shù)據(jù)變化
watch: {},
//方法集合
methods: {
// 獲取菜單數(shù)據(jù)
getMenus() {
this.$http({
url: this.$http.adornUrl("/product/category/list/tree"),
method: "get"
}).then(({ data }) => {
//console.log("獲取菜單數(shù)據(jù)的data:" + data.data);
this.menus = data.data;
});
},
edit(data){
},
append(data) {
},
//移除節(jié)點(diǎn)方法
remove(node, data) {
}
},
//生命周期 - 創(chuàng)建完成(可以訪問當(dāng)前this實(shí)例)
created() {
this.getMenus();
},
//生命周期 - 掛載完成(可以訪問DOM元素)
mounted() {},
beforeCreate() {}, //生命周期 - 創(chuàng)建之前
beforeMount() {}, //生命周期 - 掛載之前
beforeUpdate() {}, //生命周期 - 更新之前
updated() {}, //生命周期 - 更新之后
beforeDestroy() {}, //生命周期 - 銷毀之前
destroyed() {}, //生命周期 - 銷毀完成
activated() {} //如果頁面有keep-alive緩存功能,這個(gè)函數(shù)會(huì)觸發(fā)
};
</script>
<style lang='scss' scoped>
//@import url(); 引入公共css類
</style>
2. 展示效果

三、數(shù)據(jù)庫
1. 建表sql
CREATE TABLE `pms_category` ( `cat_id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '分類id', `name` char(50) DEFAULT NULL COMMENT '分類名稱', `parent_cid` bigint(20) DEFAULT NULL COMMENT '父分類id', `cat_level` int(11) DEFAULT NULL COMMENT '層級(jí)', `show_status` tinyint(4) DEFAULT NULL COMMENT '是否顯示[0-不顯示,1顯示]', `sort` int(11) DEFAULT NULL COMMENT '排序', `icon` char(255) DEFAULT NULL COMMENT '圖標(biāo)地址', `product_unit` char(50) DEFAULT NULL COMMENT '計(jì)量單位', `product_count` int(11) DEFAULT NULL COMMENT '商品數(shù)量', PRIMARY KEY (`cat_id`) ) ENGINE=InnoDB AUTO_INCREMENT=1450 DEFAULT CHARSET=utf8mb4 COMMENT='商品分類';
2. 模擬數(shù)據(jù)
可以自己造些數(shù)據(jù),有需要的數(shù)據(jù)可以云盤拿,懶得摘了!
鏈接: https://pan.baidu.com/s/1Brt8682D3ydvorEWhgEUEA 提取碼: kkjx
到此這篇關(guān)于jdk1.8+vue elementui實(shí)現(xiàn)多級(jí)菜單功能的文章就介紹到這了,更多相關(guān)vue elementui實(shí)現(xiàn)多級(jí)菜單內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue+element+springboot實(shí)現(xiàn)文件下載進(jìn)度條展現(xiàn)功能示例
本文主要介紹了vue + element-ui + springboot 實(shí)現(xiàn)文件下載進(jìn)度條展現(xiàn)功能,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
vue中vue-router的使用說明(包括在ssr中的使用)
這篇文章主要介紹了vue中vue-router的使用說明(包括在ssr中的使用),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05
vue?scss后綴文件background-image路徑錯(cuò)誤的解決
這篇文章主要介紹了vue?scss后綴文件background-image路徑錯(cuò)誤的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
vue復(fù)雜表格單元格合并根據(jù)數(shù)據(jù)動(dòng)態(tài)合并方式
這篇文章主要介紹了vue復(fù)雜表格單元格合并根據(jù)數(shù)據(jù)動(dòng)態(tài)合并方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02
vue實(shí)現(xiàn)右鍵點(diǎn)擊彈框信息功能
這篇文章主要介紹了vue實(shí)現(xiàn)右鍵點(diǎn)擊彈框信息功能方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
一文詳解vue-router如何實(shí)現(xiàn)動(dòng)態(tài)路由
在構(gòu)建基于Vue.js的單頁面應(yīng)用(SPA)時(shí),Vue?Router是一個(gè)不可或缺的工具,本文將詳細(xì)介紹動(dòng)態(tài)路由的概念與作用及其在Vue?Router中的具體實(shí)現(xiàn),需要的可以參考下2024-11-11

