<template> <!-- 认证用户组管理 --> <div class="fansInfoGroup"> <div class="searchBoxs"> <n-space> <n-input v-model:value="searchVal1" placeholder="请输入分组名称" style="width: 200px" clearable> </n-input> <n-button type="primary" @click="handleClick('search')"> <template #icon> <n-icon> <Search /> </n-icon> </template >搜索 </n-button> <n-button type="primary" @click="handleClick('add')"> <template #icon> <n-icon> <Add /> </n-icon> </template >新增分组 </n-button> </n-space> </div> <!-- 表格 --> <div class="tableBox"> <n-data-table :bordered="false" :max-height="700" striped :columns="columns" :data="tableData" :loading="tableLoading" :remote="true" :pagination="pagination" > </n-data-table> </div> <!-- 新增修改弹窗 --> <n-modal :title="modalTitle" :mask-closable="false" preset="dialog" :show-icon="false" :style="{ width: '600px' }" v-model:show="modalShow" > <n-form ref="formRef" :label-width="120" :rules="formInfo.rules" :model="formInfo.data" label-placement="left"> <n-form-item label="分组名称:" path="groupName"> <n-input v-model:value="formInfo.data.groupName" placeholder="请输入分组名称" /> </n-form-item> </n-form> <template #action> <n-space> <n-button @click="() => (modalShow = false)">取消</n-button> <n-button type="primary" @click="handleClick('submit')">确定 </n-button> </n-space> </template> </n-modal> </div> </template> <script> import { toRefs, onMounted, reactive, h, ref, nextTick } from 'vue'; import { Search, Add } from '@vicons/ionicons5'; import { NButton } from 'naive-ui'; import { userGroupSearch, userGroupSave, userGroupUpdate, userGroupDelete } from '@/services'; export default { name: 'fansInfoGroup', components: { Search, Add, NButton }, setup() { const allData = reactive({ searchVal1: null, modalTitle: '新增', modalShow: false, uploadList: [], formInfo: { data: { groupName: '', id: '', }, rules: { groupName: { required: true, trigger: ['blur'], message: '请输入组名称', }, }, }, tableLoading: true, tableData: [], columns: [ { title: '序号', align: 'center', width: '80', render(row, index) { return (paginationReactive.page - 1) * paginationReactive.pageSize + index + 1; }, }, { title: '分组名称', align: 'center', key: 'groupName' }, { title: '组内人数', align: 'center', key: 'groupNum' }, { title: '创建时间', align: 'center', key: 'createTime' }, { title: '修改时间', align: 'center', key: 'updateTime' }, { title: '操作', key: 'actions', align: 'center', render(row) { const btn = allData.actionColumn.map((item, index) => { return h( NButton, { text: true, size: item.size, style: { margin: '10px', }, type: item.btnType, onClick: () => handleClick(item.type, row), }, { default: () => item.default } ); }); return btn; }, }, ], actionColumn: [ { size: 'small', btnType: 'primary', type: 'edit', default: '修改', }, { size: 'small', btnType: 'error', type: 'delete', default: '删除', }, ], }); //分页 const paginationReactive = reactive({ page: 1, pageSize: 10, showSizePicker: true, pageSizes: [10, 20, 50], showQuickJumper: true, pageCount: 0, itemCount: 0, prefix: () => { return '共 ' + paginationReactive.itemCount + ' 项'; }, onChange: (page) => { paginationReactive.page = page; getTableData(); }, onPageSizeChange: (pageSize) => { paginationReactive.pageSize = pageSize; paginationReactive.page = 1; getTableData(); }, }); const getTableData = async () => { allData.tableLoading = true; let pramas = { current: paginationReactive.page, size: paginationReactive.pageSize, groupName: allData.searchVal1, }; let res = await userGroupSearch(pramas); if (res && res.code == 200) { allData.tableData = res.data.records; paginationReactive.pageCount = res.data.pages; paginationReactive.itemCount = res.data.total; } allData.tableLoading = false; }; const formRef = ref(null); // 点击事件 const handleClick = async (type, row) => { switch (type) { case 'search': paginationReactive.page = 1; getTableData(); break; case 'add': allData.modalTitle = '新增'; allData.formInfo.data.groupName = ''; if (allData.formInfo.data.id) delete allData.formInfo.data.id; allData.modalShow = true; break; case 'edit': allData.modalTitle = '修改'; allData.formInfo.data = { ...row }; allData.modalShow = true; break; case 'submit': formRef.value.validate((errors) => { if (!errors) { submitData(); } else { $message.error('验证失败,请检查必填项'); } }); break; case 'delete': $dialog.info({ title: '提示', content: `确定删除该数据吗?`, positiveText: '确定', negativeText: '取消', onPositiveClick: () => { let ids = [row.id]; dataDel(ids); }, }); break; } }; // 删除数据 async function dataDel(ids) { let res = await userGroupDelete({ ids: ids }); if (res && res.code === 200) { $message.success('删除成功'); getTableData(); } } // 提交数据 async function submitData() { let params = { ...allData.formInfo.data }; if (allData.modalTitle == '新增') { let res = await userGroupSave(params); if (res && res.code == 200) { $message.success('操作成功'); getTableData(); allData.modalShow = false; } } else { let res = await userGroupUpdate(params); if (res && res.code == 200) { $message.success('操作成功'); getTableData(); allData.modalShow = false; } } } onMounted(() => { getTableData(); }); return { ...toRefs(allData), pagination: paginationReactive, handleClick, getTableData, formRef, }; }, }; </script> <style lang="less" scoped> .fansInfoGroup { width: 100%; .searchBoxs { margin: 10px; } } </style>