如何在VUE框架中使用ztree外掛
阿新 • • 發佈:2018-12-25
一切還是以需求為導向!如下圖所示:這是要求作出的效果。經過我綜合考慮發現ztree外掛與需求契合度比較高,所以果斷的使用了該外掛進行開發。簡單說下需求:管理目錄層級有四個子選單,可以新建父目錄和子目錄以及對目錄進行相關編輯操作。
相信大家遇到的第一個問題就是如何使用ztree外掛,沒錯!那自然要跑到ztree官網去看文件,熟讀文件是必不可少的!讀完文件後,那怎麼在vue框架中配置ztree外掛呢?網上的相關文章很少,這就需要自己摸索了。不過我已經踩過不少坑,童鞋拿去用即可。
下面開始貼程式碼:
html:
<el-dropdown @command="handleCommand"> <el-button type="primary"> 管理目錄層級<i class="el-icon-arrow-down el-icon--right"></i> </el-button> <el-dropdown-menu slot="dropdown" style="width:145px;top: 189px;vertical-align: top;"> <el-dropdown-item command="1">新建目錄</el-dropdown-item> <el-dropdown-item command="2" :disabled="disabledMuse">新建子目錄</el-dropdown-item> <el-dropdown-item command="3" :disabled="disabledMuse">修改</el-dropdown-item> <el-dropdown-item command="4" :disabled="disabledMuse">刪除</el-dropdown-item> </el-dropdown-menu> </el-dropdown> // ztree主體 <div class="personal_library"> <div class="seachVal"> <el-input v-model="inputSeach" id="inputSeachs" placeholder="請輸入關鍵字..." @focus="inputFocus" @blur="inputBlur" ></el-input> <i class="el-icon-search" :class="{ iconSeach1 : isShowSeach1}"></i> <i class="el-icon-close" :class="{ iconSeach2 : isShowSeach2}" @click="clearInput"></i> <i class="el-icon-plus" :class="{ iconNone1 : isShowicon1}" @click="expandAndCloseNode(true)"></i> <i class="el-icon-minus" :class="{ iconNone2 : isShowicon2}" @click="expandAndCloseNode(false)"></i> </div> <div class="folderMain"> <p class="tipFont" :class="{ tipShow: activeTip1 }">請點選“管理目錄層級—新建目錄”</p> <ul id="tree" class="ztree" :class="{ ulShow: activeTip2 }"></ul> </div> </div> new Vue({ el: '#appSetFast', data: function () { return { // ztree配置 setting: { data: { simpleData: { enable: true, idKey: "id", pIdKey: "fast_id", rootPId: 0 } }, view: { showLine: false, dblClickExpand: false, addDiyDom: this.addDiyDom, nameIsHTML: true, selectedMulti: false }, callback: { onClick: this.onNodeClick, beforeRename: this.zTreeBeforeRename, } }, zNodes: [] }, methods: { // 點選管理目錄 handleCommand: function (command) { if (command == 1) { this.addFolder(1); } else if (command == 2) { this.addFolder(2); } else if (command == 3) { this.editNode(); } else if (command == 4) { this.removeNode(); } }, // 獲取目錄 freshArea: function () { $.ajax({ type: 'post', url: '/api/get_fast', processData: false, contentType: false, success: res => { this.zNodes = res.data $.fn.zTree.init($("#tree"), this.setting, this.zNodes); if (!this.zNodes) { return } this.zNodesData(); this.selectNodes(this.nodesIds) // 初始化模糊搜尋方法 fuzzySearch('tree', '#inputSeachs', true, true) }, error: () => { this.tipMsg('目錄獲取失敗!', 'error') } }) }, // 通過判斷陣列有沒有位元組點來顯示按鈕和提示 zNodesData: function () { if (this.zNodes.length) { this.activeTip1 = true; this.activeTip2 = false; this.disabledMuse = false; } else { this.activeTip1 = false; this.activeTip2 = true; this.disabledMuse = true; } }, // 單擊選中目錄並獲取快捷回覆條目數 onNodeClick: function (e, treeId, treeNode) { var zTree = $.fn.zTree.getZTreeObj("tree"), nodes = zTree.getSelectedNodes(), treeNode = nodes[0]; console.log(treeNode, 44) if(treeNode.oldname){ this.nodeNames = treeNode.oldname }else{ this.nodeNames = treeNode.name } this.nodesIds = treeNode.id; this.fastreplyId = treeNode.fastreply_id; this.getFilePath(treeNode); $.ajax({ type: 'get', url: get_fast', data: {'id': this.nodesIds}, success: res => { this.dataTable = res.data this.currentPage = 1 this.userListDataLimit(this.dataTable, this.currentPage); this.userTotal = this.dataTable.length; }, error: () => { this.tipMsg('獲取回覆內容失敗!', 'error') } }) }, // 麵包屑 getFilePath: function (treeObj) { if (treeObj == null)return ''; console.log(treeObj, 33) if(treeObj.oldname){ var filename = treeObj.oldname }else { var filename = treeObj.name } if (filename.length > 12) { filename = filename.substring(0, 12) + "..." } var pNode = treeObj.getParentNode(); if (pNode != null) { filename = this.getFilePath(pNode) + " / " + filename; } this.breadcrumbArr = filename; return this.breadcrumbArr; }, // 搜尋框聚焦事件 inputFocus: function () { $('#inputSeachs').attr('placeholder', '') }, // 搜尋框失焦事件 inputBlur: function () { if (this.inputSeach == '') { $('#inputSeachs').attr('placeholder', '請輸入關鍵字...') } }, // 清楚搜尋框 clearInput: function () { this.inputSeach = ''; $.fn.zTree.init($("#tree"), this.setting, this.zNodes); }, // 新建目錄 addFolder: function (e) { var zTree = $.fn.zTree.getZTreeObj("tree"), nodes = zTree.getSelectedNodes(), treeNode = nodes[0]; this.newCountNode = "新建目錄" + (this.newCount++); if (treeNode) { var parentNode = treeNode.getParentNode(); // 選中節點的條件下新建同級目錄 console.log(treeNode, 22) if (e == 1) { if (treeNode.getParentNode()) { this.parentNodeId = treeNode.getParentNode().id $.ajax({ type: 'post', url: 'add_fast', data: {'fastreply_id': parseInt(this.parentNodeId), 'name': this.newCountNode}, success: data => { treeNode = zTree.addNodes(parentNode, { id: data.id, fastreply_id: data.fastreply_id, name: data.name, len: data.len }) }, error: () => { this.tipMsg('新建目錄失敗1!', 'error') } }) } else { $.ajax({ type: 'post', url: '/api/add_fast', data: {'name': this.newCountNode}, success: data => { treeNode = zTree.addNodes(null, { id: data.id, fastreply_id: data.fastreply_id, name: data.name, len: data.len }) }, error: () => { this.tipMsg('新建目錄失敗2!', 'error') } }) } // 新建子目錄 } else if (e == 2) { console.log(treeNode, 66) $.ajax({ type: 'post', url:'/api/add_fast', data: {'fastreply_id': treeNode.id, 'name': this.newCountNode}, success: data => { console.log(treeNode, 77) treeNode = zTree.addNodes(treeNode, { id: data.id, fastreply_id: data.fastreply_id, name: data.name, len: data.len }) console.log(treeNode, 99) console.log(this.zNodes, 88) this.freshArea() }, error: () => { this.tipMsg('新建目錄失敗3!', 'error') } }) } // 新建根目錄 } else if (e != 2) { $.ajax({ type: 'post', url:'/api/add_fast', data: {'name': this.newCountNode}, success: data => { this.activeTip1 = true; this.activeTip2 = false; this.disabledMuse = false; console.log(data, 322) treeNode = zTree.addNodes(null, { id: data.id, fastreply_id: data.fastreply_id, name: data.name, len: data.len }) }, error: () => { this.tipMsg('新建目錄失敗!', 'error') } }) } else if (e == 2) { this.tipMsg('請新建目錄才能新建子目錄!', 'warning'); } }, // 修改名稱 editNode: function () { var zTree = $.fn.zTree.getZTreeObj("tree"), nodes = zTree.getSelectedNodes(), treeNode = nodes[0]; if (nodes.length == 0) { this.tipMsg('請選擇要修改的目錄!', 'warning') return } this.nodesNames = treeNode.name; if(treeNode.oldname){ treeNode.name = treeNode.oldname } zTree.editName(treeNode) }, zTreeBeforeRename(treeId, treeNode, newName, isCancel) { if (this.nodesNames != newName) { $.ajax({ type: 'post', url:'/api/set_fast', data: {'id': treeNode.id, 'name': newName}, success: res => { this.freshArea() }, error: () => { this.tipMsg('名稱修改失敗!', 'error') } }) } }, // 新增條目數 addDiyDom: function (treeId, treeNode) { var aObj = $("#" + treeNode.tId + this.IDMark_A); var editStr = "<span id='diyBtn_" + treeNode.id + "'>(" + treeNode.len + ")</span>"; aObj.after(editStr); // 顯示省略號 var spantxt = $("#" + treeNode.tId + "_span").html(); if (spantxt.length > 12) { spantxt = spantxt.substring(0, 12) + "..."; $("#" + treeNode.tId + "_span").html(spantxt); } }, // 展開或關閉所有節點 expandAndCloseNode: function (e) { var zTree = $.fn.zTree.getZTreeObj("tree"); if (e) { this.isShowicon2 = false; this.isShowicon1 = true; } else { this.isShowicon2 = true; this.isShowicon1 = false; } zTree.expandAll(e); }, // 提示 tipMsg: function (msg, type) { this.$message({ message: msg, type: type }); }, // 刪除選中的節點 removeNode: function () { var zTree = $.fn.zTree.getZTreeObj("tree"), nodes = zTree.getSelectedNodes(); if (nodes.length == 0) { this.tipMsg('請選擇要刪除的目錄!', 'warning') return } else { this.delTip = '您是否確認要刪除當前目錄'; this.activeShadow1 = false; this.activeShadow2 = true; this.activeShadow3 = false; this.flagSure = 1; } }, // 刪除框確認按鈕 sureRemove: function () { if (this.flagSure == 1) { var zTree = $.fn.zTree.getZTreeObj("tree"), nodes = zTree.getSelectedNodes(), treeNode = nodes[0]; $.ajax({ type: 'post', url: '/api/del_fast', data: {'id': treeNode.id}, success: data => { zTree.removeNode(treeNode); this.activeShadow1 = true; this.activeShadow3 = true; this.breadcrumbArr = '' this.freshArea() }, error: () => { this.tipMsg('刪除失敗!', 'error') } }) } else if (this.flagSure == 2) { $.ajax({ type: 'post', url: '/api/del_fast', data: {'id': this.listId}, success: data => { this.activeShadow1 = true; this.activeShadow3 = true; this.freshArea() this.onNodeClick() }, error: () => { this.tipMsg('刪除失敗!', 'error') } }) } else if (this.flagSure == 3) { this.activeShadow1 = true; this.activeShadow3 = true; $('.uploadPortrait').click() } }, // 獲取資料後根據id重新選中節點 selectNodes: function (id) { var zTree = $.fn.zTree.getZTreeObj("tree"), nodes = zTree.transformToArray(zTree.getNodes()); for(var i=0; i<nodes.length; i++){ if(id == nodes[i].id){ zTree.selectNode(nodes[i], true) if(nodes[i].fastreply_id){ zTree.expandNode(nodes[i], true, false) } } } },
上面每一個方法都有註釋,一看便知。如果要做搜尋功能的童鞋需要注意引入 fuzzySearch(‘tree’, ‘#inputSeachs’, true, true)外掛,這個官網的demo有很詳細的介紹,下載原始碼就可。如果每個層級目錄需要新增自己的內容,可以仔細看下上面這個addDiyDom() 函式,使用的相關方法一定要在data中的setting:{}中配置,否則不會生效。ztree外掛是我使用過最完美的外掛之一,在此感謝作者開發這麼強大的外掛!
作者:愛吃漁的熊
來源:CSDN
原文:https://blog.csdn.net/gaoxin666/article/details/82859355
版權宣告:本文為博主原創文章,轉載請附上博文連結!