XGrid 表格
示例
基础表格
No Data
No Data
加载中...
<template>
<div style="height: 200px">
<XGrid
ref="gridRef"
id="demo"
size="small"
:columns="columns"
:loader="loader">
</XGrid>
</div>
</template>
<script lang="ts" setup>
import { ref, type Ref } from 'vue';
import { XGrid, type GridColumns } from '@vtj/ui';
import { numberFormat } from '@vtj/utils';
import { data as tableData } from './data';
const columns: Ref<GridColumns> = ref([]);
setTimeout(() => {
columns.value = [
{ type: 'checkbox', title: '', fixed: 'left' },
{ type: 'seq', title: '序号', fixed: 'left' },
{
field: 'id',
title: 'ID'
},
{
field: 'name',
title: '姓名',
filters: [{ value: null }],
cellRender: {
name: 'LinkCell',
events: {
click(value: any, e: any) {
console.log(value, e);
}
}
}
},
{
field: 'sex',
title: '性别',
filters: [
{ label: '男', value: 1 },
{ label: '女', value: 0 }
],
formatter: (data: any) => {
const { row } = data;
const map: Record<number, any> = {
1: '男',
0: '女'
};
return map[row.sex];
}
},
{
field: 'age',
title: '年龄',
sortable: true
},
{
field: 'salary',
title: '工资',
formatter: (e: any) => `¥${numberFormat(e.cellValue, '0.00')}`
}
] as GridColumns;
}, 1000);
const loader = () => {
return tableData;
};
</script>
分页表格
Total 0
- 1
<template>
<div class="container">
<XGrid
ref="gridRef"
id="demo"
size="small"
:columns="columns"
:rowSortable="true"
@row-sort="onRowSort"
@column-sort="onColSort"
columnSortable
:filter-renders="{ name: 'XInput' }"
:toolbar-config="{}"
:cell-renders="{ sex: 'XTag' }"
@resizable-change="onResize"
:customable="true"
:getCustom="getCustom"
:saveCustom="saveCustom"
:border="true"
:stripe="false"
:loader="loader"
:page="page"
:pager="true"
:auto="true"
:resizable="true"
:virtual="true"
:sumFields="['age', 'month']"
:avgFields="['age', 'year']"
:sumAllFields="{ age: 23232 }">
</XGrid>
</div>
</template>
<script lang="ts" setup>
import { ref, type Ref } from 'vue';
import { XGrid, type GridColumns } from '@vtj/ui';
import { storage, numberFormat } from '@vtj/utils';
import { data as fetchTableData } from './data';
const gridRef = ref<InstanceType<typeof XGrid>>();
const page = ref(1);
const fetchData = (data: any) => {
return fetchTableData;
};
const columns: Ref<GridColumns> = ref([]);
setTimeout(() => {
columns.value = [
{ type: 'checkbox', title: '', width: 60, fixed: 'left' },
{ type: 'seq', title: '序号', width: 60, fixed: 'left' },
{
field: 'id',
title: 'ID'
},
{
field: 'name',
title: '姓名',
filters: [{ value: null }],
cellRender: {
name: 'LinkCell',
events: {
click(value: any, e: any) {
console.log(value, e);
}
}
}
},
{
field: 'avatar',
title: '头像',
showOverflow: false,
visible: true
},
{
field: 'sex',
title: '性别',
filters: [
{ label: '男', value: 1 },
{ label: '女', value: 0 }
],
formatter: (data: any) => {
const { row } = data;
const map: Record<number, any> = {
1: '男',
0: '女'
};
return map[row.sex];
}
},
{
field: 'age',
title: '年龄',
sortable: true
},
{
field: 'birth',
title: '出生日期',
children: [
{ field: 'year', title: '年' },
{ field: 'month', title: '月' },
{ field: 'date', title: '日' }
]
},
{
field: 'address',
title: '地址',
children: [
{ field: 'province', title: '省' },
{ field: 'city', title: '市' },
{ field: 'county', title: '区' }
]
},
{
field: 'salary',
title: '工资',
formatter: (e: any) => `¥${numberFormat(e.cellValue, '0.00')}`
},
{
field: 'intro',
title: '简介'
},
{
field: 'join',
title: '入职日期',
filters: [{ value: '' }],
filterRender: {
name: 'DateFilter'
}
},
{
field: 'create',
title: '创建时间'
},
{
field: 'actions',
title: '操作',
width: '150px'
}
] as GridColumns;
}, 100);
setTimeout(() => {
columns.value = [
{ type: 'checkbox', title: '', width: 60, fixed: 'left' },
{ type: 'seq', title: '序号', width: 60, fixed: 'left' },
{
field: 'id',
title: 'ID'
},
{
field: 'name',
title: '姓名',
filters: [{ value: null }],
cellRender: {
name: 'LinkCell',
events: {
click(value: any, e: any) {
console.log(value, e);
}
}
}
},
{
field: 'avatar',
title: '头像',
showOverflow: false,
visible: true
},
{
field: 'sex',
title: '性别',
filters: [
{ label: '男', value: 1 },
{ label: '女', value: 0 }
],
formatter: (data: any) => {
const { row } = data;
const map: Record<number, any> = {
1: '男',
0: '女'
};
return map[row.sex];
}
},
{
field: 'age',
title: '年龄',
sortable: true
},
{
field: 'birth',
title: '出生日期',
children: [
{ field: 'year', title: '年' },
{ field: 'month', title: '月' },
{ field: 'date', title: '日' }
]
},
{
field: 'address',
title: '地址',
children: [
{ field: 'province', title: '省' },
{ field: 'city', title: '市' },
{ field: 'county', title: '区' }
]
},
{
field: 'salary',
title: '工资',
formatter: (e: any) => `¥${numberFormat(e.cellValue, '0.00')}`
},
{
field: 'intro',
title: '简介'
},
{
field: 'join',
title: '入职日期',
filters: [{ value: '' }],
filterRender: {
name: 'DateFilter'
}
},
{
field: 'create',
title: '创建时间'
},
{
field: 'actions',
title: '操作',
width: '150px'
}
] as GridColumns;
// console.log(gridRef.value?.vxeRef?.getTableColumn());
}, 1000);
const onRowSort = (e: any) => {
console.log('onRowSort', e);
};
const onColSort = (e: any) => {
console.log('onColSort', e);
};
const onResize = (e: any) => {
console.log('onResize', e);
};
const getCustom = async (id: string) => {
return storage.get(id, { type: 'local' });
};
const saveCustom = async (info: any) => {
storage.save(info.id, info, { type: 'local' });
};
const loader: any = async (params: any) => {
const { page, pageSize = 50 } = params || {};
console.log('query', params);
return await fetchData({
currentPage: page,
pageSize,
total: 1000
});
};
</script>
<style lang="scss" scope>
.container {
height: 100%;
}
</style>
API
属性
属性名 | 说明 | 类型 | 默认值 |
---|---|---|---|
id | 表格id, 保存用户自定义配置时需要用到 的唯一 标识 (被某些特定的功能所依赖) | string | - |
columns | 列配置,重定义是为了实现列拖拽排序, watch列变化变化刷新列 | array | [] |
loader | 数据加载器 | function | - |
rowSortable | 行拖拽排序 | boolean | object | false |
columnSortable | 列拖拽排序 | boolean | object | false |
customable | 开启用户自定义 | boolean | - |
getCustom | 获取自定义配置 | function | - |
saveCustom | 保存自定义配置 | function | - |
resizable | 开启列resizable | boolean | false |
pager | 开启分页 | boolean | - |
page | 初始页码 | number | 1 |
pageSize | 初始每页显示条目个数 | number | 50 |
pageSizes | 每页显示个数选择器的选项设置 | array | [50, 100, 200, 500] |
auto | 初始执行加载函数 | boolean | true |
virtual | 开启虚拟滚动 | boolean | false |
cellRenders | 单元格渲染器 | object | - |
editRenders | 编辑状态单元格渲染器 | object | - |
filterRenders | 过滤器渲染器 | object | - |
editable | 开启支持编辑模式 | boolean | false |
sumFields | 单页合计列 | array | - |
avgFields | 单页平均值列 | array | - |
sumAllFields | 全部数据总计列 | object | - |
事件
名称 | 说明 | 参数 |
---|---|---|
editChange | 修改数据时触发 | (rows) |
插槽
插槽名 | 说明 | 类型 |
---|---|---|
custom | 自定义插槽 | - |
empty | 没有数据 | - |
loading | 加载中效果 | - |
form | 表单展示 | - |
toolbar | 工具栏 | - |
toolbar__buttons | 工具栏按钮 | - |
top | 顶部区域 | - |
bottom | 底部区域 | - |
pager | 开启分页 | - |
pager__left | 分页左边 | - |
暴露
名称 | 说明 | 类型 |
---|---|---|
state | page:number ; pageSize:number | object |
load | - | - |
search | - | - |
vxeRef | - | - |
rowSortable | - | - |
columnSortable | - | - |
insertActived | - | - |
validate | - | - |
getSelected | - | - |
remove | - | - |
getRows | - | - |
setActived | - | - |
doLayout | - | - |
getRecords | - | - |
setSelectCell | - | - |
$vtjDynamicSlots | - | - |