Vue使用Ant design中a-tree的scopedSlots

Vue使用Ant design中a-tree的scopedSlots实现树标题的前后都有icon图标

在这里插入图片描述

<a-tree style="width:100%" :tree-data="treeData" show-icon :default-selected-keys="[treeData[0].key]">
	<!-- 每项 最前面的 箭头的 icon -->
    <a-icon slot="switcherIcon" type="caret-down" />
    <!-- slot="praent" 配合 treeData数据中的 slots 设置目录下 的 icon -->
    <a-icon slot="praent" style="color:#0DD3FF" type="folder-open" />
    <!-- slot="child" 配合 treeData数据children下的 slots 设置children目录 的 icon -->
    <a-icon slot="child" style="color:#FED037" type="folder-open" />
    <!-- 数据中 每个 节点 的 scopedSlots 字段(插槽) 用来设置 所有(子父节点)节点  的 统一的 icon-->
    <template slot="handle" slot-scope="item">
       <!--item.title 为treeData的 title 字段-->
       <span>{{item.title}}</span>
       <a-icon 
         style="position:absolute;right: 0;" 
         @click.stop="treeHandleClick" 
         type="ellipsis" />
     </template>
</a-tree>
<script>
const treeData = [
  {
    title: 'parent 1',
    key: '0-0',
    slots: {icon: 'praent',},
    // ⚠️重点这这里⚠️每一条数据上都添加scopedSlots属性
    scopedSlots:{title:"handle"},
    children: [
      { 
        title: 'leaf', 
        key: '0-0-0', 
        slots: { icon: 'child' },
        scopedSlots:{title:"handle"},
        children: [
          { 
            title: 'leaf', 
            key: '0-0-0-0', 
            slots: { icon: 'child' } ,
            scopedSlots:{title:"handle"},
          },
        ]
      },
    ],
  },
];
</script>

剩下的 样式 根据需求 自己调试
有更好的 方法 欢迎探讨
如果后台数据和 a-tree组件 的 字段对不上 可以用replaceFields
在这里插入图片描述

Ant Design Vue 的 A-Tree 组件提供了一个 `checkable` 属性来实现全选功能。通过将 `checkable` 属性设置为 true,将会在每个节点旁边显示一个复选框,用户可以通过勾选这些复选框来选择节点。 如果您想要实现全选功能,可以在 A-Tree 的顶层节点上添加一个全选的复选框,并通过监听 `on-check` 事件来控制所有节点的选状态。具体实现方法如下: 1. 在 A-Tree 的根节点上添加一个全选的复选框: ```html <a-tree :checkable="true" :checked-keys="checkedKeys" @check="handleCheck"> <a-tree-node :title="rootTitle" :key="rootKey"> <!-- 全选复选框 --> <template slot="title"> <a-checkbox v-model="allChecked">全选</a-checkbox> {{ rootTitle }} </template> <!-- 树节点 --> <a-tree-node v-for="node in nodes" :title="node.title" :key="node.key"></a-tree-node> </a-tree-node> </a-tree> ``` 2. 在 Vue 实例定义 `allChecked` 和 `checkedKeys` 变量,并在 `handleCheck` 方法更新所有节点的选状态: ```js data() { return { allChecked: false, // 全选状态 checkedKeys: [], // 选的节点 key nodes: [...] // 树节点数据 } }, methods: { handleCheck(checkedKeys) { // 更新选状态 this.checkedKeys = checkedKeys; // 如果全选复选框被勾选,则选所有节点;否则取消所有节点的选状态 if (this.allChecked) { this.$refs.tree.setCheckedKeys(this.nodes.map(node => node.key)); } else { this.$refs.tree.setCheckedKeys([]); } } } ``` 3. 在 A-Tree 上添加一个 `ref` 属性,以便在 Vue 实例引用该组件: ```html <a-tree ref="tree" ...> ``` 现在,您可以通过勾选顶层树节点旁边的全选复选框来实现 A-Tree 的全选功能了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值