最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

jdk1.8+vue elementui實現(xiàn)多級菜單功能

 更新時間:2020年09月24日 10:09:03   作者:hxk_  
這篇文章主要介紹了jdk1.8+vue elementui實現(xiàn)多級菜單功能,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

前言:在學(xué)習(xí)谷粒商城的時候,在做分類維護(hù)樹形菜單維護(hù)的功能中,項目中只講了菜單三級樹怎么實現(xiàn),想拓展一下多級菜單,功能已實現(xiàn),記錄一下,有不對的地方歡迎指正。

一、后端部分

使用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é)點對象存放到這里
 private List<CategoryEntity> bodyList; // 其他節(jié)點存放到這里,可以包含根節(jié)點

 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()) {
   // 聲明一個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. 新建實體CategoryEntity,這里用了lombok,idea安裝lombok插件,項目添加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;
	/**
	 * 父級菜單ID
	 */
	private Long parentCid;
	/**
	 * 層級,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;
	//這個注解的含義是在數(shù)據(jù)庫表中不存在
	/**
	 * 用于裝載子菜單children
	 */
	@TableField(exist=false)
	private List<CategoryEntity> children;
}

3. 業(yè)務(wù)層新建service,這里只貼service實現(xiàn)層的代碼

 /**
  * 遞歸查詢樹形菜單數(shù)據(jù)邏輯已經(jīng)抽取出來,
  * 這里只需要傳入兩個數(shù)據(jù)集合即可:1、所有菜單數(shù)據(jù),包括根節(jié)點以及子節(jié)點 2、所有一級菜單數(shù)據(jù)
  * @return
  */
 @Override
 public List<CategoryEntity> getAllTree() {
  //使用mybatis-plus自帶的baseMapper.selectList方法查詢出所有
  List<CategoryEntity> bodyList = baseMapper.selectList(null);
  //使用xml查詢出所有一級菜單
  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引入的組件需要注入到對象中才能使用
 components: {},
 data() {
 //這里存放數(shù)據(jù)
 return {
  //菜單欄數(shù)據(jù)
  menus: [],
  defaultProps: {
		//與后端實體中封裝的子節(jié)點名稱對應(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é)點方法
 remove(node, data) { 
 }
 },
 //生命周期 - 創(chuàng)建完成(可以訪問當(dāng)前this實例)
 created() {
 this.getMenus();
 },
 //生命周期 - 掛載完成(可以訪問DOM元素)
 mounted() {},
 beforeCreate() {}, //生命周期 - 創(chuàng)建之前
 beforeMount() {}, //生命周期 - 掛載之前
 beforeUpdate() {}, //生命周期 - 更新之前
 updated() {}, //生命周期 - 更新之后
 beforeDestroy() {}, //生命周期 - 銷毀之前
 destroyed() {}, //生命周期 - 銷毀完成
 activated() {} //如果頁面有keep-alive緩存功能,這個函數(shù)會觸發(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 '層級',
 `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 '計量單位',
 `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實現(xiàn)多級菜單功能的文章就介紹到這了,更多相關(guān)vue elementui實現(xiàn)多級菜單內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue2中@hook的解析與妙用實例

    vue2中@hook的解析與妙用實例

    vue-hooks是簡化組件定義、復(fù)用狀態(tài)邏輯的一種最新嘗試,下面這篇文章主要給大家介紹了關(guān)于vue2中@hook的解析與妙用的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-03-03
  • vue+element+springboot實現(xiàn)文件下載進(jìn)度條展現(xiàn)功能示例

    vue+element+springboot實現(xiàn)文件下載進(jìn)度條展現(xiàn)功能示例

    本文主要介紹了vue + element-ui + springboot 實現(xiàn)文件下載進(jìn)度條展現(xiàn)功能,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • vue中vue-router的使用說明(包括在ssr中的使用)

    vue中vue-router的使用說明(包括在ssr中的使用)

    這篇文章主要介紹了vue中vue-router的使用說明(包括在ssr中的使用),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • vue?scss后綴文件background-image路徑錯誤的解決

    vue?scss后綴文件background-image路徑錯誤的解決

    這篇文章主要介紹了vue?scss后綴文件background-image路徑錯誤的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • vue如何在store倉庫中使用路由

    vue如何在store倉庫中使用路由

    這篇文章主要介紹了vue如何在store倉庫中使用路由,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • vue復(fù)雜表格單元格合并根據(jù)數(shù)據(jù)動態(tài)合并方式

    vue復(fù)雜表格單元格合并根據(jù)數(shù)據(jù)動態(tài)合并方式

    這篇文章主要介紹了vue復(fù)雜表格單元格合并根據(jù)數(shù)據(jù)動態(tài)合并方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • 如何使用 Vuex的入門教程

    如何使用 Vuex的入門教程

    在vue中當(dāng)我們管理數(shù)據(jù)的時候比較亂,我們要用到下面的這個庫,本文主要介紹了如何使用 Vuex的入門教程,具有一定的參考價值,感興趣的可以了解一下
    2022-02-02
  • vue實現(xiàn)右鍵點擊彈框信息功能

    vue實現(xiàn)右鍵點擊彈框信息功能

    這篇文章主要介紹了vue實現(xiàn)右鍵點擊彈框信息功能方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • vue-router之解決addRoutes使用遇到的坑

    vue-router之解決addRoutes使用遇到的坑

    這篇文章主要介紹了vue-router之解決addRoutes使用遇到的坑,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • 一文詳解vue-router如何實現(xiàn)動態(tài)路由

    一文詳解vue-router如何實現(xiàn)動態(tài)路由

    在構(gòu)建基于Vue.js的單頁面應(yīng)用(SPA)時,Vue?Router是一個不可或缺的工具,本文將詳細(xì)介紹動態(tài)路由的概念與作用及其在Vue?Router中的具體實現(xiàn),需要的可以參考下
    2024-11-11

最新評論

岑巩县| 敦化市| 内江市| 望奎县| 西乡县| 巴马| 西安市| 商南县| 阿荣旗| 抚顺市| 绥棱县| 百色市| 青州市| 盐边县| 屏山县| 兴海县| 景谷| 宜兰县| 康保县| 新竹市| 三河市| 彝良县| 西安市| 周口市| 怀集县| 嵩明县| 奈曼旗| 娱乐| 博乐市| 永泰县| 济宁市| 昌乐县| 托里县| 兴文县| 萝北县| 华宁县| 清徐县| 皋兰县| 永登县| 漳州市| 永康市|