本文实例为大家分享了Vue自定义树形控件的使用方法,供大家参考,具体内容如下
效果图:
数据结构:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | tree: { title: '' , // 标题(姓名) key: '0' , head: '' , // 头像 selectStatus: false , // checkBox选中状态 children: [ { title: '旺旺一部' , key: '0-0' , head: '' , selectStatus: false , children: [ { key: '0-0-0' , title: '旺仔1' , head: require( '@/assets/wan.jpg' ), selectStatus: false } ] }, { title: '旺旺二部' , key: '0-1' , head: '' , selectStatus: false , children: [ { title: '旺旺二部一队' , key: '0-1-0' , head: '' , selectStatus: false , children: [ { title: '旺旺二部一队一班' , key: '0-1-0-2' , head: '' , selectStatus: false , children: [ { title: '旺仔3' , key: '0-1-0-2-0' , head: require( '@/assets/wan.jpg' ), selectStatus: false } ] } ] } ] } ] }, |
思路:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | /*自定义树形控件的核心就是“组件自己调用自己” 这里将树形控件封装成一个子组件*/ < template > < div > < div class = "tree-custom" > < div :style = "indent" @ click = "toggleChildren" > //toggleChildren事件为“展开内容”、“关闭内容”的控制事件 /* 这里是递归数据显示的具体内容 例如:本项目递归的具体内容从效果图上看就是“图片/头像”、“标题/名字”、“null/CheckBox” 效果图显示逻辑是: < div v-if = "!headImg && label" > //如果没有头像图片有标题,则显示 “箭头-标题”样式 </ div > < div v-if = "headImg" > //如果有头像图片,则显示 “头像-姓名-checkBox”样式 </ div > */ </ div > < tree-custom // “自己调用自己” :key = "children.key" // key值唯一 v-for = "children in treeData" v-if = "showChildren" // 根据 toggleChildren事件 判断是否展开内容 :treeData = "children.children" // 下面都是一些属性,应该都能看懂吧!不多说了! :label = "children.title" :headImg = "children.head" :pkid = "children.key" :depth = "depth+1" // 这个是用来控制每行缩进的样式,可移步下方=>indent ()看具体用处 :selectStatus="children.selectStatus" v-bind="$attrs" // 这两个是用来实现祖孙组件通信的 v-on="$listeners" > </ tree-custom > </ div > </ div > </ template > < script > export default { name: 'TreeCustom', // 要给我们的组件一个名字!不然怎么调用呢 data () { return { showChildren: true, // 这个就是控制是否显示内容的data~也就是展开和收起! currentInfoData: {} // 这个的用处是获取当前行的数据,为了简洁在上方代码的具体用处已经被我删掉了~意义不大 } }, //对象的默认值应由一个工厂函数返回,避免采坑 props: { treeData: { type: Array, default: () => [] }, label: { type: String, default: () => '' }, depth: { type: Number, default: () => 0 }, headImg: { type: String, default: () => '' }, pkid: { type: String, default: () => '' }, selectStatus: { type: Boolean, default: () => null } }, computed: { indent () { // 定义不同层级的缩进样式 return { transform: `translate(${(this.depth - 1) * 15}px)` } } }, methods: { toggleChildren () { this.showChildren = !this.showChildren }, checkBoxSelectChange (e) { const checked = e.target.checked if (checked) { //使用$listeners方法调用祖辈的函数,因为这边是递归组件所以组件之间可能并不是严格的父子关系,所以$emit、$parent等方法都是不合适的 this.$listeners.addSelectedData(this.currentInfoData) } if (!checked) { this.$listeners.deleteSelectedData(this.currentInfoData) } }, getCurrentInfo (label, headImg, pkid) { this.currentInfoData = { key: pkid, title: label, head: headImg } } } } </ script > |
1 2 3 4 5 6 7 8 9 10 11 12 | /*组件调用方法*/ < div class = "tree-scroll" > < tree-custom :label = "tree.title" :headImg = "tree.head" :treeData = "tree.children" :pkid = "tree.key" :depth = "0" :selectStatus = "tree.selectStatus" @ addSelectedData = "addSelectedData" @ deleteSelectedData = "deleteSelectedData" /> </ div > |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自学编程网。
- 本文固定链接: https://zxbcw.cn/post/219824/
- 转载请注明:必须在正文中标注并保留原文链接
- QQ群: PHP高手阵营官方总群(344148542)
- QQ群: Yii2.0开发(304864863)