前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >vue-treeselect 的使用

vue-treeselect 的使用

作者头像
tianyawhl
发布2020-05-11 09:13:11
1.6K0
发布2020-05-11 09:13:11
举报
文章被收录于专栏:前端之攻略前端之攻略

1、安装 

代码语言:javascript
复制
npm install --save @riophae/vue-treeselect

然后在页面中引入

代码语言:javascript
复制
  // import the component
  import Treeselect from '@riophae/vue-treeselect'
  // import the styles
  import '@riophae/vue-treeselect/dist/vue-treeselect.css'

注册组件

代码语言:javascript
复制
components: { Treeselect }

使用

代码语言:javascript
复制
<treeselect v-model="value" :multiple="true" :options="options" />

完整代码

代码语言:javascript
复制
<template>
  <div id="app">
    <treeselect v-model="value" :multiple="true" :options="options" />
  </div>
</template>

<script>
  // import the component
  import Treeselect from '@riophae/vue-treeselect'
  // import the styles
  import '@riophae/vue-treeselect/dist/vue-treeselect.css'

  export default {
    // register the component
    components: { Treeselect },
    data() {
      return {
        // define the default value
        value: null,
        // define options
        options: [ {
          id: 'a',
          label: 'a',
          children: [ {
            id: 'aa',
            label: 'aa',
          }, {
            id: 'ab',
            label: 'ab',
          } ],
        }, {
          id: 'b',
          label: 'b',
        }, {
          id: 'c',
          label: 'c',
        } ],
      }
    },
  }
</script>

详细的文档

注意绑定的数据结构是 id, label, children

代码语言:javascript
复制
{
  id: '<id>',
  label: '<label>',
  children: [
    {
      id: '<child id>',
      label: '<child label>',
    },
    ...
  ],
}

有时候后台传过来的数据结构的键不是 id, label, children,我们需要转换成符合要求的数据结构

代码语言:javascript
复制
    // 获取信息
    getAllDeps() {
      this.$axios.post("url").then(res => {
        if (res.data.responseCode === "200") {
         let tempArr=[]
          this.changeDepsDataName(res.data.data,tempArr)
          console.log(tempArr)
          this.options= tempArr
        }
      });
    },
代码语言:javascript
复制
    changeDepsDataName(dataArr, tepArr){
      for( let i=0;i<dataArr.length;i++){
        let obj={}
        obj.id = dataArr[i].id
        obj.label = dataArr[i].departmentName
        if(dataArr[i].childDepartment && dataArr[i].childDepartment.length){
          obj.children = this.changeDepsDataName(dataArr[i].childDepartment,[])
        }
        tepArr.push(obj)
      }
      return tepArr
    },
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档