当前位置:首页 > 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..."
/>

高级多选功能

限制选择数量

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

扁平化显示选项

props: {
  flattenSearchResults: true
}

禁用选项

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

样式自定义

修改默认样式

.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-treeselect实现多选

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

相关文章

vue的动画实现

vue的动画实现

Vue 动画实现方式 Vue 提供了多种方式实现动画效果,主要通过内置组件和第三方库支持。 使用 Vue 内置过渡组件 Vue 的 transition 和 transition-group 组件…

vue实现单点

vue实现单点

Vue 实现单点登录(SSO)方案 单点登录(SSO)允许用户在多个相关但独立的系统中使用同一组凭证登录。以下是基于 Vue 的实现方案: 基于 OAuth2/OpenID Connect 的实现…

js实现vue

js实现vue

Vue.js 的基本实现方法 在 JavaScript 中实现 Vue.js 的功能,可以通过直接引入 Vue 库或使用现代构建工具(如 Vite 或 Webpack)。以下是几种常见的实现方式:…

vue 实现toast

vue 实现toast

vue 实现 toast 的方法 在 Vue 中实现 Toast 提示功能可以通过多种方式,以下是几种常见的实现方法: 使用第三方库 Vue 生态中有许多成熟的 Toast 库,例如 vue-to…

vue实现setinterval

vue实现setinterval

在 Vue 中使用 setInterval Vue 中可以通过生命周期钩子和方法结合 setInterval 实现定时任务。以下是一个完整的实现示例: <template> <…

分页实现vue

分页实现vue

分页实现(Vue) 在Vue中实现分页功能通常需要结合前端分页逻辑和后端API支持。以下是两种常见的实现方式: 前端分页实现 适用于数据量较小的情况,直接在客户端完成分页逻辑。 <templ…