322 lines
8.3 KiB
Smarty
322 lines
8.3 KiB
Smarty
<template>
|
|
<div>
|
|
<!-- 搜索表单卡片 -->
|
|
<n-card :bordered="false" class="pt-3 mb-3 proCard">
|
|
<BasicForm @register="register" @submit="handleSubmit" @reset="handleReset" />
|
|
</n-card>
|
|
|
|
<!-- 数据表格卡片 -->
|
|
<n-card :bordered="false" class="proCard">
|
|
<BasicTable
|
|
:columns="columns"
|
|
:request="loadDataTable"
|
|
:row-key="(row) => row.id"
|
|
ref="basicTableRef"
|
|
:actionColumn="actionColumn"
|
|
@update:checked-row-keys="onCheckedRow"
|
|
:auto-scroll-x="true"
|
|
>
|
|
<!-- 表格顶部工具栏 -->
|
|
<template #tableTitle>
|
|
<n-space>
|
|
<!-- 新增按钮 -->
|
|
<n-button type="primary" @click="handleAdd" v-perm="['{{ permission_prefix }}:add']">
|
|
<template #icon>
|
|
<n-icon>
|
|
<PlusOutlined />
|
|
</n-icon>
|
|
</template>
|
|
添加{{ module_comment }}
|
|
</n-button>
|
|
|
|
<!-- 批量删除按钮 -->
|
|
<n-button
|
|
type="error"
|
|
@click="handleDelete()"
|
|
:disabled="!rowKeys.length"
|
|
v-perm="['{{ permission_prefix }}:batchDelete']"
|
|
>
|
|
<template #icon>
|
|
<n-icon>
|
|
<DeleteOutlined />
|
|
</n-icon>
|
|
</template>
|
|
批量删除
|
|
</n-button>
|
|
{% if has_export %}
|
|
<!-- 导出按钮 -->
|
|
<n-button
|
|
type="success"
|
|
@click="handleExport"
|
|
v-perm="['{{ permission_prefix }}:export']"
|
|
>
|
|
<template #icon>
|
|
<n-icon>
|
|
<DownloadOutlined />
|
|
</n-icon>
|
|
</template>
|
|
导出
|
|
</n-button>
|
|
{% endif %}
|
|
</n-space>
|
|
</template>
|
|
</BasicTable>
|
|
</n-card>
|
|
|
|
<!-- 编辑对话框组件 -->
|
|
<editDialog
|
|
ref="editModalRef"
|
|
v-if="editVisible"
|
|
:{{ primary_key }}="currentId"
|
|
v-model:visible="editVisible"
|
|
@success="reloadTable('noRefresh')"
|
|
/>
|
|
<!-- 查看详情对话框组件 -->
|
|
<detailDialog
|
|
ref="detailModalRef"
|
|
v-if="detailVisible"
|
|
:{{ primary_key }}="currentId"
|
|
v-model:visible="detailVisible"
|
|
@success="reloadTable('noRefresh')"
|
|
/>
|
|
</div>
|
|
</template>
|
|
|
|
<script lang="ts" setup>
|
|
import { h, nextTick, reactive, ref } from 'vue';
|
|
import { useMessage, useDialog } from 'naive-ui';
|
|
import { PlusOutlined, DeleteOutlined, EditOutlined, EyeOutlined } from '@vicons/antd';
|
|
import { TableAction } from '@/components/Table';
|
|
import { BasicForm, useForm } from '@/components/Form/index';
|
|
import { get{{ model_class_name }}Page, {{ model_class_name_camel }}Delete, {{ model_class_name_camel }}BatchDelete } from '@/api/{{ api_path }}';
|
|
import { renderIcon } from '@/utils';
|
|
|
|
import { columns } from './columns';
|
|
import { schemas } from './querySchemas';
|
|
|
|
// 动态导入编辑对话框组件
|
|
import editDialog from './edit.vue';
|
|
// 动态导入详情对话框组件
|
|
import detailDialog from './detail.vue';
|
|
|
|
// 消息提示和对话框
|
|
const message = useMessage();
|
|
const dialog = useDialog();
|
|
|
|
// 表格引用
|
|
const basicTableRef = ref();
|
|
const editModalRef = ref();
|
|
const detailModalRef = ref();
|
|
|
|
// 响应式状态定义
|
|
const currentId = ref(0); // 当前操作的ID
|
|
const editVisible = ref(false); // 编辑对话框显示状态
|
|
const detailVisible = ref(false); // 详情对话框显示状态
|
|
const rowKeys = ref([]); // 表格选中行Key
|
|
|
|
/**
|
|
* 定义查询参数
|
|
* 用于存储搜索表单的值
|
|
*/
|
|
const formParams = reactive({
|
|
{% for field in filter_fields %}
|
|
{{ field.name }}: '',
|
|
{% endfor %}
|
|
});
|
|
|
|
/**
|
|
* 定义操作栏配置
|
|
* 包含编辑、详情和删除按钮
|
|
*/
|
|
const actionColumn = reactive({
|
|
width: 280,
|
|
title: '操作',
|
|
align: 'center',
|
|
key: 'action',
|
|
fixed: 'right',
|
|
render(record) {
|
|
return h(TableAction as any, {
|
|
style: 'button',
|
|
actions: [
|
|
{
|
|
label: '编辑',
|
|
icon: renderIcon(EditOutlined),
|
|
type: 'info',
|
|
onClick: handleEdit.bind(null, record),
|
|
auth: ['{{ permission_prefix }}:update'],
|
|
},
|
|
{
|
|
label: '详情',
|
|
icon: renderIcon(EyeOutlined),
|
|
type: 'default',
|
|
onClick: handleDetail.bind(null, record),
|
|
auth: ['{{ permission_prefix }}:detail'],
|
|
},
|
|
{
|
|
label: '删除',
|
|
icon: renderIcon(DeleteOutlined),
|
|
type: 'error',
|
|
onClick: handleDelete.bind(null, record),
|
|
auth: ['{{ permission_prefix }}:delete'],
|
|
},
|
|
],
|
|
});
|
|
},
|
|
});
|
|
|
|
/**
|
|
* 加载数据列表
|
|
* @param params 分页参数
|
|
* @returns 表格数据
|
|
*/
|
|
const loadDataTable = async (params: any) => {
|
|
// 清空选中行
|
|
rowKeys.value = [];
|
|
const result = await get{{ model_class_name }}Page({ ...formParams, ...params });
|
|
return result;
|
|
};
|
|
|
|
/**
|
|
* 刷新表格数据
|
|
* @param noRefresh 是否重置页码
|
|
*/
|
|
function reloadTable(noRefresh = '') {
|
|
basicTableRef.value.reload(noRefresh ? {} : { pageNo: 1 });
|
|
}
|
|
|
|
/**
|
|
* 注册搜索表单
|
|
* 配置表单布局和字段
|
|
*/
|
|
const [register, {}] = useForm({
|
|
gridProps: { cols: '1 s:1 m:2 l:3 xl:4 2xl:4' },
|
|
labelWidth: 80,
|
|
schemas,
|
|
});
|
|
|
|
/**
|
|
* 提交搜索表单
|
|
* @param values 表单值
|
|
*/
|
|
function handleSubmit(values: Recordable) {
|
|
for (const key in formParams) {
|
|
formParams[key] = '';
|
|
}
|
|
for (const key in values) {
|
|
formParams[key] = values[key];
|
|
}
|
|
reloadTable();
|
|
}
|
|
|
|
/**
|
|
* 重置搜索表单
|
|
*/
|
|
function handleReset(values: Recordable) {
|
|
for (const key in formParams) {
|
|
formParams[key] = '';
|
|
}
|
|
for (const key in values) {
|
|
formParams[key] = values[key];
|
|
}
|
|
reloadTable();
|
|
}
|
|
|
|
/**
|
|
* 表格选择项变化时的回调
|
|
* @param keys 选中的key数组
|
|
*/
|
|
function onCheckedRow(keys: (string | number)[]) {
|
|
rowKeys.value = keys;
|
|
}
|
|
|
|
/**
|
|
* 处理新增按钮点击
|
|
*/
|
|
const handleAdd = async () => {
|
|
currentId.value = 0;
|
|
editVisible.value = true;
|
|
await nextTick();
|
|
editModalRef.value?.openModal();
|
|
};
|
|
|
|
/**
|
|
* 处理编辑按钮点击
|
|
* @param record 当前行数据
|
|
*/
|
|
const handleEdit = async (record: Recordable) => {
|
|
currentId.value = record.id;
|
|
editVisible.value = true;
|
|
await nextTick();
|
|
editModalRef.value?.openModal();
|
|
};
|
|
|
|
/**
|
|
* 处理详情按钮点击
|
|
* @param record 当前行数据
|
|
*/
|
|
const handleDetail = async (record: Recordable) => {
|
|
currentId.value = record.id;
|
|
detailVisible.value = true;
|
|
await nextTick();
|
|
detailModalRef.value?.openModal();
|
|
};
|
|
|
|
/**
|
|
* 处理删除操作
|
|
* @param record 当前行数据(可选,不传时表示批量删除)
|
|
*/
|
|
async function handleDelete(record?: Recordable) {
|
|
let ids = [];
|
|
if (!record) {
|
|
// 批量删除:获取选中行的ID
|
|
ids = rowKeys.value;
|
|
if (!ids.length) {
|
|
message.warning('请选择要删除的数据');
|
|
return;
|
|
}
|
|
}
|
|
|
|
dialog.warning({
|
|
title: '提示',
|
|
content: '确定要删除选中的{{ module_comment }}吗?',
|
|
positiveText: '确定',
|
|
negativeText: '取消',
|
|
onPositiveClick: async () => {
|
|
try {
|
|
record
|
|
? await {{ model_class_name_camel }}Delete(record.id)
|
|
: await {{ model_class_name_camel }}BatchDelete(ids);
|
|
message.success('删除成功');
|
|
reloadTable();
|
|
} catch (error) {
|
|
message.error('删除失败');
|
|
}
|
|
},
|
|
});
|
|
}
|
|
|
|
{% if has_export %}
|
|
/**
|
|
* 处理导出操作
|
|
*/
|
|
async function handleExport() {
|
|
dialog.warning({
|
|
title: '提示',
|
|
content: '确定要导出{{ module_comment }}数据吗?',
|
|
positiveText: '确定',
|
|
negativeText: '取消',
|
|
onPositiveClick: async () => {
|
|
try {
|
|
// TODO: 调用导出API
|
|
message.success('导出成功');
|
|
} catch (error) {
|
|
message.error('导出失败');
|
|
console.error('导出失败', error);
|
|
}
|
|
},
|
|
});
|
|
}
|
|
{% endif %}
|
|
</script>
|
|
|
|
<style lang="less" scoped></style> |