skills/vk-data-table/SKILL.md
vk-unicloud-admin框架的万能表格组件完整使用指南。Use when working with vk-data-table component for: (1) Creating data tables with JSON configuration, (2) Implementing CRUD operations with table, (3) Using table with cloud functions (vk.baseDao.getTableData), (4) Configuring table columns, buttons, pagination, sorting, filtering, (5) Implementing table events, methods, and slots, (6) Using table query component (vk-data-table-query) for search functionality.
npx skillsauth add caobingsheng/skills vk-data-tableInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
vk-data-table是vk-unicloud-admin框架的核心表格组件,通过JSON配置实现数据展示、查询、排序、分页、CRUD操作等功能。支持静态数据和动态数据(云函数/HTTP请求),配合vk.baseDao.getTableData可实现高性能的数据查询。
<template>
<vk-data-table :data="table1.data" :columns="table1.columns"></vk-data-table>
</template>
<script>
export default {
data() {
return {
table1: {
data: [],
columns: [
{ key: "_id", title: "ID", type: "text", width: 200 },
{ key: "username", title: "用户名", type: "text", width: 150 },
{ key: "nickname", title: "昵称", type: "text", width: 150 },
{ key: "mobile", title: "手机号", type: "text", width: 150 }
]
}
};
}
};
</script>
<template>
<vk-data-table
ref="table1"
action="admin/user/sys/getList"
:columns="table1.columns"
:query-form-param="queryForm1"
:pagination="true"
:page-size="20"
></vk-data-table>
</template>
<script>
export default {
data() {
return {
table1: {
columns: [
{ key: "username", title: "用户名", type: "text", width: 150 },
{ key: "nickname", title: "昵称", type: "text", width: 150 },
{ key: "_add_time", title: "添加时间", type: "time", width: 160 }
]
},
queryForm1: {
formData: {},
columns: []
}
};
}
};
</script>
columns是表格的核心配置,定义每个字段的显示规则:
columns: [
{
key: "username", // 字段键名
title: "用户名", // 列标题
type: "text", // 字段类型
width: 150, // 列宽度
align: "center", // 对齐方式:left/center/right
sortable: "custom", // 是否可排序
show: ["row", "detail"], // 显示场景:row/detail/expand/none
formatter: (val, row) => { // 自定义格式化
return val + "(已审核)";
}
}
]
常用字段类型:
text - 文本image - 图片tag - 标签switch - 开关time - 时间money - 金额radio/select/checkbox - 选择类html - HTML渲染详见:字段类型完整列表
table1: {
rightBtns: [
'detail_auto', // 自动详情
{
mode: 'update',
title: '编辑',
type: 'primary',
show: (item) => {
// 根据条件显示按钮
return item.status === 1;
}
},
{
mode: 'delete',
title: '删除',
type: 'danger',
disabled: (item) => {
// 根据条件禁用按钮
return item._id === 'admin';
}
},
'more' // 更多按钮
],
rightBtnsMore: [
{
title: '重置密码',
onClick: (item) => {
vk.toast(`重置${item.username}的密码`);
}
}
]
}
<vk-data-table
:pagination="true"
:page-size="20"
:page-sizes="[10, 20, 50, 100]"
:get-count="'auto'"
:max-page-count="1000"
></vk-data-table>
分页方案:
getCount="true" - 传统分页(每次都count)getCount="auto" - 智能分页(默认,首次count)getCount="false" - 滚动分页(从不count)详见:分页方案详解
<template>
<!-- 查询组件 -->
<vk-data-table-query
v-model="queryForm1.formData"
:columns="queryForm1.columns"
@search="search"
></vk-data-table-query>
<!-- 表格组件 -->
<vk-data-table
:query-form-param="queryForm1"
></vk-data-table>
</template>
<script>
export default {
data() {
return {
queryForm1: {
formData: {
nickname: "",
_add_time: []
},
columns: [
{ key: "nickname", title: "昵称", type: "text", mode: "%%" },
{ key: "_add_time", title: "添加时间", type: "datetimerange", mode: "[]" }
]
}
};
},
methods: {
search() {
this.$refs.table1.search();
}
}
};
</script>
查询模式(mode):
= - 精确匹配%% - 模糊匹配> / >= / < / <= - 数值比较[] - 范围查询in / nin - 在/不在数组中详见:查询组件完整指南
<vk-data-table
@success="onSuccess"
@detail="onDetail"
@update="onUpdate"
@delete="onDelete"
@row-click="onRowClick"
></vk-data-table>
methods: {
onSuccess({ data, list, total }) {
console.log("查询成功", list);
},
onDetail({ item, row }) {
console.log("查看详情", item);
},
onUpdate({ item, row }) {
console.log("编辑", item);
},
onDelete({ item, deleteFn }) {
vk.confirm("确认删除?", () => {
deleteFn(); // 调用删除函数
});
}
}
// 刷新表格
this.$refs.table1.refresh();
// 查询
this.$refs.table1.search();
// 显示详情
this.$refs.table1.showDetail(item);
// 获取选中行
let row = this.$refs.table1.getCurrentRow();
// 获取多选数据
let rows = this.$refs.table1.getMultipleSelection();
// 导出Excel
this.$refs.table1.exportExcel({
fileName: "用户列表",
pageIndex: 1,
pageSize: -1 // -1表示导出所有数据
});
详见:事件和方法完整列表
<vk-data-table>
<!-- 自定义gender字段 -->
<template v-slot:gender="{ row, column, index }">
<el-tag :type="row.gender === 1 ? 'primary' : 'success'">
{{ row.gender === 1 ? '男' : '女' }}
</el-tag>
</template>
<!-- 展开行 -->
<template v-slot:tableExpand="{ row }">
<div>展开内容:{{ row._id }}</div>
</template>
<!-- 表头插槽 -->
<template v-slot:header_nickname="{ column }">
<el-input v-model="queryForm1.formData.nickname" placeholder="搜索昵称"></el-input>
</template>
</vk-data-table>
详见:插槽使用指南
前端传递columns参数:
vk.callFunction({
url: "admin/user/sys/getList",
data: {
columns: [{
key: "nickname",
type: "text",
mode: "%%" // 必须指定mode
}],
formData: { nickname: "张" },
pageIndex: 1,
pageSize: 20
}
});
云函数使用getTableData:
let res = await vk.baseDao.getTableData({
dbName: "uni-id-users",
data: event.data, // 前端传来的data
whereJson: {
is_delete: false // 强制条件
}
});
getCount="false"max-page-count="1000"retry-count="3"rightBtns: [
{
mode: 'delete',
title: '删除',
show: (item) => {
return this.$hasRole("admin") || this.$hasPermission("user-delete");
}
}
]
table1: {
dataPreprocess: (list) => {
return list.map(item => {
item.fullname = item.firstname + " " + item.lastname;
return item;
});
}
}
查看详细参考文档的场景:
development
Use when working with tdd workflows tdd refactor
testing
Generate failing tests for the TDD red phase to define expected behavior and edge cases.
development
Implement the minimal code needed to make failing tests pass in the TDD green phase.
tools
Use when working with tdd workflows tdd cycle