vue-treeselect组件树懒加载

            <treeselect
                :options="treeOptions"
                :normalizer="normalizer"
                :default-expand-level="0"
                noChildrenText="没有数据了"
                noOptionsText="没有数据"
                noResultsText="没有搜索结果"
                placeholder="请选择"
                @select="getSelect"
                v-model="list"
              >
              </treeselect>

:default-expand-level="0"  有多个一级,设置为0默认不展开

这里使用的是单选,只有两级并且将第二级数据禁用,点击第一级数据加载第二级 

<script>
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
export default {
  components: {
    Treeselect
  },   
  data() {
    return {
       treeOptions: [],
       list:null
    }
  },
  methods: {
    normalizer(node) {
      return {
        id: node.id,
        label: node.name,
        children:
          node.GroupList&& node.GroupList.length > 0
            ? node.GroupList
            : 0
      }
    },
    // 根据点击获取到当前节点信息    
    getSelect(node) {
      // 通过id查找子节点数据
      getInfo(node.id).then((res) => {
      res.data.GroupList.map((item) => {
        // 循环数据因为normalizer里面label设置为name
        item.name = item.NikeName
        // 将第二级数据禁用
        item.isDisabled = true
      })
      // 将当前节点id  与树结构对比通过id拿到所在数组
      const nodeToUpdate = this.findNodeById(this.treeOptions, node.id)
      if (nodeToUpdate) {
        // 更新节点对象的 videoGroupList 字段
        nodeToUpdate.GroupList= res.data.GroupList
      }
      })
    }
    findNodeById(options, id) {
      // 递归查找节点对象
      for (let i = 0; i < options.length; i++) {
        const node = options[i]
        if (node.id === id) {
          return node
        } else if (node.children) {
          const found = this.findNodeById(node.children, id)
          if (found) {
            return found
          }
        }
      }
      return null
    }
}
</script>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值