<script lang="ts" setup>
|
import type { CategoryApi } from '#/api/infra/demo';
|
|
import { ref, h, reactive, onMounted, nextTick } from 'vue';
|
|
import { Page, useVbenModal } from '@vben/common-ui';
|
import { getDictOptions } from '@vben/hooks';
|
import { useTableToolbar, VbenVxeTableToolbar } from '@vben/plugins/vxe-table';
|
import { cloneDeep, downloadFileFromBlobPart, formatDateTime, isEmpty } from '@vben/utils';
|
import { Button, Card, message, Tabs, Pagination, Form, RangePicker, DatePicker, Select, Input } from 'ant-design-vue';
|
import CategoryForm from './modules/form.vue';
|
import { Download, Plus, RefreshCw, Search, Trash2 } from '@vben/icons';
|
import { DictTag } from '#/components/dict-tag';
|
import { VxeColumn, VxeTable } from '#/adapter/vxe-table';
|
import { getRangePickerDefaultProps } from '#/utils/rangePickerProps';
|
|
|
import { $t } from '#/locales';
|
import { handleTree } from '@vben/utils'
|
import { getCategoryList, deleteCategory, exportCategory } from '#/api/infra/demo';
|
|
|
const loading = ref(true) // 列表的加载中
|
const list = ref<any[]>([]) // 树列表的数据
|
|
const queryParams = reactive({
|
name: undefined,
|
})
|
const queryFormRef = ref() // 搜索的表单
|
const exportLoading = ref(false) // 导出的加载中
|
|
/** 查询列表 */
|
async function getList() {
|
loading.value = true
|
try {
|
const params = cloneDeep(queryParams) as any;
|
list.value = await getCategoryList(params);
|
} finally {
|
loading.value = false
|
}
|
}
|
|
/** 搜索按钮操作 */
|
function handleQuery() {
|
getList()
|
}
|
|
/** 重置按钮操作 */
|
function resetQuery() {
|
queryFormRef.value.resetFields()
|
handleQuery()
|
}
|
|
const [FormModal, formModalApi] = useVbenModal({
|
connectedComponent: CategoryForm,
|
destroyOnClose: true,
|
});
|
|
/** 创建分类 */
|
function handleCreate() {
|
formModalApi.setData(null).open();
|
}
|
|
/** 编辑分类 */
|
function handleEdit(row: CategoryApi.Category) {
|
formModalApi.setData(row).open();
|
}
|
|
/** 新增下级分类 */
|
function handleAppend(row: CategoryApi.Category) {
|
formModalApi.setData({ parentId: row.id }).open();
|
}
|
|
/** 删除分类 */
|
async function handleDelete(row: CategoryApi.Category) {
|
const hideLoading = message.loading({
|
content: $t('ui.actionMessage.deleting', [row.id]),
|
duration: 0,
|
});
|
try {
|
await deleteCategory(row.id!);
|
message.success($t('ui.actionMessage.deleteSuccess', [row.id]));
|
await getList();
|
} finally {
|
hideLoading();
|
}
|
}
|
|
|
/** 导出表格 */
|
async function handleExport() {
|
try {
|
exportLoading.value = true;
|
const data = await exportCategory(queryParams);
|
downloadFileFromBlobPart({ fileName: '分类.xls', source: data });
|
}finally {
|
exportLoading.value = false;
|
}
|
}
|
|
|
/** 切换树形展开/收缩状态 */
|
const isExpanded = ref(true);
|
function handleExpand() {
|
isExpanded.value = !isExpanded.value;
|
tableRef.value?.setAllTreeExpand(isExpanded.value);
|
}
|
|
/** 初始化 */
|
const { hiddenSearchBar, tableToolbarRef, tableRef } = useTableToolbar();
|
onMounted(() => {
|
getList();
|
});
|
</script>
|
|
<template>
|
<Page auto-content-height>
|
<FormModal @success="getList" />
|
|
<Card v-if="!hiddenSearchBar" class="mb-4">
|
<!-- 搜索工作栏 -->
|
<Form
|
:model="queryParams"
|
ref="queryFormRef"
|
layout="inline"
|
>
|
<Form.Item label="名字" name="name">
|
<Input
|
v-model:value="queryParams.name"
|
placeholder="请输入名字"
|
allowClear
|
@pressEnter="handleQuery"
|
class="w-full"
|
/>
|
</Form.Item>
|
<Form.Item>
|
<Button class="ml-2" @click="resetQuery"> 重置 </Button>
|
<Button class="ml-2" @click="handleQuery" type="primary">
|
搜索
|
</Button>
|
</Form.Item>
|
</Form>
|
</Card>
|
|
<!-- 列表 -->
|
<Card title="分类">
|
<template #extra>
|
<VbenVxeTableToolbar
|
ref="tableToolbarRef"
|
v-model:hidden-search="hiddenSearchBar"
|
>
|
<Button @click="handleExpand" class="mr-2">
|
{{ isExpanded ? '收缩' : '展开' }}
|
</Button>
|
<Button
|
class="ml-2"
|
:icon="h(Plus)"
|
type="primary"
|
@click="handleCreate"
|
v-access:code="['infra:category:create']"
|
>
|
{{ $t('ui.actionTitle.create', ['分类']) }}
|
</Button>
|
<Button
|
:icon="h(Download)"
|
type="primary"
|
class="ml-2"
|
:loading="exportLoading"
|
@click="handleExport"
|
v-access:code="['infra:category:export']"
|
>
|
{{ $t('ui.actionTitle.export') }}
|
</Button>
|
</VbenVxeTableToolbar>
|
</template>
|
<VxeTable
|
ref="tableRef"
|
:data="list"
|
:tree-config="{
|
parentField: 'parentId',
|
rowField: 'id',
|
transform: true,
|
expandAll: true,
|
reserve: true,
|
}"
|
show-overflow
|
:loading="loading"
|
>
|
<VxeColumn field="id" title="编号" align="center" />
|
<VxeColumn field="name" title="名字" align="center" tree-node/>
|
<VxeColumn field="parentId" title="父编号" align="center" />
|
<VxeColumn field="operation" title="操作" align="center">
|
<template #default="{row}">
|
<Button
|
size="small"
|
type="link"
|
@click="handleAppend(row)"
|
v-access:code="['infra:category:create']"
|
>
|
新增下级
|
</Button>
|
<Button
|
size="small"
|
type="link"
|
@click="handleEdit(row)"
|
v-access:code="['infra:category:update']"
|
>
|
{{ $t('ui.actionTitle.edit') }}
|
</Button>
|
<Button
|
size="small"
|
type="link"
|
danger
|
class="ml-2"
|
:disabled="!isEmpty(row?.children)"
|
@click="handleDelete(row)"
|
v-access:code="['infra:category:delete']"
|
>
|
{{ $t('ui.actionTitle.delete') }}
|
</Button>
|
</template>
|
</VxeColumn>
|
</VxeTable>
|
</Card>
|
</Page>
|
</template>
|