ant design vue的使用过程,遇到问题记录

1、异步TreeSelect 树型选择控件,回显问题

解决思路,记录各个节点的分支的所有ID,从第一层开始遍历,判断是否包含选中的ID,不断补充children

代码如下

<a-form-model-item  label="中图法分类号">
          <a-tree-select
            ref="tree_select"
            :replaceFields="replaceFields"
            v-model="form.category"
            style="width: 100%"
            multiple
            :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
            :tree-data="treeData"
            placeholder=""
            :load-data="onLoadData"
          />
        </a-form-model-item>

js部分,定义data

<script>
import { materialType, materialStatus } from '@/config/database'
import { getCategoryList, getCategorySingle } from '@/api/category'
export default {
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    loading: {
      type: Boolean,
      default: () => false
    },
    model: {
      type: Object,
      default: () => null
    }
  },
  data () {
    return {
      ids: [],
      category: [],
      replaceFields: {
        children: 'children',
        title: 'name',
        key: '_id',
        value: '_id'
      },
      treeData: [
      ],
      materialType: materialType,
      materialStatus: materialStatus,
      labelCol: { span: 4 },
      wrapperCol: { span: 14 },
      other: '',
      form: {
      },
      rules: {
        originalId: [ { required: true, message: '原始Id不能为空!', trigger: 'change' } ],
        name: [{ required: true, message: '素材名称不能为空!', trigger: 'change' }],
        url: [{ required: true, message: '存储位置不能为空!', trigger: 'change' }],
        type: [{ required: true, message: '类型不能为空!', trigger: 'change' }],
        status: [{ required: true, message: '状态不能为空!', trigger: 'change' }]
      }
      // form: this.$form.createForm(this)
    }
  },
  watch: {
    value (value) {
      console.log(value)
    }
  },
  async created () {
    this.form = { ...this.model }
    this.initTreeData()
    // 初始化树形数据
    this.treeData = await this.getCategoryList(null)
    // 当 model 发生改变时,为表单设置值
    this.$watch('model', () => {
      this.form = { ...this.model }
      this.initTreeData()
    })
  },
  methods: {
    async initTreeData () {
      this.ids = []
      if (this.form.category && this.form.category.length > 0) {
        for (let i = 0; i < this.form.category.length; i++) {
          await this.getCategoryPID(this.form.category[i])
        }
        if (this.ids.length > 0) {
          var mySet = new Set(this.ids)
          this.ids = [ ...mySet ]
          // 处理完后加载数据
          this.calTree(this.treeData)
        }
      }
    },
    async calTree (dataArray) {
      for (let i = 0; i < dataArray.length; i++) {
        if (this.ids.indexOf(dataArray[i]._id) >= 0) {
          // 主动加载
          const res = await getCategoryList({
            query: { parent: dataArray[i]._id, status: 'ENABLED' }
          })
          this.$set(dataArray[i], 'children', res.data.rows)
          if (res.data.rows.length > 0) {
            this.calTree(dataArray[i].children)
          }
        }
      }
    },
    // 不断获得上一级的pid
    async getCategoryPID (_id) {
      const res = await getCategorySingle({
        query: {
          _id: _id
        }
      })
      this.ids.push(res.data.row._id)
      if (res.data.row.parent) {
        await this.getCategoryPID(res.data.row.parent)
      }
    },
    // 通过PID查询树形列表的内容
    async getCategoryList (pid) {
      const res = await getCategoryList({
        query: { parent: pid, status: 'ENABLED' }
      })
      return res.data.rows
    },
    onLoadData (treeNode) {
      return new Promise(resolve => {
        const { _id } = treeNode.dataRef
        if (this.ids.indexOf(_id) >= 0) {
          // 存在,不重新查该children了
          resolve()
        } else {
          setTimeout(async () => {
            const data = await this.getCategoryList(_id)
            this.$set(treeNode.dataRef, 'children', data)
            resolve()
          }, 300)
        }
      })
    }
  }
}
</script>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值