lk-component-element
一个基于Element Plus封装的Vue3功能组件库
3,095
| Install | |
|---|---|
npm install lk-component-element |
|
| Latest Version: | 1.0.18 |
| License: | ISC |
| Last Updated: | Sep 3, 2026 |
| Links: | npm |
Maintainer: liuke115npm
lk-component-element
基于 Vue 3 + Element Plus 的业务组件库,提供表格和表单的快速封装组件。 该组件基于element-plus封装,请务必提前安装element-plus。
安装
pnpm add lk-component-element
快速开始
在 main.js 中全局注册:
import { createApp } from 'vue'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import lkComponentElement from 'lk-component-element'
import 'lk-component-element/lk-component-element.css'
import App from './App.vue'
const app = createApp(App)
app.use(ElementPlus)
app.use(lkComponentElement)
app.mount('#app')
组件列表
test-button
演示用测试按钮组件。
<test-button />
lk-datatable
前端分页表格组件。传入完整数据,组件内部自动完成分页切片。
Props
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| data | Array | [] |
表格数据(全量) |
| columns | Array | [] |
列配置 |
| title | String | '' |
表格标题 |
| pageSize | Number | 10 |
每页条数 |
| currentPage | Number | 1 |
当前页码 |
| pageSizes | Array | [10, 20, 50, 100] |
每页条数选项 |
| paginationLayout | String | 'total, sizes, prev, pager, next, jumper' |
分页器布局 |
| tableProps | Object | {} |
透传给 el-table 的额外属性 |
Events
| 事件 | 说明 |
|---|---|
| update:currentPage | 页码变化 |
| update:pageSize | 每页条数变化 |
Slots
| 插槽 | 说明 |
|---|---|
| toolbar | 标题栏右侧工具区(筛选、查询等) |
action |
操作列内容(slotProps: { row }) |
columns 配置
| 属性 | 类型 | 说明 |
|---|---|---|
| prop | String | 字段名 |
| label | String | 列标题 |
| width | String/Number | 列宽 |
| minWidth | String/Number | 最小列宽 |
| align | String | 对齐方式(left / center / right),默认 left |
| sortable | Boolean | 是否可排序 |
| fixed | String/Boolean | 固定列(left / right / true) |
| showOverflowTooltip | Boolean | 超出是否显示 tooltip,默认 true |
| type | String | 列类型,'action' 为操作列 |
| emptyText | String | 值为 null/undefined/空字符串时的默认显示文字 |
const columns = [
{ prop: 'name', label: '姓名', width: '120' },
{ prop: 'email', label: '邮箱', emptyText: '未填写' },
{ prop: 'remark', label: '备注', emptyText: '暂无' },
{ type: 'action', label: '操作', width: '180', fixed: 'right' }
]
示例
<template>
<lk-datatable
v-model:current-page="page"
v-model:page-size="pageSize"
:data="tableData"
:columns="columns"
title="用户列表"
>
<template #toolbar>
<el-input placeholder="搜索..." style="width: 200px" />
</template>
<template #action="{ row }">
<el-button size="small" @click="handleEdit(row)">编辑</el-button>
<el-button size="small" type="danger" @click="handleDelete(row)">删除</el-button>
</template>
</lk-datatable>
</template>
lk-datatable-server
后端分页表格组件。数据由外部请求获取,组件只负责展示和分页事件触发。
Props
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| data | Array | [] |
当前页数据 |
| total | Number | 0 |
数据总条数 |
| columns | Array | [] |
列配置 |
| title | String | '' |
表格标题 |
| loading | Boolean | false |
加载状态 |
| pageSize | Number | 10 |
每页条数 |
| currentPage | Number | 1 |
当前页码 |
| pageSizes | Array | [10, 20, 50, 100] |
每页条数选项 |
| paginationLayout | String | 'total, sizes, prev, pager, next, jumper' |
分页器布局 |
| tableProps | Object | {} |
透传给 el-table 的额外属性 |
Events
| 事件 | 说明 |
|---|---|
| update:currentPage | 页码变化 |
| update:pageSize | 每页条数变化 |
| page-change | 页码变化(同 update:currentPage) |
| size-change | 每页条数变化 |
Slots
与 lk-datatable 相同(toolbar、action)。
示例
<template>
<lk-datatable-server
v-model:current-page="page"
v-model:page-size="pageSize"
:data="tableData"
:total="total"
:loading="loading"
:columns="columns"
title="用户列表"
@page-change="fetchData"
@size-change="fetchData"
/>
</template>
<script setup>
import { ref, onMounted } from 'vue'
const page = ref(1)
const pageSize = ref(10)
const total = ref(0)
const tableData = ref([])
const loading = ref(false)
const columns = [
{ prop: 'name', label: '姓名' },
{ prop: 'email', label: '邮箱' },
{ type: 'action', label: '操作', width: '180', fixed: 'right' }
]
const fetchData = async () => {
loading.value = true
const res = await api.getUsers({ page: page.value, pageSize: pageSize.value })
tableData.value = res.list
total.value = res.total
loading.value = false
}
onMounted(fetchData)
</script>
lk-autoform
自动渲染表单组件,支持 20+ 种 Element Plus 字段类型和分步表单。
Props
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| modelValue | Object | {} |
表单数据(v-model) |
| fields | Array | [] |
字段配置 |
| steps | Array | null |
步骤配置(为 null 时为普通表单) |
| currentStep | Number | 0 |
当前步骤(v-model:current-step) |
| labelPosition | String | — | 标签位置(left / right / top) |
| labelWidth | String | '100px' |
标签宽度 |
| labelSuffix | String | — | 标签后缀 |
| hideRequiredAsterisk | Boolean | — | 隐藏必填星号 |
| requireAsteriskPosition | String | — | 星号位置(left / right) |
| showMessage | Boolean | — | 是否显示校验错误信息 |
| inlineMessage | Boolean | — | 是否行内显示校验信息 |
| inline | Boolean | — | 行内表单模式 |
| size | String | 'default' |
表单尺寸(large / default / small) |
| disabled | Boolean | — | 禁用整个表单 |
| validateOnRuleChange | Boolean | — | 规则变化时是否触发验证 |
| scrollToError | Boolean | — | 验证失败时自动滚动到错误字段 |
| scrollIntoView | String | — | 验证失败时滚动到指定元素 |
| submitText | String | '提交' |
提交按钮文字 |
| submitType | String | 'primary' |
提交按钮类型(同 el-button type) |
| submitSize | String | 'default' |
提交按钮尺寸(large / default / small) |
| buttonAlign | String | 'center' |
按钮对齐方式(left / center / right) |
| showActions | Boolean | true |
是否显示底部操作区域 |
| alignCenter | Boolean | true |
步骤条文字是否居中 |
标注
—的属性默认值为undefined,即使用 Element Plus 自身默认值。
Events
| 事件 | 说明 |
|---|---|
| update:modelValue | 表单数据变化 |
| update:currentStep | 步骤变化 |
| submit | 提交(验证通过后触发) |
| field-change | 任意字段值变化时触发,参数:{ field, value, model } |
Slots
| 插槽名 | 说明 |
|---|---|
| actions | 自定义整个操作区域内容(按钮、链接等)。slotProps:{ isSteps, isFirst, isLast, submit, prev, next } |
Expose 方法
| 方法 | 说明 |
|---|---|
| submit() | 触发表单验证并提交 |
| validate() | 表单验证(同 el-form) |
| validateField() | 验证指定字段 |
| resetFields() | 重置整个表单 |
| resetField(name) | 重置单个字段并清除其验证状态 |
| clearValidate() | 清除验证状态 |
| scrollToField() | 滚动到指定字段 |
| setFieldValue(name, value) | 编程式设置单个字段值(触发 onChange) |
| setFieldsValue(obj) | 批量设置多个字段值 |
| getFormData() | 返回表单数据的副本 |
| formRef | el-form 原始模板引用 |
字段级回调
写在 fields 配置的顶层(与 field、label、type 同级):
| 回调 | 参数 | 说明 |
|---|---|---|
| onChange | (value, model) |
字段值变化后触发,可直接修改 model 实现联动 |
| onFocus | 通过 params.onFocus 设置 |
字段获得焦点,接收原生 Event |
| onBlur | 通过 params.onBlur 设置 |
字段失去焦点,接收原生 Event |
{
field: 'role',
label: '角色',
type: 'select',
params: {
options: [...],
onFocus: (e) => console.log('获得焦点'),
onBlur: (e) => console.log('失去焦点')
},
onChange: (value, model) => {
// 选择访客时自动关闭启用状态
if (value === 'guest') model.status = false
}
}
fields 配置结构
每个字段包含四个顶层属性和一个 params 对象:
{
field: 'fieldName', // 字段名,对应 modelValue 中的 key
label: '字段标签', // 表单标签
type: 'input', // 字段类型
params: { ... }, // Element Plus 组件的所有参数
visibleIf: { ... }, // 可选,条件显示
onChange: (value, model) => { ... } // 可选,值变化回调
}
支持的字段类型
| type | 对应组件 | params 常用参数 |
|---|---|---|
| input | el-input | placeholder, disabled, clearable, maxlength, rules |
| textarea | el-input(type=textarea) | placeholder, rows, maxlength, showWordLimit |
| number | el-input-number | min, max, step, controlsPosition |
| select | el-select | options, placeholder, multiple, clearable |
| radio | el-radio-group | options, optionType('button') |
| checkbox | el-checkbox-group | options |
| switch | el-switch | activeText, inactiveText |
| date | el-date-picker | dateType, valueFormat |
| time | el-time-picker | valueFormat |
| slider | el-slider | min, max, step, showInput |
| autocomplete | el-autocomplete | fetchSuggestions, valueKey |
| cascader | el-cascader | options, cascaderProps |
| colorPicker | el-color-picker | showAlpha, colorFormat |
| inputTag | el-input-tag | maxTags |
| mention | el-mention | options, prefix |
| rate | el-rate | max, allowHalf, showText |
| selectV2 | el-select-v2 | options, multiple, filterable |
| transfer | el-transfer | transferData, titles |
| treeSelect | el-tree-select | treeData, multiple, showCheckbox |
| upload | el-upload | action, listType, limit, accept, buttonText |
普通表单示例
<template>
<lk-autoform
v-model="formData"
:fields="fields"
label-width="100px"
submit-text="保存"
@submit="handleSubmit"
/>
</template>
<script setup>
import { ref } from 'vue'
const formData = ref({ name: '', role: '' })
const fields = [
{
field: 'name',
label: '姓名',
type: 'input',
params: {
placeholder: '请输入姓名',
rules: [{ required: true, message: '请输入姓名', trigger: 'blur' }]
}
},
{
field: 'role',
label: '角色',
type: 'select',
params: {
placeholder: '请选择角色',
options: [
{ label: '管理员', value: 'admin' },
{ label: '编辑', value: 'editor' }
]
}
}
]
const handleSubmit = (data) => {
console.log('提交:', data)
}
</script>
分步表单示例
<template>
<lk-autoform
v-model="formData"
:fields="fields"
:steps="steps"
v-model:current-step="currentStep"
submit-text="提交申请"
@submit="handleSubmit"
/>
</template>
<script setup>
import { ref } from 'vue'
import { Edit, Reading, CircleCheck } from '@element-plus/icons-vue'
const currentStep = ref(0)
const formData = ref({ name: '', education: '', school: '' })
const fields = [
{ field: 'name', label: '姓名', type: 'input', params: { placeholder: '请输入姓名' } },
{ field: 'education', label: '学历', type: 'select', params: { options: [...], placeholder: '请选择' } },
{
field: 'school',
label: '学校',
type: 'input',
params: { placeholder: '请输入学校' },
visibleIf: { education: ['bachelor', 'master', 'phd'] } // 支持数组匹配
}
]
const steps = [
{ title: '基本信息', description: '填写个人资料', icon: Edit, fields: ['name'] },
{ title: '教育背景', description: '学历信息', icon: Reading, fields: ['education', 'school'] },
{ title: '确认提交', description: '核对信息', icon: CircleCheck, fields: ['education', 'school'] }
]
</script>
visibleIf 条件显示
visibleIf 写在字段顶层(与 field/label/type 同级),支持三种写法:
// 单值匹配
visibleIf: { education: 'bachelor' }
// 数组匹配(满足任一即可)
visibleIf: { education: ['bachelor', 'master', 'phd'] }
// 函数自定义
visibleIf: (modelValue) => modelValue.age >= 18
// 多条件(全部满足)
visibleIf: { working: true, department: 'tech' }
Related Packages
vue2-datatable-component-uikit
The best Datatable for Vue.js 2.x which never sucks... with ui-kit
4,771
870
Version History
| Version | Released | License |
|---|---|---|
| 1.0.18 | ISC | |
| 1.0.17 | ISC | |
| 1.0.16 | ISC | |
| 1.0.15 | ISC | |
| 1.0.14 | ISC | |
| 1.0.12 | ISC | |
| 1.0.11 | ISC | |
| 1.0.10 | ISC | |
| 1.0.9 | ISC | |
| 1.0.8 | ISC | |
| 1.0.7 | ISC | |
| 1.0.6 | ISC | |
| 1.0.5 | ISC | |
| 1.0.4 | ISC | |
| 1.0.3 | ISC | |
| 1.0.2 | ISC | |
| 1.0.1 | ISC | |
| 1.0.0 | ISC |