jdk1.8+vue elementui實(shí)現(xiàn)多級(jí)菜單功能
前言:在學(xué)習(xí)谷粒商城的時(shí)候,在做分類(lèi)維護(hù)樹(shù)形菜單維護(hù)的功能中,項(xiàng)目中只講了菜單三級(jí)樹(shù)怎么實(shí)現(xiàn),想拓展一下多級(jí)菜單,功能已實(shí)現(xiàn),記錄一下,有不對(duì)的地方歡迎指正。
一、后端部分
使用Jdk1.8的新特性Stream和lamada表達(dá)式,數(shù)據(jù)庫(kù)的框架使用苞米豆的mybatis plus,話(huà)不多說(shuō),上代碼
1. 新建ManyTree類(lèi),可封裝成工具類(lèi)
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,用來(lái)過(guò)濾已操作過(guò)的數(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的依賴(lài),詳細(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;
/**
* 商品分類(lèi)
*
*/
@Data
@TableName("pms_category")
public class CategoryEntity implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 主鍵id
*/
@TableId
private Long catId;
/**
* 菜單名稱(chēng)
*/
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ù)庫(kù)表中不存在
/**
* 用于裝載子菜單children
*/
@TableField(exist=false)
private List<CategoryEntity> children;
}
3. 業(yè)務(wù)層新建service,這里只貼service實(shí)現(xiàn)層的代碼
/**
* 遞歸查詢(xún)樹(shù)形菜單數(shù)據(jù)邏輯已經(jīng)抽取出來(lái),
* 這里只需要傳入兩個(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方法查詢(xún)出所有
List<CategoryEntity> bodyList = baseMapper.selectList(null);
//使用xml查詢(xún)出所有一級(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 《組件名稱(chēng)》 from '《組件路徑》';
export default {
//import引入的組件需要注入到對(duì)象中才能使用
components: {},
data() {
//這里存放數(shù)據(jù)
return {
//菜單欄數(shù)據(jù)
menus: [],
defaultProps: {
//與后端實(shí)體中封裝的子節(jié)點(diǎn)名稱(chēng)對(duì)應(yīng)
children: "children",
label: "name"
},
showCheckbox:true
};
},
//監(jiān)聽(tīng)屬性 類(lèi)似于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)建完成(可以訪問(wèn)當(dāng)前this實(shí)例)
created() {
this.getMenus();
},
//生命周期 - 掛載完成(可以訪問(wèn)DOM元素)
mounted() {},
beforeCreate() {}, //生命周期 - 創(chuàng)建之前
beforeMount() {}, //生命周期 - 掛載之前
beforeUpdate() {}, //生命周期 - 更新之前
updated() {}, //生命周期 - 更新之后
beforeDestroy() {}, //生命周期 - 銷(xiāo)毀之前
destroyed() {}, //生命周期 - 銷(xiāo)毀完成
activated() {} //如果頁(yè)面有keep-alive緩存功能,這個(gè)函數(shù)會(huì)觸發(fā)
};
</script>
<style lang='scss' scoped>
//@import url(); 引入公共css類(lèi)
</style>
2. 展示效果

三、數(shù)據(jù)庫(kù)
1. 建表sql
CREATE TABLE `pms_category` ( `cat_id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '分類(lèi)id', `name` char(50) DEFAULT NULL COMMENT '分類(lèi)名稱(chēng)', `parent_cid` bigint(20) DEFAULT NULL COMMENT '父分類(lèi)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='商品分類(lèi)';
2. 模擬數(shù)據(jù)
可以自己造些數(shù)據(jù),有需要的數(shù)據(jù)可以云盤(pán)拿,懶得摘了!
鏈接: 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)文章
Vuex結(jié)合storage實(shí)現(xiàn)用戶(hù)信息本地存儲(chǔ)方式
這篇文章主要介紹了Vuex結(jié)合storage實(shí)現(xiàn)用戶(hù)信息本地存儲(chǔ)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
Vue中addEventListener()?監(jiān)聽(tīng)事件案例講解
這篇文章主要介紹了Vue中addEventListener()?監(jiān)聽(tīng)事件案例講解,包括語(yǔ)法講解和事件冒泡或事件捕獲的相關(guān)知識(shí),本文結(jié)合示例代碼給大家講解的非常詳細(xì),需要的朋友可以參考下2022-12-12
Vue v-for中:key中item.id與Index使用的區(qū)別解析
這篇文章主要介紹了Vue v-for中:key中item.id與Index使用的區(qū)別解析,推薦使用【:key="item.id"】而不是將數(shù)組下標(biāo)當(dāng)做唯一標(biāo)識(shí),前者能做到全部復(fù)用,本文給大家詳細(xì)講解,感興趣的朋友跟隨小編一起看看吧2024-02-02
詳解vue.js數(shù)據(jù)傳遞以及數(shù)據(jù)分發(fā)slot
本篇文章給大家通過(guò)代碼實(shí)例分析了vue.js數(shù)據(jù)傳遞以及數(shù)據(jù)分發(fā)slot的相關(guān)知識(shí),有這方面興趣的朋友參考下吧。2018-01-01
vue-cli3 配置開(kāi)發(fā)與測(cè)試環(huán)境詳解
這篇文章主要介紹了vue-cli3 配置開(kāi)發(fā)與測(cè)試環(huán)境詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-05-05
Vue使用自定義指令打開(kāi)dialog的實(shí)現(xiàn)方法
在web后臺(tái)管理項(xiàng)目中,經(jīng)常要用到dialog,就vue來(lái)說(shuō),使用方式則是引入組件,注冊(cè),在template中使用,試想一下,如果我們需要在項(xiàng)目中的不同.vue文件中使用該dialog,但是又不想每次都在template中寫(xiě)入組件該如何實(shí)現(xiàn)呢?本文我們介紹用指令控制dialog,需要的朋友可以參考下2024-07-07

