Skip to content

TableSelect 表格选择器

通过弹窗表格选择数据,支持单选和多选。在线演示

基础用法

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[]) {
  selectedText.value = selection.map(item => item.username).join(',')
}
</script>

多选模式

vue
<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: '昵称' }
  ]
}
</script>

自定义触发器

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

自定义表格列

vue
<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>

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

Props

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

selectConfig 配置

参数说明类型默认值
width组件宽度string'100%'
placeholder占位文本string'请选择'
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
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 许可发布 · 如需部署协助或二开定制,请查看 支持与合作