当前位置:首页 > VUE

vue-treeselect实现多选

2026-02-22 19:51:36VUE

vue-treeselect 多选实现方法

安装 vue-treeselect 包

npm install @riophae/vue-treeselect

引入并注册组件

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

export default {
  components: { Treeselect },
  // ...
}

基础多选配置

data() {
  return {
    value: [], // 多选值必须是数组
    options: [
      {
        id: 'group1',
        label: 'Group 1',
        children: [
          { id: 'item1', label: 'Item 1' },
          { id: 'item2', label: 'Item 2' }
        ]
      }
    ]
  }
}

模板中使用

<treeselect
  v-model="value"
  :multiple="true"
  :options="options"
  placeholder="Select items..."
/>

高级多选功能

限制选择数量

vue-treeselect实现多选

props: {
  limit: 3, // 最多选择3项
  limitText: count => `已选 ${count} / 3`
}

扁平化显示选项

props: {
  flattenSearchResults: true
}

禁用选项

options: [
  {
    id: 'item1',
    label: 'Item 1',
    isDisabled: true // 禁用此项
  }
]

样式自定义

修改默认样式

vue-treeselect实现多选

.vue-treeselect__control {
  border-radius: 5px;
}

.vue-treeselect__multi-value-item {
  background-color: #409eff;
}

注意事项

多选时必须将 v-model 初始化为数组

value: [] // 正确
value: null // 错误,会导致单选模式

异步加载选项时处理多选

async loadOptions({ action, parentNode, callback }) {
  if (action === 'LOAD_CHILDREN_OPTIONS') {
    const res = await api.getChildren(parentNode.id)
    callback(null, res.data)
  }
}

多选值变化监听

watch: {
  value(newVal) {
    console.log('Selected values:', newVal)
  }
}

标签: 多选vue
分享给朋友:

相关文章

vue实现tablegrid

vue实现tablegrid

Vue 实现 TableGrid 的方法 使用 Element UI 的 Table 组件 Element UI 提供了强大的 Table 组件,可以快速实现表格布局。安装 Element UI 后,…

vue compile 实现

vue compile 实现

Vue 编译实现原理 Vue 的编译过程将模板字符串转换为渲染函数,主要分为解析、优化和代码生成三个阶段。 解析阶段(Parse) 将模板字符串转换为抽象语法树(AST)。Vue 使用正则表达式和有…

vue实现slot

vue实现slot

Vue 中的 Slot 实现方法 Vue 的 slot 是一种内容分发机制,允许父组件向子组件插入内容。以下是几种常见的 Slot 实现方式: 默认 Slot 子组件通过 <slot>…

vue实现curd

vue实现curd

Vue实现CRUD操作 初始化Vue项目 使用Vue CLI创建新项目,安装必要依赖如axios用于HTTP请求 vue create crud-demo cd crud-demo npm inst…

vue实现pwa

vue实现pwa

Vue 实现 PWA 的步骤 Vue 结合 PWA(Progressive Web App)可以创建离线可用的 Web 应用。以下是实现方法: 安装 Vue CLI 并创建项目 确保已安装 Vue…

vue 实现图片

vue 实现图片

Vue 实现图片加载与展示的方法 在 Vue 中实现图片加载和展示可以通过多种方式完成,以下是一些常见的方法: 使用 v-bind 绑定图片路径 通过 v-bind(或简写为 :)动态绑定图片的…