每天学习一个vue插件(19)——vue-treeselect

7,637 阅读2分钟

每个人,终究要学会一个人独自长大

前言

image.png

1 介绍

常用属性 props

options

:options="options"

options: [{
  id: 'A',
  name: 'a',
  // 默认禁用该项
  isDisabled: true,
  // 默认展开该项叶子节点
  isDefaultExpanded: true,
  children: []
}]

value

v-model="value"

// 默认选中节点
// 以节点 id 填充
// 默认选中 a
value: ['A']

default-expand-level

:default-expand-level="level"

// 默认展开第一层级
// 所以分支下面的第一层级
// 如果希望指定某个分支,请在options里面配置 isDefaultExpanded
level: 1

normalizer

// 自定义键名
:normalizer=“normalizer”

// 函数类型,参数node
// 默认数据格式 id label children
// 只将 label 替换为 name
normalizer(node) {
  return {
    ...node,
    // id: node.key,
    label: node.name
    // children: node.subOptions,
  }
}

multiple

// 支持多选
:multiple=“true

multiple

// 显示叶子节点数目
:show-count="true"

flat

// 使用扁平模式
// 同时选中分支和叶子节点
// 默认选中分支后,叶子节点全部被选中
:flat="true"

常用插槽 slot

option-label

// 选项插槽
// 参数 {node,count}
<label
  slot="option-label"
  slot-scope="{node,count}">
  {{ node.isBranch ? 'Branch' : 'Leaf' }}: {{ node.label }}
  <span>({{ count }})</span>
</label>

常用事件 methods

select

// 选中时触发
// 参数node
@select="select"

deselect

// 取消时触发
// 参数node
@deselect="deselect"

2 使用

安装

npm i @riophae/vue-treeselect --save

引入

import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

树状选择

<template>
  <div class="BaseTreeSelect">
    <treeselect
      v-model="value"
      placeholder="请选择"
      :options="options"
      :multiple="true"
      :flat="true"
      :show-count="true"
      :normalizer="normalizer"
      :default-expand-level="1"
      @select="select"
    >
      <label
        slot="option-label"
        slot-scope="{
          node,
          shouldShowCount,
          count,
          labelClassName,
          countClassName
        }"
        :class="labelClassName"
      >
        {{ node.isBranch ? 'Branch' : 'Leaf' }}: {{ node.label }}
        <span v-if="shouldShowCount" :class="countClassName">
          ({{ count }})
        </span>
      </label>
    </treeselect>
    <p>{{ value }}</p>
  </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 {
  components: { Treeselect },
  data() {
    return {
      // 默认选中
      // id集合
      value: ['A', 'ABA', 'bb'],
      // 自定义键名
      // 默认 id label children
      // label 替换为name
      normalizer(node) {
        return {
          ...node,
          // id: node.key,
          label: node.name
          // children: node.subOptions,
        }
      },
      // 树状结构
      options: [
        {
          id: 'A',
          name: 'a',
          children: [
            {
              id: 'AA',
              name: 'aa',
              isDisabled: true
            },
            {
              id: 'AB',
              name: 'ab',
              // 默认展开
              isDefaultExpanded: true,
              children: [
                {
                  id: 'ABA',
                  name: 'aba'
                },
                {
                  id: 'ABB',
                  name: 'abb',
                  isNew: true
                }
              ]
            }
          ]
        },
        {
          id: 'b',
          name: 'b',
          children: [
            {
              id: 'ba',
              name: 'ba'
            },
            {
              id: 'bb',
              name: 'bb'
            }
          ]
        },
        {
          id: 'c',
          name: 'c'
        }
      ]
    }
  },
  methods: {
    select(node) {
      console.log('node=', node)
    }
  }
}
</script>

3.注意

1.value默认选中某些节点
2.isDefaultExpanded默认展开某个分支

尾声

今天的电影特别好看,今天的你也特别好看呢~

晚安 ^_^

参考链接

往期回顾