el-tree 和 el-switch 组合使用
1、效果
2、组件代码
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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | <template> <div style= "width: 370px" > <div> <el-input placeholder= "输入关键字进行过滤" v-model= "filterText" >></el-input > </div> <el-tree :data= "treeData" :props= "defaultProps" :width= "treeWidth" @node-click= "handleNodeClick" node-key= "id" :expand-on-click-node= "false" :filter-node-method= "filterNode" default -expand-all ref= "mapTree" > <span class = "custom-tree-node" slot-scope= "{ data }" > <span>{{ data.label }}</span> <span> <el- switch v-model= "data.isChecked" active-color= "#13ce66" inactive-color= "#608060" :width= "35" :active-value= "true" :inactive-value= "false" @change= "switchOpenOrClose(data)" > </el- switch > </span> </span> </el-tree> </div> </template> <script> export default { name: "SwitchTree" , props: { treeData: { type: Array, }, treeWidth: { type: Number, default : 200, }, }, data() { return { filterText: "" , defaultProps: { children: "children" , label: "label" , }, }; }, methods: { handleNodeClick() {}, switchOpenOrClose(data) { this .$emit( "openswitch" , data); this .setSwitchStatus(data.id, this .treeData, data.isChecked); }, filterNode(value, data) { if (!value) return true ; return data.label.indexOf(value) !== -1; }, setSwitchStatus(id, data, status) { let that = this ; data.forEach((x) => { if (x.id == id) { x.isChecked = status; if (x.children) { x.children.forEach((o) => { that.setSwitchStatus(o.id, x.children, status); }); } } else { if (x.children) { that.setSwitchStatus(id, x.children, status); } } }); this .setParentSwitcStatus( this .treeData[0]); }, setParentSwitcStatus(data) { let that = this ; let isChecked = true ; if (data.children) { data.children.forEach((x) => { if (!x.isChecked) { isChecked = false ; } if (x.children) { that.setParentSwitcStatus(x); } }); } data.isChecked = isChecked; }, }, watch: { filterText(val) { this .$refs.mapTree.filter(val); }, }, }; </script> <style scoped> .custom-tree-node /deep/ .el-switch__core { width: 35px; height: 15px; } .custom-tree-node /deep/ .el-switch__core::after { height: 12px; width: 12px; } .custom-tree-node { flex: 1; display: flex; align-items: center; justify-content: space-between; font-size: 14px; padding-right: 8px; } </style> |
3、使用示例
<template> <div> <switch-tree :treeData="data" @openswitch="openOrCloseSwitch" ></switch-tree> </div> </template> <script> import SwitchTree from "./SwitchTree.vue"; export default { name: "HelloWorld", components: { SwitchTree, }, props: { treeWith: { type: Number, default: 200, }, }, data() { return { filterText: "", data: [ { id: 1, label: "一级", isChecked: false, children: [ { id: 2, label: "二级 1-1", isChecked: false, children: [ { id: 3, label: "三级 1-1-1", isChecked: false, }, { id: 4, label: "三级 1-1-2", }, ], }, ], }, { id: 5, label: "一级", isChecked: true, }, ], }; }, methods: { openOrCloseSwitch(data) { if (data.isChecked) { console.log("open"); } else { console.log("close"); } }, }, }; </script> <!-- Add "scoped" attribute to limit CSS to this component only --> <style scoped> </style>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
2021-03-03 JavaScript 常用工具方法