1、:ref="'treeselect' + scope.$index" 此列为必填项,有校验;

2、:options="deptOptions" 就是绑树数据 ;

3、 :normalizer="normalizer" 把后端返的格式换成树自己的格式;

4、:load-options="loadOptions"懒加载方法;

5、 @select="node => treeHandleSelect(scope.$index, node)" 点击树节点把id和code传给后端;

6、option-label插槽是显示下拉的内容,用treeselect自己的,通过node赋值及回显 ;

7、value-label插槽是显示文本框的内容,要用el-table的scope插槽赋值及回显;

8、要引入LOAD_CHILDREN_OPTIONS;

import { LOAD_CHILDREN_OPTIONS } from ‘@riophae/vue-treeselect’

<TreeSelect
                  :ref="'treeselect' + scope.$index"
                  v-model="scope.row.assetTypeCode"
                  :options="deptOptions"
                  :normalizer="normalizer"
                  :load-options="loadOptions"
                  clearable
                  no-options-text="暂无可用选项"
                  noChildrenText="暂无数据"
                  noResultsText="暂无匹配项"
                  loadingText="数据加载中"
                  :matchKeys="['name', 'code']"
                  no-results-text="没有匹配的搜索结构"
                  placeholder="请选择资产分类名称"
                  style="width: 100%"
                  :appendToBody="true"
                  
                  @select="node => treeHandleSelect(scope.$index, node)"
                >
                  <div slot="option-label" slot-scope="{ node }" :style="{ marginLeft: !node.raw.children ? '16px' : '0' }">
                    [{{ node.raw.code }}]{{ node.raw.name }}
                  </div>
                  <div slot="value-label" slot-scope="{ node }">
                    {{ scope.row.assetTypeCode ? `[${scope.row.assetTypeCode}]` : '' }}{{ scope.row.assetTypeName }}
                  </div>
                </TreeSelect>

格式化:

 normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children
      }
      return {
        id: node.code,
        label: node.name,
        children: node.children,
      }
    },

初始化的时候,先调树接口,拿到根节点

// 初始化数据
    initData() {
     //什么都不传,拿到根节点     
      lazyTreeselect().then(res => {      
        for (let index of res.data) {
          let modeInfo = {}
          modeInfo.id = index.id
          modeInfo.code = index.code
          modeInfo.label = index.name
          modeInfo.name = index.name
          modeInfo.children = null
          this.deptOptions.push(modeInfo)
        }      
      })     
    },

点击的时候拿到子节点,这时要用到loadOptions方法


  // 树懒加载
    loadOptions({ action, parentNode, callback }) {    
      if (action === LOAD_CHILDREN_OPTIONS) {
        //加载点击节点的子节点,parentNode.id即父节点,
        //还是调树接口,传id进去
        lazyTreeselect({ id: parentNode.id }).then(response => {
          let arr = []
          for (let index of response.data) {
            let chiledModeInfo = {}
            chiledModeInfo.id = index.id
            chiledModeInfo.code = index.code
            chiledModeInfo.label = index.name
            chiledModeInfo.name = index.name
            chiledModeInfo.children = null
             //后端返的接口里正常都会有个类似于isLeaf/hasLaef字段,末级为1或者true
            //如果没有,直接和后端要就行了
             if (index.hasLeaf == '1') {
              delete chiledModeInfo.children//为末级的时候删掉children,就不会出现末级也有小箭头可以点
            }
            arr.push(chiledModeInfo)
          }
          parentNode.children = arr
        })
        callback()
      }
    },

点击树节点的时候,把code和id都传给后端

 treeHandleSelect(index, node) {     
      this.standTableList[index].assetTypeName = node.label
      this.standTableList[index].assetTypeName = node.name
      this.standTableList[index].assetTypeid = node.id
      this.standTableList[index].assetTypeCode = node.code
    },

选完节点并回显后,调保存接口,把数据传给后端,然后就完成了,我们调详情接口回显也是可以正常回显的,因为上面文本框的插槽绑的已经是scope.row.字段的值,相当于自己自定义的值后端已经保存了。

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