本文實例講述了Vue使用zTree插件封裝樹組件操作。分享給大家供大家參考,具體如下:
創(chuàng)新互聯(lián)公司主營丁青網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,APP應(yīng)用開發(fā),丁青h5微信小程序開發(fā)搭建,丁青網(wǎng)站營銷推廣歡迎丁青等地區(qū)企業(yè)咨詢
1.通過npm安裝jquery
npm install jquery --save-dev
2.在build/webpack.base.conf文件當(dāng)中引入jquery
module.exports = { ... resolve: { extensions: ['.js', '.vue', '.json'], alias: { 'vue$': 'vue/dist/vue.esm.js', '@': resolve('src'), 'jquery': path.resolve(__dirname, '../node_modules/jquery/src/jquery') } }, ... }
3.在項目根目錄創(chuàng)建一個文件夾plugins,將zTree的js文件放進去
4.將zTree的樣式文件放在static文件夾下(放在別的地方大飽之后會出現(xiàn)圖片路徑找不到等問題,放在static下打包時會避免ztree樣式的壓縮)。在main.js中引入該樣式:
import '../static/zTreeStyle/zTreeStyle.css'
5.創(chuàng)建公共組件ztree>index.vue,下面為我的公共組件
html:
<template> <div class="ztree"> <el-row> <el-col :span="12"> <el-button icon="el-icon-plus" size="mini" type="text" @click="add">新增節(jié)點</el-button> </el-col> <el-col :span="12"> <el-button icon="el-icon-delete" type="text" size="mini" @click="remove">刪除節(jié)點</el-button> </el-col> </el-row> <ul id="treeDemo" class="ztree"></ul> </div> </template>
js:
<script> import '../../../plugins/zTree/jquery-1.4.4.min' import '../../../plugins/zTree/jquery.ztree.core' import '../../../plugins/zTree/jquery.ztree.excheck' import '../../../plugins/zTree/jquery.ztree.exedit' export default { props: { zNodes: { type: Array }, selectedNodeId: "" }, data() { return { selectedNode: {}, setting: { view: { showIcon: false }, edit: { enable: true, showRemoveBtn: false, showRenameBtn: false }, data: { keep: { parent: true, leaf: true }, simpleData: { enable: true } }, callback: { onClick: this.zTreeOnClick, onRename: this.zTreeOnRename } } }; }, methods: { // 初始化ztree init() { $.fn.zTree.init($("#treeDemo"), this.setting, this.zNodes); }, // 樹節(jié)點點擊事件 zTreeOnClick: function(event, treeId, treeNode) { this.selectedNode = treeNode; this.$emit("treeClick", treeNode); }, // 樹節(jié)點編輯名稱回調(diào) zTreeOnRename(event, treeId, treeNode, isCancel) { this.$emit("addNode", treeNode); }, // 點擊添加按鈕事件 add() { let newCount = 1; let zTree = $.fn.zTree.getZTreeObj("treeDemo"), nodes = zTree.getSelectedNodes(), treeNode = nodes[0]; if (treeNode) { if (treeNode.level === 2) { this.$message("分類不能超過三級"); return; } else { treeNode = zTree.addNodes(treeNode, { id: 100 + newCount, isParent: true, pId: treeNode.id, name: "new node" + newCount++ }); } } else { treeNode = zTree.addNodes(null, { id: 100 + newCount, isParent: true, pId: 0, name: "new node" + newCount++ }); } zTree.editName(treeNode[0]); }, // 點擊刪除按鈕事件 remove() { let zTree = $.fn.zTree.getZTreeObj("treeDemo"), nodes = zTree.getSelectedNodes(), treeNode = nodes[0]; if (nodes.length == 0) { this.$message("請先選擇一個節(jié)點"); return; } else { this.$confirm("此操作將永久刪除該節(jié)點, 是否繼續(xù)?", "提示", { confirmButtonText: "確定", cancelButtonText: "取消", type: "warning" }) .then(() => { this.$emit("removeNode", treeNode); }) .catch(() => {}); } } }, watch: { // 監(jiān)聽樹節(jié)點的變化更新樹 zNodes: function(val) { this.init(); }, }, mounted() { this.init(); } }; </script>
希望本文所述對大家vue.js程序設(shè)計有所幫助。
本文題目:Vue使用zTree插件封裝樹組件操作示例
本文來源:http://www.rwnh.cn/article12/jsccgc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、網(wǎng)頁設(shè)計公司、網(wǎng)站排名、全網(wǎng)營銷推廣、用戶體驗、App開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)