Files
DjangoAdmin_Django_EleVue/public/templates/ui/index.vue.tpl
T
2026-09-11 11:39:07 +08:00

278 lines
7.3 KiB
Smarty

<template>
<PageWrapper>
<!-- 搜索表单卡片 -->
<el-card :bordered="false" class="pt-3 mb-3 proCard">
<BasicForm @register="register" @submit="handleSubmit" @reset="handleReset" />
</el-card>
<!-- 数据表格卡片 -->
<el-card :bordered="false" class="proCard">
<BasicTable
:columns="columns"
:request="loadDataTable"
:row-key="(row) => row.id"
ref="tableRef"
:actionColumn="actionColumn"
@selection-change="onSelectionChange"
>
<!-- 表格顶部工具栏 -->
<template #tableTitle>
<!-- 新增按钮 -->
<el-button type="primary" @click="handleAdd" v-perm="['{{ permission_prefix }}:add']">
<template #icon>
<el-icon class="el-input__icon">
<PlusOutlined />
</el-icon>
</template>
添加{{ module_comment }}
</el-button>
<!-- 批量删除按钮 -->
<el-button
type="danger"
@click="handleDelete()"
:disabled="!selectionData.length"
v-perm="['{{ permission_prefix }}:batchDelete']"
>
<template #icon>
<el-icon class="el-input__icon">
<Delete />
</el-icon>
</template>
批量删除
</el-button>
{% if has_export %}
<!-- 导出按钮 -->
<el-button
type="success"
@click="handleExport"
v-perm="['{{ permission_prefix }}:export']"
>
<template #icon>
<el-icon class="el-input__icon">
<Download />
</el-icon>
</template>
导出
</el-button>
{% endif %}
</template>
</BasicTable>
</el-card>
<!-- 编辑对话框组件 -->
<editDialog
v-if="editVisible"
:{{ primary_key }}="currentId"
v-model:visible="editVisible"
@success="reloadTable('noRefresh')"
/>
<!-- 查看详情对话框组件 -->
<detailDialog
v-if="detailVisible"
:{{ primary_key }}="currentId"
v-model:visible="detailVisible"
@success="reloadTable('noRefresh')"
/>
</PageWrapper>
</template>
<script lang="ts" setup>
import { reactive, ref, h, nextTick, defineAsyncComponent } from 'vue';
import { ColProps } from 'element-plus';
import { schemas } from './querySchemas';
import { useForm } from '@/components/Form/index';
import { TableAction } from '@/components/Table';
import { get{{ model_class_name }}Page, {{ model_class_name_camel }}Delete, {{ model_class_name_camel }}BatchDelete } from '@/api/{{ api_path }}';
import { columns } from './columns';
import { PlusOutlined } from '@vicons/antd';
import { message, confirm } from '@/utils/auth';
// 动态导入编辑对话框组件
const editDialog = defineAsyncComponent(() => import('./edit.vue'));
// 动态导入详情对话框组件
const detailDialog = defineAsyncComponent(() => import('./detail.vue'));
// 响应式状态定义
const currentId = ref(0); // 当前操作的ID
const editVisible = ref(false); // 编辑对话框显示状态
const detailVisible = ref(false); // 详情对话框显示状态
const selectionData = ref([]); // 表格选中数据
const tableRef = ref(); // 表格组件引用
/**
* 定义查询参数
* 用于存储搜索表单的值
*/
const formParams = reactive({
{% for field in filter_fields %}
{{ field.name }}: '',
{% endfor %}
});
/**
* 定义操作栏配置
* 包含编辑、详情和删除按钮
*/
const actionColumn = reactive({
width: 300,
label: '操作',
prop: 'action',
fixed: 'right',
render(record) {
return h(TableAction, {
style: 'button',
actions: [
{
label: '编辑',
icon: 'Edit',
type: 'warning',
onClick: handleEdit.bind(null, record),
auth: ['{{ permission_prefix }}:update'],
},
{
label: '详情',
icon: 'View',
type: '',
onClick: handleDetail.bind(null, record),
auth: ['{{ permission_prefix }}:detail'],
},
{
label: '删除',
icon: 'Delete',
type: 'danger',
onClick: handleDelete.bind(null, record),
auth: ['{{ permission_prefix }}:delete'],
},
],
});
},
});
/**
* 加载数据列表
* @param params 分页参数
* @returns 表格数据
*/
const loadDataTable = async (params: any) => {
const result = await get{{ model_class_name }}Page({ ...formParams, ...params });
return result;
};
/**
* 刷新表格数据
* @param noRefresh 是否重置页码
*/
function reloadTable(noRefresh = '') {
tableRef.value.reload(noRefresh ? {} : { pageNo: 1 });
}
/**
* 注册搜索表单
* 配置表单布局和字段
*/
const [register, {}] = useForm({
labelWidth: 80,
layout: 'horizontal',
colProps: { span: 6 } as ColProps,
submitOnReset: true,
schemas,
});
/**
* 提交搜索表单
* @param values 表单值
*/
function handleSubmit(values: Recordable) {
handleReset();
for (const key in values) {
formParams[key] = values[key];
}
reloadTable();
}
/**
* 重置搜索表单
*/
function handleReset() {
for (const key in formParams) {
formParams[key] = '';
}
}
/**
* 处理新增按钮点击
*/
const handleAdd = async () => {
currentId.value = 0;
await nextTick();
editVisible.value = true;
};
/**
* 处理编辑按钮点击
* @param record 当前行数据
*/
const handleEdit = async (record: Recordable) => {
currentId.value = record.row.id;
await nextTick();
editVisible.value = true;
};
/**
* 处理详情按钮点击
* @param record 当前行数据
*/
const handleDetail = async (record: Recordable) => {
currentId.value = record.row.id;
await nextTick();
detailVisible.value = true;
};
/**
* 处理删除操作
* @param record 当前行数据(可选,不传时表示批量删除)
*/
async function handleDelete(record: Recordable) {
let ids = [];
if (!record) {
// 批量删除:获取选中行的ID
ids = selectionData.value.map(({ id }) => id);
if (!ids.length) {
message('请选择要删除的数据', 'warning');
return;
}
}
// 确认删除
await confirm('确定要删除选中的{{ module_comment }}吗?');
// 执行删除操作
record ? await {{ model_class_name_camel }}Delete(record.row.id) : await {{ model_class_name_camel }}BatchDelete(ids);
message('删除成功');
reloadTable();
}
/**
* 表格选择项变化时的回调
* @param value 选中的数据
*/
function onSelectionChange(value) {
selectionData.value = value;
}
{% if has_export %}
/**
* 处理导出操作
*/
async function handleExport() {
try {
await confirm('确定要导出{{ module_comment }}数据吗?');
// TODO: 调用导出API
message('导出成功');
} catch (error) {
console.error('导出失败', error);
}
}
{% endif %}
</script>
<style lang="scss" scoped></style>