当前位置:   article > 正文

【Element】el-tree 树形控件

el-tree

目录

ElementUI

公共参数

基础用法

最后一层不可选择

思路一:第三层设置disable

思路二:利用样式隐藏掉第三层的选框

最后一层展示复选框

节点筛选

展开/折叠、全选/全不选、父子联动

checkbox被触发两次

ElementPlus

默认选中第一个


转载请注明:宾果的救星的博客_CSDN博客

ElementUI

公共参数

  1. treeContent:[
  2. {
  3. name: '1',
  4. id: 1,
  5. level: 1,
  6. children: [
  7. {
  8. name: '2-1',
  9. id: 21,
  10. level: 2,
  11. children: [
  12. {
  13. name: '3-1',
  14. id: 31,
  15. level: 3,
  16. children:[]
  17. },
  18. {
  19. name: '3-2',
  20. id: 32,
  21. level: 3,
  22. children:[]
  23. }
  24. ]
  25. },
  26. {
  27. name: '2-2',
  28. id: 22,
  29. level: 2,
  30. children: [
  31. {
  32. name: '3-3',
  33. id: 33,
  34. level: 3,
  35. children:[]
  36. },
  37. {
  38. name: '3-4',
  39. id: 34,
  40. level: 3,
  41. children:[]
  42. }
  43. ]
  44. }
  45. ]
  46. }
  47. ],
  48. defaultProps: {
  49. children: "children",
  50. label: "name",
  51. },

基础用法

accordion 手风琴

:highlight-current="true"  高亮

@node-click  节点被点击
@check-change  节点发生变化

  1. <el-tree
  2. accordion
  3. :data="treeContent"
  4. :highlight-current="true"
  5. :props="defaultProps"
  6. @node-click="handleNodeClick"
  7. @check-change="checkChange"
  8. ></el-tree>
  1. handleNodeClick(data) {
  2. if (data.id == 1) {
  3. // ...
  4. }
  5. },
  6. checkChange(current, isChecked) {
  7. if (isChecked) {
  8. this.$refs.Tree.setCheckedNodes([current]);
  9. }
  10. },

最后一层不可选择

思路一:第三层设置disable

单纯设置disabled无法选中。

  1. <el-tree
  2. ref="tree"
  3. :data="treeContent"
  4. show-checkbox
  5. :highlight-current="true"
  6. :props="defaultProps"
  7. ></el-tree>
  1. created() {
  2. this.dg(this.treeContent);
  3. },
  4. dg(list) {
  5. list && list.forEach((v) => {
  6. v.disabled = v.level == 3;
  7. if (v.children && v.children.length > 0) {
  8. this.dg(v.children);
  9. }
  10. });
  11. },

思路二:利用样式隐藏掉第三层的选框

  1. /deep/ .el-tree>.el-tree-node >.el-tree-node__children>.el-tree-node > .el-tree-node__children > .el-tree-node >.el-tree-node__content>label.el-checkbox {
  2. display: none!important;
  3. }

3.复选框改变的事件

  1. <el-tree
  2. ref="taskContents"
  3. :data="treeContent"
  4. show-checkbox
  5. :highlight-current="true"
  6. :props="defaultProps"
  7. @check="handleContentCheck"
  8. ></el-tree>
  1. handleContentCheck(n, obj) {
  2. console.log('n=====>', n);
  3. console.log('obj=====>', obj)
  4. let nodes = []
  5. obj.checkedNodes.forEach((v) => {
  6. if (v.level !== 3) {
  7. nodes.push(v)
  8. }
  9. });
  10. console.log('nodes =====>', nodes)
  11. },

  

n:为当前选中节点

obj:为当前树选中的所有节点

nodes:过滤掉第三层level=3 的节点

最后一层展示复选框

  1. <el-tree
  2. ref="users"
  3. :data="treeContent"
  4. show-checkbox
  5. :highlight-current="true"
  6. :props="defaultProps"
  7. @check-change="handleNodeClick"
  8. ></el-tree>
  1. created() {
  2. this.dg(this.treeContent);
  3. },
  4. dg(list) {
  5. list && list.forEach((v) => {
  6. v.disabled = v.level != 3;
  7. if (v.children && v.children.length > 0) {
  8. this.dg(v.children);
  9. }
  10. });
  11. },
  1. handleNodeClick() {
  2. let ids = this.$refs.users.getCheckedNodes(true);
  3. },

