Skip to content

TableSelect 表格选择器

表格选择器组件,支持单选和多选,通过弹窗表格选择数据。

🎯 在线演示https://vue.youlai.tech/#/demo/table-select

基本用法

vue
<template>
  <TableSelect
    v-model="selectedText"
    :select-config="selectConfig"
    @confirm-click="handleConfirm"
  />
</template>

<script setup lang="ts">
import UserAPI from '@/api/system/user'

const selectedText = ref('')

const selectConfig = {
  // 宽度
  width: '100%',
  // 占位符
  placeholder: '请选择用户',
  // 是否多选
  multiple: false,
  // 主键字段(跨页选择必填)
  pk: 'id',
  // 列表请求接口
  indexAction: UserAPI.getPage,
  // 搜索表单项
  formItems: [
    {
      type: 'input',
      label: '关键字',
      prop: 'keywords',
      attrs: { placeholder: '用户名/昵称/手机号' }
    }
  ],
  // 表格列配置
  tableColumns: [
    { type: 'selection', width: 50 },
    { prop: 'username', label: '用户名' },
    { prop: 'nickname', label: '昵称' },
    { prop: 'mobile', label: '手机号' }
  ]
}

function handleConfirm(selection: any[]) {
  console.log('选中的数据:', selection)
  selectedText.value = selection.map(item => item.username).join(',')
}
</script>

多选模式

vue
<template>
  <TableSelect
    :select-config="selectConfig"
    @confirm-click="handleConfirm"
  />
</template>

<script setup lang="ts">
const selectConfig = {
  multiple: true,  // 开启多选
  pk: 'id',
  indexAction: UserAPI.getPage,
  formItems: [
    { type: 'input', label: '关键字', prop: 'keywords' }
  ],
  tableColumns: [
    { type: 'selection', width: 50 },
    { prop: 'username', label: '用户名' },
    { prop: 'nickname', label: '昵称' }
  ]
}

function handleConfirm(selection: any[]) {
  console.log('选中了', selection.length, '条数据')
}
</script>

自定义触发器

使用默认插槽自定义触发器:

vue
<template>
  <TableSelect :select-config="selectConfig" @confirm-click="handleConfirm">
    <el-button type="primary">选择用户</el-button>
  </TableSelect>
</template>

自定义表格列

vue
<template>
  <TableSelect :select-config="selectConfig" @confirm-click="handleConfirm">
    <!-- 自定义状态列 -->
    <template #status="{ row }">
      <el-tag :type="row.status === 1 ? 'success' : 'danger'">
        {{ row.status === 1 ? '启用' : '禁用' }}
      </el-tag>
    </template>
  </TableSelect>
</template>

<script setup lang="ts">
const selectConfig = {
  indexAction: UserAPI.getPage,
  formItems: [
    { type: 'input', label: '关键字', prop: 'keywords' }
  ],
  tableColumns: [
    { type: 'selection', width: 50 },
    { prop: 'username', label: '用户名' },
    { prop: 'nickname', label: '昵称' },
    { 
      prop: 'status', 
      label: '状态',
      templet: 'custom',
      slotName: 'status'  // 对应插槽名
    }
  ]
}
</script>

Props

参数说明类型默认值
selectConfig选择器配置ISelectConfig-
text显示文本string''

selectConfig 配置

参数说明类型默认值
width组件宽度string'100%'
placeholder占位文本string'请选择'
popoverPopover 组件属性object-
pk主键字段(跨页选择必填)string'id'
multiple是否多选booleanfalse
indexAction列表请求函数(params) => Promise-
formItems搜索表单项配置FormItem[][]
tableColumns表格列配置TableColumn[][]

formItems 配置

参数说明类型
type组件类型:input / select / tree-select / date-pickerstring
label标签文本string
prop字段名string
attrs组件属性object
initialValue初始值any
options选项(select 使用){ label, value }[]

tableColumns 配置

参数说明类型
type列类型:selection / index / expandstring
prop字段名string
label列标题string
width列宽度string | number
templet模板类型:custom 使用插槽string
slotName插槽名称string

Events

事件名说明回调参数
confirm-click确认选择时触发(selection: any[])

Slots

插槽名说明
default自定义触发器
[slotName]自定义表格列内容

功能特性

  • ✅ 单选/多选模式
  • ✅ 分页加载
  • ✅ 搜索筛选
  • ✅ 跨页选择(多选时保留选中状态)
  • ✅ 自定义触发器
  • ✅ 自定义表格列

使用场景

  • 选择用户
  • 选择部门
  • 选择角色
  • 选择商品
  • 任何需要弹窗选择的场景

相关链接

基于 MIT 许可发布