1. 程式人生 > 其它 >穀粒商城學習——P54商品服務-API-三級分類-修改-拖拽效果

穀粒商城學習——P54商品服務-API-三級分類-修改-拖拽效果

本節講述了elementui tree 可拖拽節點的使用

通過 draggable 屬性可讓節點變為可拖拽。

allow-drop判定目標節點能否被放置。allow-drop是個函式型別的屬性

Function(draggingNode, dropNode, type)
draggingNode當前拖動的節點

dropNode要拖動到哪個節點
type引數有三種情況:'prev'、'inner' 和 'next',分別表示放置在目標節點前、插入至目標節點和放置在目標節點後

本節原始碼:測試拖動發現有點小bug,估計原視訊也有,不是大問題,學習為主就不糾結了
<template>
<div> <el-tree :data="menus" :props="defaultProps" show-checkbox="" node-key="catId" :expand-on-click-node="false" :default-expanded-keys="expandedkey" draggable :allow-drop="allowDrop" > <span class="custom-tree-node" slot-scope
="{ node, data }"> <span>{{ node.label }}</span> <span> <el-button v-if="node.level <= 2" type="text" size="mini" @click="() => append(data)" > Append </el-button
> <el-button v-if="node.level <= 2" type="text" size="mini" @click="() => edit(data)" > edit </el-button> <el-button v-if="node.childNodes.length == 0" type="text" size="mini" @click="() => remove(node, data)" > Delete </el-button> </span> </span> </el-tree> <el-dialog :title="title" :visible.sync="dialogVisible" width="30%" :close-on-click-modal="false" > <el-form :model="category"> <el-form-item label="分類名稱"> <el-input v-model="category.name" autocomplete="off"></el-input> </el-form-item> </el-form> <el-form :model="category"> <el-form-item label="圖示"> <el-input v-model="category.icon" autocomplete="off"></el-input> </el-form-item> </el-form> <el-form :model="category"> <el-form-item label="計量單位"> <el-input v-model="category.productUnit" autocomplete="off" ></el-input> </el-form-item> </el-form> <span slot="footer" class="dialog-footer"> <el-button @click="dialogVisible = false">取 消</el-button> <el-button type="primary" @click="submitData">確 定</el-button> </span> </el-dialog> </div> </template> <script> //這裡可以匯入其他檔案(比如:元件,工具js,第三方外掛js,json檔案,圖片檔案等等) //例如:import《元件名稱》from'《元件路徑》'; export default { data() { return { maxLevel: 0, title: "", dialogType: "", category: { name: "", parentCid: 0, catLevel: 0, showStatus: 1, sort: 0, catId: null, icon: "", productUnit: "", }, dialogVisible: false, expandedkey: [], menus: [], defaultProps: { children: "children", label: "name", }, }; }, created() { console.log(this.expandedkey); this.getMenus(); }, methods: { allowDrop(draggingNode, dropNode, type) { //1、被拖動的當前節點以及所在的父節點總層數不能大於3 console.log("allowDrop:", draggingNode, dropNode, type); this.countNodeLevel(draggingNode); //當前正在拖動的節點+父節點所在的深度不大於3即可 let deep = Math.abs(this.maxLevel - draggingNode.level) + 1; console.log("深度:", deep); if (type == "inner") { return deep + dropNode.level <= 3; } else { return deep + dropNode.parent.level <= 3; } }, countNodeLevel(node) { //找到所有子節點,求出最大深度 if (node.childNodes != null && node.childNodes.length > 0) { for (let i = 0; i < node.childNodes.length; i++) { if (node.childNodes[i].level > this.maxLevel) { this.maxLevel = node.childNodes[i].level; } this.countNodeLevel(node.childNodes[i]); } } }, submitData() { if (this.dialogType == "add") { this.addCategory(); } else if (this.dialogType == "edit") { this.editCategory(); } }, editCategory() { alert(this.dialogType); var { catId, name, icon, productUnit } = this.category; this.$http({ url: this.$http.adornUrl("/product/category/update"), method: "post", data: this.$http.adornData({ catId, name, icon, productUnit }, false), }).then(({ data }) => { this.$message({ message: "選單修改成功", type: "success", }); //關閉對話方塊 this.dialogVisible = false; //刷新出新的選單 this.getMenus(); //設定需要預設展開的選單 this.expandedkey = [this.category.parentCid]; }); }, edit(data) { console.log(data); this.title = "修改分類"; this.dialogType = "edit"; this.dialogVisible = true; //傳送請求獲取當前節點最新的資料 this.$http({ url: this.$http.adornUrl(`/product/category/info/${data.catId}`), method: "get", }).then(({ data }) => { //請求成功 console.log("要回顯的資料", data); this.category.name = data.data.name; this.category.catId = data.data.catId; this.category.icon = data.data.icon; this.category.productUnit = data.data.productUnit; this.category.parentCid = data.data.parentCid; this.category.catLevel = data.data.catLevel; this.category.sort = data.data.sort; this.category.showStatus = data.data.showStatus; }); }, addCategory() { this.$http({ url: this.$http.adornUrl("/product/category/save"), method: "post", data: this.$http.adornData(this.category, false), }).then(({ data }) => { this.$message({ message: "選單儲存成功", type: "success", }); //關閉對話方塊 this.dialogVisible = false; //刷新出新的選單 this.getMenus(); //設定需要預設展開的選單 this.expandedkey = [this.category.parentCid]; }); }, remove(node, data) { let ids = [data.catId]; console.log(ids); console.log(node); this.$confirm(`是否刪除${data.name}選單?`, "提示", { confirmButtonText: "確定", cancelButtonText: "取消", type: "warning", }) .then(() => { this.$http({ url: this.$http.adornUrl("/product/category/delete"), method: "post", data: this.$http.adornData(ids, false), }).then(({ data }) => { this.$message({ message: "選單刪除成功", type: "success", }); this.getMenus(); console.log(node.parent.data.catId); this.expandedkey = [node.parent.data.catId]; }); }) .catch(() => {}); }, append(data) { this.dialogType = "add"; this.title = "新增分類"; this.dialogVisible = true; this.category.parentCid = data.catId; this.category.catLevel = data.catLevel * 1 + 1; this.category.catId = null; this.category.name = ""; this.category.icon = ""; this.category.productUnit = ""; this.category.sort = 0; this.category.showStatus = 1; }, getMenus() { this.$http({ url: this.$http.adornUrl("/product/category/list/tree"), method: "get", }).then(({ data }) => { console.log("成功獲取選單", data.data); this.menus = data.data; console.log("thismenus", this.menus); }); }, }, }; </script> <stylescoped> </style>
View Code