这个时候就要使用样式隐藏disable的选框了。

  1. <style lang="less" scoped>
  2. /deep/.el-checkbox__input.is-disabled {
  3. display: none;
  4. }
  5. </style>

节点筛选

expand-on-click-node="false"  点击节点不展开

default-expand-all 默认全部展开
highlight-current 高亮行

  1. <el-input v-model="name" placeholder="请输入名称" clearable/>
  2. <el-tree ref="tree"
  3. :data="treeContent"
  4. :props="defaultProps"
  5. :expand-on-click-node="false"
  6. :filter-node-method="filterNode"
  7. default-expand-all
  8. highlight-current
  9. />
  1. watch: {
  2. name(val) {
  3. this.$refs.tree.filter(val);
  4. }
  5. },
  1. // 筛选节点
  2. filterNode(value, data) {
  3. if (!value) return true;
  4. return data.name.indexOf(value) !== -1;
  5. },

展开/折叠、全选/全不选、父子联动

  1. <el-form-item label="菜单权限">
  2. <el-checkbox v-model="menuExpand" @change="handleCheckedTreeExpand($event)">展开/折叠</el-checkbox>
  3. <el-checkbox v-model="menuNodeAll" @change="handleCheckedTreeNodeAll($event)">全选/全不选</el-checkbox>
  4. <el-checkbox v-model="menuCheckStrictly" @change="handleCheckedTreeConnect($event)">父子联动</el-checkbox>
  5. <el-tree
  6. class="tree-border"
  7. ref="menu"
  8. node-key="id"
  9. :data="treeContent"
  10. :props="defaultProps"
  11. :check-strictly="!menuCheckStrictly"
  12. show-checkbox
  13. empty-text="加载中,请稍候"
  14. ></el-tree>
  15. </el-form-item>
  1. data() {
  2. return {
  3. menuExpand: false,
  4. menuNodeAll: false,
  5. menuCheckStrictly: true,
  6. }
  7. }
  1. // 展开/折叠
  2. handleCheckedTreeExpand(value) {
  3. let treeList = this.treeContent;
  4. for (let i = 0; i < treeList.length; i++) {
  5. this.$refs.menu.store.nodesMap[treeList[i].id].expanded = value;
  6. }
  7. },
  8. // 全选/全不选
  9. handleCheckedTreeNodeAll(value) {
  10. this.$refs.menu.setCheckedNodes(value ? this.treeContent: []);
  11. },
  12. // 父子联动
  13. handleCheckedTreeConnect(value) {
  14. this.menuCheckStrictly = value ? true: false;
  15. },

checkbox被触发两次

使用check-change导致的触发两次事件,可以将check-change改成check

  1. <el-tree
  2. ref="users"
  3. :data="treeContent"
  4. show-checkbox
  5. @check-change="handleNodeClick"
  6. ></el-tree>

改为

  1. <el-tree
  2. ref="users"
  3. :data="treeContent"
  4. show-checkbox
  5. @check="handleNodeClick"
  6. ></el-tree>

ElementPlus

默认选中第一个

  1. <el-tree
  2. class="filter-tree"
  3. ref="treeRef"
  4. node-key="id"
  5. :data="treeContent"
  6. :default-expand-all="true"
  7. :highlight-current="true"
  8. :expand-on-click-node="false"
  9. :filter-node-method="filterNode"
  10. @node-click="handleNodeClick"
  11. >
  12. <template #default="{ node, data }">
  13. <span class="span-ellipsis">{{ data.name }}</span>
  14. </template>
  15. </el-tree>
  1. setup(){
  2. const id = ref();
  3. const treeContent = ref();
  4. const treeRef = ref();
  5. onActivated(() => {
  6. getGroupList();
  7. });
  8. const getGroupList = () => {
  9. getTree().then((res: any) => {
  10. treeContent.value = res;
  11. if (res && res.length > 0 && res[0].children.length > 0) {
  12. id.value = res[0].children[0].id || 0
  13. nextTick(() => {
  14. treeRef.value.setCheckedKeys([id.value])
  15. treeRef.value.setCurrentKey(id.value)
  16. })
  17. }
  18. getTable();
  19. }).catch(() => {
  20. getTable();
  21. })
  22. }
  23. const filterNode = (value: any, data: any) => {
  24. if (!value) return true;
  25. return data.name.indexOf(value) !== -1;
  26. };
  27. const handleNodeClick = (data: any) => {
  28. id.value = data.id;
  29. getTable();
  30. };
  31. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/119260
推荐阅读
相关标签
  

闽ICP备14008679号