179 lines
4.1 KiB
TypeScript
179 lines
4.1 KiB
TypeScript
import type { VxeTableGridOptions } from '@vben/plugins/vxe-table';
|
||
|
||
import type { VbenFormSchema } from '#/adapter/form';
|
||
import type { OnActionClickFn } from '#/adapter/vxe-table';
|
||
import type { DeptApi } from '#/api';
|
||
|
||
import { ref } from 'vue';
|
||
|
||
import { z } from '#/adapter/form';
|
||
import { getAllDeptTree } from '#/api';
|
||
import { $t } from '#/locales';
|
||
|
||
const treeData = ref([]);
|
||
|
||
function transformToTreeData(data: any) {
|
||
return data.map((item) => ({
|
||
value: item.id,
|
||
label: item.name,
|
||
...(item.children && { children: transformToTreeData(item.children) }),
|
||
}));
|
||
}
|
||
|
||
// 使用示例
|
||
getAllDeptTree(0).then((res) => {
|
||
treeData.value = transformToTreeData(res);
|
||
});
|
||
/**
|
||
* 获取编辑表单的字段配置。如果没有使用多语言,可以直接export一个数组常量
|
||
*/
|
||
export function useSchema(): VbenFormSchema[] {
|
||
return [
|
||
{
|
||
component: 'Input',
|
||
fieldName: 'name',
|
||
label: '部门名称',
|
||
rules: z.string(),
|
||
},
|
||
{
|
||
component: 'TreeSelect',
|
||
componentProps: {
|
||
allowClear: true,
|
||
// api: getAllDeptTree(1),
|
||
treeData,
|
||
class: 'w-full',
|
||
labelField: 'name',
|
||
valueField: 'id',
|
||
childrenField: 'children',
|
||
},
|
||
fieldName: 'pid',
|
||
label: '上级部门',
|
||
},
|
||
{
|
||
component: 'RadioGroup',
|
||
componentProps: {
|
||
buttonStyle: 'solid',
|
||
options: [
|
||
{ label: $t('common.enabled'), value: true },
|
||
{ label: $t('common.disabled'), value: false },
|
||
],
|
||
optionType: 'button',
|
||
},
|
||
defaultValue: true,
|
||
fieldName: 'enabled',
|
||
label: '状态',
|
||
},
|
||
{
|
||
component: 'Textarea',
|
||
componentProps: {
|
||
maxLength: 50,
|
||
rows: 3,
|
||
showCount: true,
|
||
},
|
||
fieldName: 'remark',
|
||
label: '备注',
|
||
rules: z.string().optional(),
|
||
},
|
||
];
|
||
}
|
||
|
||
// export function useGridFormSchema(): VbenFormSchema[] {
|
||
// return [
|
||
// {
|
||
// component: 'Input',
|
||
// fieldName: 'name',
|
||
// label: '部门名称',
|
||
// },
|
||
// {
|
||
// component: 'Select',
|
||
// componentProps: {
|
||
// allowClear: true,
|
||
// options: [
|
||
// { label: $t('common.enabled'), value: true },
|
||
// { label: $t('common.disabled'), value: false },
|
||
// ],
|
||
// },
|
||
// fieldName: 'enable',
|
||
// label: '状态',
|
||
// },
|
||
// // {
|
||
// // component: 'Input',
|
||
// // fieldName: 'remark',
|
||
// // label: '备注',
|
||
// // },
|
||
// // {
|
||
// // component: 'RangePicker',
|
||
// // fieldName: 'createTime',
|
||
// // label: '创建时间',
|
||
// // },
|
||
// ];
|
||
// }
|
||
|
||
/**
|
||
* 获取表格列配置
|
||
* @description 使用函数的形式返回列数据而不是直接export一个Array常量,是为了响应语言切换时重新翻译表头
|
||
* @param onActionClick 表格操作按钮点击事件
|
||
*/
|
||
export function useColumns(
|
||
onActionClick?: OnActionClickFn<DeptApi.Dept>,
|
||
): VxeTableGridOptions<DeptApi.Dept>['columns'] {
|
||
return [
|
||
{
|
||
align: 'left',
|
||
field: 'name',
|
||
fixed: 'left',
|
||
title: '部门名称',
|
||
treeNode: true,
|
||
width: 150,
|
||
},
|
||
{
|
||
cellRender: {
|
||
name: 'CellTag',
|
||
},
|
||
field: 'enabled',
|
||
title: '状态',
|
||
width: 100,
|
||
},
|
||
{
|
||
field: 'remark',
|
||
title: '备注',
|
||
},
|
||
{
|
||
field: 'createTime',
|
||
slots: { default: 'createTime' },
|
||
title: '创建时间',
|
||
width: 180,
|
||
},
|
||
{
|
||
align: 'right',
|
||
cellRender: {
|
||
attrs: {
|
||
nameField: 'name',
|
||
nameTitle: '部门',
|
||
onClick: onActionClick,
|
||
},
|
||
name: 'CellOperation',
|
||
options: [
|
||
{
|
||
code: 'append',
|
||
text: '新增下级',
|
||
},
|
||
'edit', // 默认的编辑按钮
|
||
{
|
||
code: 'delete', // 默认的删除按钮
|
||
disabled: (row: DeptApi.Dept) => {
|
||
return !!(row.children && row.children.length > 0);
|
||
},
|
||
},
|
||
],
|
||
},
|
||
field: 'operation',
|
||
fixed: 'right',
|
||
headerAlign: 'center',
|
||
showOverflow: false,
|
||
title: '操作',
|
||
width: 200,
|
||
},
|
||
];
|
||
}
|