Files
iot-device-management-frontend/apps/web-antd/src/views/system/dict/modules/data-grid.vue

214 lines
5.3 KiB
Vue
Raw Normal View History

2025-04-06 18:56:42 +08:00
<script lang="ts" setup>
2025-05-19 11:21:04 +08:00
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { SystemDictDataApi } from '#/api/system/dict/data';
2025-04-06 18:56:42 +08:00
import { ref, watch } from 'vue';
2025-04-22 11:25:11 +08:00
2025-04-06 18:56:42 +08:00
import { useVbenModal } from '@vben/common-ui';
import { downloadFileFromBlobPart, isEmpty } from '@vben/utils';
2025-04-06 18:56:42 +08:00
2025-06-21 14:46:26 +08:00
import { message, Tag } from 'ant-design-vue';
2025-04-22 11:25:11 +08:00
import { ACTION_ICON, TableAction, useVbenVxeGrid } from '#/adapter/vxe-table';
2025-04-22 11:25:11 +08:00
import {
deleteDictData,
deleteDictDataList,
2025-04-22 11:25:11 +08:00
exportDictData,
getDictDataPage,
} from '#/api/system/dict/data';
import { $t } from '#/locales';
2025-04-06 18:56:42 +08:00
2025-04-22 11:25:11 +08:00
import { useDataGridColumns, useDataGridFormSchema } from '../data';
import DataForm from './data-form.vue';
2025-04-06 18:56:42 +08:00
const props = defineProps({
dictType: {
type: String,
default: undefined,
},
});
const [DataFormModal, dataFormModalApi] = useVbenModal({
connectedComponent: DataForm,
destroyOnClose: true,
});
/** 刷新表格 */
function onRefresh() {
gridApi.query();
}
/** 导出表格 */
2025-05-20 11:23:02 +08:00
async function handleExport() {
2025-04-06 18:56:42 +08:00
const data = await exportDictData(await gridApi.formApi.getValues());
downloadFileFromBlobPart({ fileName: '字典数据.xls', source: data });
2025-04-06 18:56:42 +08:00
}
/** 创建字典数据 */
2025-05-20 11:23:02 +08:00
function handleCreate() {
2025-04-06 18:56:42 +08:00
dataFormModalApi.setData({ dictType: props.dictType }).open();
}
/** 编辑字典数据 */
2025-05-20 11:23:02 +08:00
function handleEdit(row: SystemDictDataApi.DictData) {
2025-04-06 18:56:42 +08:00
dataFormModalApi.setData(row).open();
}
/** 删除字典数据 */
2025-05-20 11:23:02 +08:00
async function handleDelete(row: SystemDictDataApi.DictData) {
const hideLoading = message.loading({
2025-05-19 11:21:04 +08:00
content: $t('ui.actionMessage.deleting', [row.label]),
key: 'action_key_msg',
2025-04-06 18:56:42 +08:00
});
2025-05-20 11:23:02 +08:00
try {
await deleteDictData(row.id as number);
message.success({
content: $t('ui.actionMessage.deleteSuccess', [row.label]),
key: 'action_key_msg',
});
onRefresh();
} finally {
hideLoading();
}
}
const checkedIds = ref<number[]>([]);
function handleRowCheckboxChange({
records,
}: {
records: SystemDictDataApi.DictData[];
}) {
checkedIds.value = records.map((item) => item.id as number);
}
/** 批量删除字典数据 */
async function handleDeleteBatch() {
const hideLoading = message.loading({
content: $t('ui.actionMessage.deleting'),
duration: 0,
key: 'action_process_msg',
});
try {
await deleteDictDataList(checkedIds.value);
checkedIds.value = [];
message.success($t('ui.actionMessage.deleteSuccess'));
2025-05-20 11:23:02 +08:00
onRefresh();
} finally {
hideLoading();
}
2025-04-06 18:56:42 +08:00
}
const [Grid, gridApi] = useVbenVxeGrid({
formOptions: {
schema: useDataGridFormSchema(),
},
gridOptions: {
2025-05-19 11:21:04 +08:00
columns: useDataGridColumns(),
2025-04-06 18:56:42 +08:00
height: 'auto',
keepSource: true,
proxyConfig: {
ajax: {
query: async ({ page }, formValues) => {
return await getDictDataPage({
pageNo: page.currentPage,
pageSize: page.pageSize,
dictType: props.dictType,
...formValues,
2025-04-06 18:56:42 +08:00
});
},
},
},
rowConfig: {
keyField: 'id',
isHover: true,
2025-04-06 18:56:42 +08:00
},
toolbarConfig: {
2025-07-19 17:15:39 +08:00
refresh: true,
2025-04-06 18:56:42 +08:00
search: true,
2025-04-22 11:25:11 +08:00
},
} as VxeTableGridOptions<SystemDictDataApi.DictData>,
gridEvents: {
checkboxAll: handleRowCheckboxChange,
checkboxChange: handleRowCheckboxChange,
},
2025-04-06 18:56:42 +08:00
});
/** 监听 dictType 变化,重新查询 */
watch(
() => props.dictType,
() => {
if (props.dictType) {
onRefresh();
}
},
);
</script>
<template>
2025-04-22 11:25:11 +08:00
<div class="flex h-full flex-col">
2025-04-06 18:56:42 +08:00
<DataFormModal @success="onRefresh" />
<Grid table-title="字典数据列表">
<template #toolbar-tools>
2025-05-19 16:31:17 +08:00
<TableAction
:actions="[
{
label: $t('ui.actionTitle.create', ['字典数据']),
type: 'primary',
icon: ACTION_ICON.ADD,
auth: ['system:dict:create'],
2025-05-20 11:23:02 +08:00
onClick: handleCreate,
2025-05-19 16:31:17 +08:00
},
{
label: $t('ui.actionTitle.export'),
type: 'primary',
icon: ACTION_ICON.DOWNLOAD,
auth: ['system:dict:export'],
2025-05-20 11:23:02 +08:00
onClick: handleExport,
2025-05-19 16:31:17 +08:00
},
{
label: '批量删除',
type: 'primary',
danger: true,
disabled: isEmpty(checkedIds),
icon: ACTION_ICON.DELETE,
auth: ['system:dict:delete'],
onClick: handleDeleteBatch,
},
2025-05-19 16:31:17 +08:00
]"
/>
2025-04-06 18:56:42 +08:00
</template>
2025-06-21 14:46:26 +08:00
<template #colorType="{ row }">
<Tag :color="row.colorType">{{ row.colorType }}</Tag>
</template>
<template #cssClass="{ row }">
<Tag :color="row.cssClass">{{ row.cssClass }}</Tag>
</template>
2025-05-19 11:21:04 +08:00
<template #actions="{ row }">
<TableAction
:actions="[
{
label: $t('common.edit'),
type: 'link',
2025-05-19 16:31:17 +08:00
icon: ACTION_ICON.EDIT,
2025-05-19 11:21:04 +08:00
auth: ['system:dict:update'],
2025-05-20 11:23:02 +08:00
onClick: handleEdit.bind(null, row),
2025-05-19 11:21:04 +08:00
},
{
label: $t('common.delete'),
type: 'link',
danger: true,
2025-05-19 16:31:17 +08:00
icon: ACTION_ICON.DELETE,
2025-05-19 11:21:04 +08:00
auth: ['system:dict:delete'],
popConfirm: {
title: $t('ui.actionMessage.deleteConfirm', [row.label]),
2025-05-20 11:23:02 +08:00
confirm: handleDelete.bind(null, row),
2025-05-19 11:21:04 +08:00
},
},
]"
/>
</template>
2025-04-06 18:56:42 +08:00
</Grid>
</div>
</template>