ant design a-tree的搜索功能

a-tree的搜索功能,可以按照自己需求来做

<a-input-search style="margin-bottom: 8px"
                    @search="onSearch"
                    allow-clear />
    <a-tree :treeData="treeData"
            :selectedKeys.sync="selectKeys"
            @expand="onExpand"
            :expanded-keys="expandedKeys"
            :auto-expand-parent="autoExpandParent">
        <template slot-scope="item"
                  class="item-content">
          <span>{{ item }}</span>

        </template>
  </a-tree>
    
  data () {
    return {
      treeData: [],//树的data
      selectKeys: [],
      searchStr: '',
      expandedKeys: [],
      autoExpandParent: true
   }
 },
 
 methods: {
    handleNodeClick (keys, event) {
      this.$emit('select', event.node.dataRef)
    },
    onExpand (expandedKeys) {
      console.log(expandedKeys, 'expandedKeys')
      this.expandedKeys = expandedKeys
      this.autoExpandParent = false
    },
    onSearch (e) {
      if (e === '') {
        this.getDeptTree()
        return
      }
      if (e === this.searchStr) { //避免重复搜索
        return
      }
      const newData = JSON.parse(JSON.stringify(this.treeData))
      const allKey = this.getkeyList(e, newData, [])
      if (allKey.length > 0) { //有数据赋值,再将父节点展开
        newData[0].children = []
        newData[0].children = allKey
        this.treeData = newData
        this.onExpand([this.treeData[0].deptId])
      } else {
        return this.$notification['info']({ message: '没有找到" ' + e + ' "的部门' })
      }
    },
    getkeyList (value, tree, keyList) { //递归调用
      this.searchStr = value
      // 遍历所有同一级的树
      for (let i = 0; i < tree.length; i++) {
        const node = tree[i]
        // 如果该节点存在value值则push
        if (node.deptName.indexOf(value) !== -1) {
          keyList.push(node)
        }
        // 如果拥有孩子继续遍历
        if (node.children.length > 0) {
          this.getkeyList(value, node.children, keyList)
        }
      }
      // 因为是引用类型,所有每次递归操作的都是同一个数组
      return keyList
    }
  }

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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、付费专栏及课程。

余额充值