@ -1,19 +1,191 @@ | |||||
package org.jeecg.modules.employCategory.service.impl; | package org.jeecg.modules.employCategory.service.impl; | ||||
import org.jeecg.common.exception.JeecgBootException; | |||||
import org.jeecg.common.util.oConvertUtils; | |||||
import org.jeecg.modules.employCategory.entity.EmployCategory; | import org.jeecg.modules.employCategory.entity.EmployCategory; | ||||
import org.jeecg.modules.employCategory.mapper.EmployCategoryMapper; | import org.jeecg.modules.employCategory.mapper.EmployCategoryMapper; | ||||
import org.jeecg.modules.employCategory.service.IEmployCategoryService; | import org.jeecg.modules.employCategory.service.IEmployCategoryService; | ||||
import org.springframework.stereotype.Service; | import org.springframework.stereotype.Service; | ||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | |||||
import org.springframework.transaction.annotation.Transactional; | |||||
import java.util.ArrayList; | |||||
import java.util.Arrays; | |||||
import java.util.List; | |||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; | ||||
/** | /** | ||||
* @Description: 分类表 | * @Description: 分类表 | ||||
* @Author: jeecg-boot | * @Author: jeecg-boot | ||||
* @Date: 2025-02-21 | |||||
* @Date: 2025-02-26 | |||||
* @Version: V1.0 | * @Version: V1.0 | ||||
*/ | */ | ||||
@Service | @Service | ||||
public class EmployCategoryServiceImpl extends ServiceImpl<EmployCategoryMapper, EmployCategory> implements IEmployCategoryService { | public class EmployCategoryServiceImpl extends ServiceImpl<EmployCategoryMapper, EmployCategory> implements IEmployCategoryService { | ||||
@Override | |||||
public void addEmployCategory(EmployCategory employCategory) { | |||||
//新增时设置hasChild为0 | |||||
employCategory.setHasChild(IEmployCategoryService.NOCHILD); | |||||
if(oConvertUtils.isEmpty(employCategory.getPid())){ | |||||
employCategory.setPid(IEmployCategoryService.ROOT_PID_VALUE); | |||||
}else{ | |||||
//如果当前节点父ID不为空 则设置父节点的hasChildren 为1 | |||||
EmployCategory parent = baseMapper.selectById(employCategory.getPid()); | |||||
if(parent!=null && !"1".equals(parent.getHasChild())){ | |||||
parent.setHasChild("1"); | |||||
baseMapper.updateById(parent); | |||||
} | |||||
} | |||||
baseMapper.insert(employCategory); | |||||
} | |||||
@Override | |||||
public void updateEmployCategory(EmployCategory employCategory) { | |||||
EmployCategory entity = this.getById(employCategory.getId()); | |||||
if(entity==null) { | |||||
throw new JeecgBootException("未找到对应实体"); | |||||
} | |||||
String old_pid = entity.getPid(); | |||||
String new_pid = employCategory.getPid(); | |||||
if(!old_pid.equals(new_pid)) { | |||||
updateOldParentNode(old_pid); | |||||
if(oConvertUtils.isEmpty(new_pid)){ | |||||
employCategory.setPid(IEmployCategoryService.ROOT_PID_VALUE); | |||||
} | |||||
if(!IEmployCategoryService.ROOT_PID_VALUE.equals(employCategory.getPid())) { | |||||
baseMapper.updateTreeNodeStatus(employCategory.getPid(), IEmployCategoryService.HASCHILD); | |||||
} | |||||
} | |||||
baseMapper.updateById(employCategory); | |||||
} | |||||
@Override | |||||
@Transactional(rollbackFor = Exception.class) | |||||
public void deleteEmployCategory(String id) throws JeecgBootException { | |||||
//查询选中节点下所有子节点一并删除 | |||||
id = this.queryTreeChildIds(id); | |||||
if(id.indexOf(",")>0) { | |||||
StringBuffer sb = new StringBuffer(); | |||||
String[] idArr = id.split(","); | |||||
for (String idVal : idArr) { | |||||
if(idVal != null){ | |||||
EmployCategory employCategory = this.getById(idVal); | |||||
String pidVal = employCategory.getPid(); | |||||
//查询此节点上一级是否还有其他子节点 | |||||
List<EmployCategory> dataList = baseMapper.selectList(new QueryWrapper<EmployCategory>().eq("pid", pidVal).notIn("id",Arrays.asList(idArr))); | |||||
if((dataList == null || dataList.size()==0) && !Arrays.asList(idArr).contains(pidVal) | |||||
&& !sb.toString().contains(pidVal)){ | |||||
//如果当前节点原本有子节点 现在木有了,更新状态 | |||||
sb.append(pidVal).append(","); | |||||
} | |||||
} | |||||
} | |||||
//批量删除节点 | |||||
baseMapper.deleteBatchIds(Arrays.asList(idArr)); | |||||
//修改已无子节点的标识 | |||||
String[] pidArr = sb.toString().split(","); | |||||
for(String pid : pidArr){ | |||||
this.updateOldParentNode(pid); | |||||
} | |||||
}else{ | |||||
EmployCategory employCategory = this.getById(id); | |||||
if(employCategory==null) { | |||||
throw new JeecgBootException("未找到对应实体"); | |||||
} | |||||
updateOldParentNode(employCategory.getPid()); | |||||
baseMapper.deleteById(id); | |||||
} | |||||
} | |||||
@Override | |||||
public List<EmployCategory> queryTreeListNoPage(QueryWrapper<EmployCategory> queryWrapper) { | |||||
List<EmployCategory> dataList = baseMapper.selectList(queryWrapper); | |||||
List<EmployCategory> mapList = new ArrayList<>(); | |||||
for(EmployCategory data : dataList){ | |||||
String pidVal = data.getPid(); | |||||
//递归查询子节点的根节点 | |||||
if(pidVal != null && !"0".equals(pidVal)){ | |||||
EmployCategory rootVal = this.getTreeRoot(pidVal); | |||||
if(rootVal != null && !mapList.contains(rootVal)){ | |||||
mapList.add(rootVal); | |||||
} | |||||
}else{ | |||||
if(!mapList.contains(data)){ | |||||
mapList.add(data); | |||||
} | |||||
} | |||||
} | |||||
return mapList; | |||||
} | |||||
/** | |||||
* 根据所传pid查询旧的父级节点的子节点并修改相应状态值 | |||||
* @param pid | |||||
*/ | |||||
private void updateOldParentNode(String pid) { | |||||
if(!IEmployCategoryService.ROOT_PID_VALUE.equals(pid)) { | |||||
Integer count = Math.toIntExact(baseMapper.selectCount(new QueryWrapper<EmployCategory>().eq("pid", pid))); | |||||
if(count==null || count<=1) { | |||||
baseMapper.updateTreeNodeStatus(pid, IEmployCategoryService.NOCHILD); | |||||
} | |||||
} | |||||
} | |||||
/** | |||||
* 递归查询节点的根节点 | |||||
* @param pidVal | |||||
* @return | |||||
*/ | |||||
private EmployCategory getTreeRoot(String pidVal){ | |||||
EmployCategory data = baseMapper.selectById(pidVal); | |||||
if(data != null && !"0".equals(data.getPid())){ | |||||
return this.getTreeRoot(data.getPid()); | |||||
}else{ | |||||
return data; | |||||
} | |||||
} | |||||
/** | |||||
* 根据id查询所有子节点id | |||||
* @param ids | |||||
* @return | |||||
*/ | |||||
private String queryTreeChildIds(String ids) { | |||||
//获取id数组 | |||||
String[] idArr = ids.split(","); | |||||
StringBuffer sb = new StringBuffer(); | |||||
for (String pidVal : idArr) { | |||||
if(pidVal != null){ | |||||
if(!sb.toString().contains(pidVal)){ | |||||
if(sb.toString().length() > 0){ | |||||
sb.append(","); | |||||
} | |||||
sb.append(pidVal); | |||||
this.getTreeChildIds(pidVal,sb); | |||||
} | |||||
} | |||||
} | |||||
return sb.toString(); | |||||
} | |||||
/** | |||||
* 递归查询所有子节点 | |||||
* @param pidVal | |||||
* @param sb | |||||
* @return | |||||
*/ | |||||
private StringBuffer getTreeChildIds(String pidVal,StringBuffer sb){ | |||||
List<EmployCategory> dataList = baseMapper.selectList(new QueryWrapper<EmployCategory>().eq("pid", pidVal)); | |||||
if(dataList != null && dataList.size()>0){ | |||||
for(EmployCategory tree : dataList) { | |||||
if(!sb.toString().contains(tree.getId())){ | |||||
sb.append(",").append(tree.getId()); | |||||
} | |||||
this.getTreeChildIds(tree.getId(),sb); | |||||
} | |||||
} | |||||
return sb; | |||||
} | |||||
} | } |
@ -1,162 +1,272 @@ | |||||
<template> | <template> | ||||
<div> | |||||
<div class="p-4"> | |||||
<!--引用表格--> | <!--引用表格--> | ||||
<BasicTable @register="registerTable" :rowSelection="rowSelection"> | |||||
<!--插槽:table标题--> | |||||
<BasicTable @register="registerTable" :rowSelection="rowSelection" :expandedRowKeys="expandedRowKeys" @expand="handleExpand" @fetch-success="onFetchSuccess"> | |||||
<!--插槽:table标题--> | |||||
<template #tableTitle> | <template #tableTitle> | ||||
<a-button type="primary" @click="handleAdd" preIcon="ant-design:plus-outlined"> 新增</a-button> | |||||
<a-button type="primary" @click="handleCreate" preIcon="ant-design:plus-outlined"> 新增</a-button> | |||||
<a-button type="primary" preIcon="ant-design:export-outlined" @click="onExportXls"> 导出</a-button> | <a-button type="primary" preIcon="ant-design:export-outlined" @click="onExportXls"> 导出</a-button> | ||||
<j-upload-button type="primary" preIcon="ant-design:import-outlined" @click="onImportXls">导入</j-upload-button> | <j-upload-button type="primary" preIcon="ant-design:import-outlined" @click="onImportXls">导入</j-upload-button> | ||||
<a-dropdown v-if="checkedKeys.length > 0"> | |||||
<template #overlay> | |||||
<a-menu> | |||||
<a-menu-item key="1" @click="batchHandleDelete"> | |||||
<Icon icon="ant-design:delete-outlined"></Icon> | |||||
删除 | |||||
</a-menu-item> | |||||
</a-menu> | |||||
</template> | |||||
<a-button>批量操作 | |||||
<Icon icon="mdi:chevron-down"></Icon> | |||||
</a-button> | |||||
<a-dropdown v-if="selectedRowKeys.length > 0"> | |||||
<template #overlay> | |||||
<a-menu> | |||||
<a-menu-item key="1" @click="batchHandleDelete"> | |||||
<Icon icon="ant-design:delete-outlined"></Icon> | |||||
删除 | |||||
</a-menu-item> | |||||
</a-menu> | |||||
</template> | |||||
<a-button>批量操作 | |||||
<Icon icon="ant-design:down-outlined"></Icon> | |||||
</a-button> | |||||
</a-dropdown> | </a-dropdown> | ||||
</template> | </template> | ||||
<!--操作栏--> | |||||
<!--操作栏--> | |||||
<template #action="{ record }"> | <template #action="{ record }"> | ||||
<TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)"/> | |||||
</template> | |||||
<!--字段回显插槽--> | |||||
<template #htmlSlot="{text}"> | |||||
<div v-html="text"></div> | |||||
</template> | |||||
<template #fileSlot="{text}"> | |||||
<span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span> | |||||
<a-button v-else :ghost="true" type="primary" preIcon="ant-design:download-outlined" size="small" @click="downloadFile(text)">下载</a-button> | |||||
<TableAction :actions="getTableAction(record)"/> | |||||
</template> | </template> | ||||
</BasicTable> | </BasicTable> | ||||
<!-- 表单区域 --> | |||||
<EmployCategoryModal @register="registerModal" @success="handleSuccess"></EmployCategoryModal> | |||||
<!--字典弹窗--> | |||||
<EmployCategoryModal @register="registerModal" @success="handleSuccess"/> | |||||
</div> | </div> | ||||
</template> | </template> | ||||
<script lang="ts" name="employCategory-employCategory" setup> | <script lang="ts" name="employCategory-employCategory" setup> | ||||
import {ref, computed, unref} from 'vue'; | |||||
import {BasicTable, useTable, TableAction} from '/@/components/Table'; | |||||
import {useModal} from '/@/components/Modal'; | |||||
//ts语法 | |||||
import {ref, computed, unref, toRaw, nextTick} from 'vue'; | |||||
import {BasicTable, useTable, TableAction} from '/src/components/Table'; | |||||
import {useModal} from '/src/components/Modal'; | |||||
import { useListPage } from '/@/hooks/system/useListPage' | import { useListPage } from '/@/hooks/system/useListPage' | ||||
import EmployCategoryModal from './components/EmployCategoryModal.vue' | |||||
import {columns, searchFormSchema} from './employCategory.data'; | |||||
import {list, deleteOne, batchDelete, getImportUrl,getExportUrl} from './employCategory.api'; | |||||
const checkedKeys = ref<Array<string | number>>([]); | |||||
//注册model | |||||
import EmployCategoryModal from './components/EmployCategoryModal.vue'; | |||||
import {columns} from './EmployCategory.data'; | |||||
import {list, deleteEmployCategory, batchDeleteEmployCategory, getExportUrl,getImportUrl, getChildList,getChildListBatch} from './EmployCategory.api'; | |||||
const expandedRowKeys = ref([]); | |||||
//字典model | |||||
const [registerModal, {openModal}] = useModal(); | const [registerModal, {openModal}] = useModal(); | ||||
//注册table数据 | |||||
//注册table数据 | |||||
const { prefixCls,tableContext,onExportXls,onImportXls } = useListPage({ | const { prefixCls,tableContext,onExportXls,onImportXls } = useListPage({ | ||||
tableProps:{ | |||||
title: '分类表', | |||||
api: list, | |||||
columns, | |||||
canResize:false, | |||||
formConfig: { | |||||
labelWidth: 120, | |||||
schemas: searchFormSchema, | |||||
autoSubmitOnEnter:true, | |||||
showAdvancedButton:true, | |||||
fieldMapToTime: [ | |||||
], | |||||
}, | |||||
actionColumn: { | |||||
width: 120, | |||||
}, | |||||
}, | |||||
exportConfig: { | |||||
name:"分类表", | |||||
url: getExportUrl, | |||||
tableProps:{ | |||||
title: '分类表', | |||||
columns, | |||||
canResize:false, | |||||
actionColumn: { | |||||
width: 120, | |||||
}, | }, | ||||
importConfig: { | |||||
url: getImportUrl | |||||
}, | |||||
}) | |||||
}, | |||||
exportConfig: { | |||||
name:"分类表", | |||||
url: getExportUrl, | |||||
}, | |||||
importConfig: { | |||||
url: getImportUrl, | |||||
success: importSuccess | |||||
}, | |||||
}) | |||||
const [registerTable, {reload},{ rowSelection, selectedRowKeys }] = tableContext | |||||
const [registerTable, {reload, collapseAll, updateTableDataRecord, findTableDataRecord,getDataSource},{ rowSelection, selectedRowKeys }] = tableContext | |||||
/** | |||||
* 新增事件 | |||||
*/ | |||||
function handleAdd() { | |||||
openModal(true, { | |||||
isUpdate: false, | |||||
showFooter: true, | |||||
}); | |||||
/** | |||||
* 新增事件 | |||||
*/ | |||||
function handleCreate() { | |||||
openModal(true, { | |||||
isUpdate: false, | |||||
}); | |||||
} | } | ||||
/** | |||||
* 编辑事件 | |||||
*/ | |||||
function handleEdit(record: Recordable) { | |||||
openModal(true, { | |||||
record, | |||||
isUpdate: true, | |||||
showFooter: true, | |||||
}); | |||||
} | |||||
/** | |||||
* 详情 | |||||
/** | |||||
* 编辑事件 | |||||
*/ | |||||
async function handleEdit(record) { | |||||
openModal(true, { | |||||
record, | |||||
isUpdate: true, | |||||
}); | |||||
} | |||||
/** | |||||
* 详情 | |||||
*/ | |||||
async function handleDetail(record) { | |||||
openModal(true, { | |||||
record, | |||||
isUpdate: true, | |||||
hideFooter: true, | |||||
}); | |||||
} | |||||
/** | |||||
* 删除事件 | |||||
*/ | */ | ||||
function handleDetail(record: Recordable) { | |||||
openModal(true, { | |||||
record, | |||||
isUpdate: true, | |||||
showFooter: false, | |||||
}); | |||||
} | |||||
/** | |||||
* 删除事件 | |||||
*/ | |||||
async function handleDelete(record) { | async function handleDelete(record) { | ||||
await deleteOne({id: record.id}, reload); | |||||
} | |||||
/** | |||||
* 批量删除事件 | |||||
*/ | |||||
await deleteEmployCategory({id: record.id}, importSuccess); | |||||
} | |||||
/** | |||||
* 批量删除事件 | |||||
*/ | |||||
async function batchHandleDelete() { | async function batchHandleDelete() { | ||||
await batchDelete({ids: checkedKeys.value}, reload); | |||||
} | |||||
/** | |||||
* 成功回调 | |||||
*/ | |||||
function handleSuccess() { | |||||
reload(); | |||||
} | |||||
/** | |||||
* 操作栏 | |||||
*/ | |||||
function getTableAction(record){ | |||||
return [ | |||||
{ | |||||
label: '编辑', | |||||
onClick: handleEdit.bind(null, record), | |||||
} | |||||
] | |||||
} | |||||
/** | |||||
* 下拉操作栏 | |||||
*/ | |||||
function getDropDownAction(record){ | |||||
return [ | |||||
{ | |||||
label: '详情', | |||||
onClick: handleDetail.bind(null, record), | |||||
}, { | |||||
label: '删除', | |||||
popConfirm: { | |||||
title: '是否确认删除', | |||||
confirm: handleDelete.bind(null, record), | |||||
} | |||||
} | |||||
] | |||||
} | |||||
const ids = selectedRowKeys.value.filter(item => !item.includes('loading')) | |||||
await batchDeleteEmployCategory({ids: ids}, importSuccess); | |||||
} | |||||
/** | |||||
* 导入 | |||||
*/ | |||||
function importSuccess() { | |||||
reload() && (expandedRowKeys.value = []); | |||||
} | |||||
/** | |||||
* 添加下级 | |||||
*/ | |||||
function handleAddSub(record) { | |||||
openModal(true, { | |||||
record, | |||||
isUpdate: false, | |||||
}); | |||||
} | |||||
/** | |||||
* 成功回调 | |||||
*/ | |||||
async function handleSuccess({isUpdate, values, expandedArr}) { | |||||
if (isUpdate) { | |||||
//编辑回调 | |||||
updateTableDataRecord(values.id, values); | |||||
} else { | |||||
if(!values['pid']){ | |||||
//新增根节点 | |||||
reload(); | |||||
}else{ | |||||
//新增子集 | |||||
expandedRowKeys.value = []; | |||||
for (let key of unref(expandedArr)) { | |||||
await expandTreeNode(key) | |||||
} | |||||
} | |||||
} | |||||
} | |||||
/** | |||||
* 接口请求成功后回调 | |||||
*/ | |||||
function onFetchSuccess(result) { | |||||
getDataByResult(result.items)&&loadDataByExpandedRows(); | |||||
} | |||||
/** | |||||
* 根据已展开的行查询数据(用于保存后刷新时异步加载子级的数据) | |||||
*/ | |||||
async function loadDataByExpandedRows() { | |||||
if (unref(expandedRowKeys).length > 0) { | |||||
const res = await getChildListBatch({ parentIds: unref(expandedRowKeys).join(',')}); | |||||
if (res.success && res.result.records.length>0) { | |||||
//已展开的数据批量子节点 | |||||
let records = res.result.records | |||||
const listMap = new Map(); | |||||
for (let item of records) { | |||||
let pid = item['pid']; | |||||
if (unref(expandedRowKeys).includes(pid)) { | |||||
let mapList = listMap.get(pid); | |||||
if (mapList == null) { | |||||
mapList = []; | |||||
} | |||||
mapList.push(item); | |||||
listMap.set(pid, mapList); | |||||
} | |||||
} | |||||
let childrenMap = listMap; | |||||
let fn = (list) => { | |||||
if(list) { | |||||
list.forEach(data => { | |||||
if (unref(expandedRowKeys).includes(data.id)) { | |||||
data.children = getDataByResult(childrenMap.get(data.id)) | |||||
fn(data.children) | |||||
} | |||||
}) | |||||
} | |||||
}; | |||||
fn(getDataSource()) | |||||
} | |||||
} | |||||
} | |||||
/** | |||||
* 处理数据集 | |||||
*/ | |||||
function getDataByResult(result){ | |||||
if(result && result.length>0){ | |||||
return result.map(item=>{ | |||||
//判断是否标记了带有子节点 | |||||
if(item["hasChild"]=='1'){ | |||||
let loadChild = { id: item.id+'_loadChild', name: 'loading...', isLoading: true } | |||||
item.children = [loadChild] | |||||
} | |||||
return item | |||||
}) | |||||
} | |||||
} | |||||
/** | |||||
*树节点展开合并 | |||||
* */ | |||||
async function handleExpand(expanded, record) { | |||||
// 判断是否是展开状态,展开状态(expanded)并且存在子集(children)并且未加载过(isLoading)的就去查询子节点数据 | |||||
if (expanded) { | |||||
expandedRowKeys.value.push(record.id) | |||||
if (record.children.length > 0 && !!record.children[0].isLoading) { | |||||
let result = await getChildList({pid: record.id}); | |||||
result=result.records?result.records:result; | |||||
if (result && result.length > 0) { | |||||
record.children = getDataByResult(result); | |||||
} else { | |||||
record.children = null | |||||
record.hasChild = '0' | |||||
} | |||||
} | |||||
} else { | |||||
let keyIndex = expandedRowKeys.value.indexOf(record.id) | |||||
if (keyIndex >= 0) { | |||||
expandedRowKeys.value.splice(keyIndex, 1); | |||||
} | |||||
} | |||||
} | |||||
/** | |||||
*操作表格后处理树节点展开合并 | |||||
* */ | |||||
async function expandTreeNode(key) { | |||||
let record = findTableDataRecord(key) | |||||
expandedRowKeys.value.push(key); | |||||
let result = await getChildList({pid: key}); | |||||
if (result && result.length > 0) { | |||||
record.children = getDataByResult(result); | |||||
} else { | |||||
record.children = null | |||||
record.hasChild = '0' | |||||
} | |||||
updateTableDataRecord(key, record); | |||||
} | |||||
/** | |||||
* 操作栏 | |||||
*/ | |||||
function getTableAction(record) { | |||||
return [ | |||||
{ | |||||
label: '编辑', | |||||
onClick: handleEdit.bind(null, record), | |||||
}, | |||||
{ | |||||
label: '删除', | |||||
popConfirm: { | |||||
title: '确定删除吗?', | |||||
confirm: handleDelete.bind(null, record), | |||||
}, | |||||
}, | |||||
{ | |||||
label: '添加下级', | |||||
onClick: handleAddSub.bind(null, {pid: record.id}), | |||||
} | |||||
] | |||||
} | |||||
</script> | </script> | ||||
<style scoped> | <style scoped> | ||||
</style> | |||||
</style> |
@ -1,58 +1,87 @@ | |||||
<template> | <template> | ||||
<BasicModal v-bind="$attrs" @register="registerModal" :title="title" @ok="handleSubmit"> | |||||
<BasicForm @register="registerForm"/> | |||||
<BasicModal v-bind="$attrs" @register="registerModal" :title="getTitle" @ok="handleSubmit"> | |||||
<BasicForm @register="registerForm"/> | |||||
</BasicModal> | </BasicModal> | ||||
</template> | </template> | ||||
<script lang="ts" setup> | <script lang="ts" setup> | ||||
import {ref, computed, unref} from 'vue'; | |||||
import {BasicModal, useModalInner} from '/@/components/Modal'; | |||||
import {BasicForm, useForm} from '/@/components/Form/index'; | |||||
import {formSchema} from '../employCategory.data'; | |||||
import {saveOrUpdate} from '../employCategory.api'; | |||||
// Emits声明 | |||||
const emit = defineEmits(['register','success']); | |||||
const isUpdate = ref(true); | |||||
//表单配置 | |||||
const [registerForm, {setProps,resetFields, setFieldsValue, validate}] = useForm({ | |||||
labelWidth: 150, | |||||
schemas: formSchema, | |||||
showActionButtonGroup: false, | |||||
}); | |||||
//表单赋值 | |||||
const [registerModal, {setModalProps, closeModal}] = useModalInner(async (data) => { | |||||
//重置表单 | |||||
await resetFields(); | |||||
setModalProps({confirmLoading: false,showCancelBtn:!!data?.showFooter,showOkBtn:!!data?.showFooter}); | |||||
isUpdate.value = !!data?.isUpdate; | |||||
if (unref(isUpdate)) { | |||||
//表单赋值 | |||||
await setFieldsValue({ | |||||
...data.record, | |||||
}); | |||||
} | |||||
// 隐藏底部时禁用整个表单 | |||||
setProps({ disabled: !data?.showFooter }) | |||||
import {ref, computed, unref} from 'vue'; | |||||
import {BasicModal, useModalInner} from '/src/components/Modal'; | |||||
import {BasicForm, useForm} from '/src/components/Form'; | |||||
import {formSchema} from '../employCategory.data'; | |||||
import {loadTreeData, saveOrUpdateDict} from '../employCategory.api'; | |||||
// 获取emit | |||||
const emit = defineEmits(['register', 'success']); | |||||
const isUpdate = ref(true); | |||||
const expandedRowKeys = ref([]); | |||||
const treeData = ref([]); | |||||
//表单配置 | |||||
const [registerForm, {resetFields, setFieldsValue, validate, updateSchema}] = useForm({ | |||||
schemas: formSchema, | |||||
showActionButtonGroup: false, | |||||
labelCol: { | |||||
xs: { span: 24 }, | |||||
sm: { span: 4 }, | |||||
}, | |||||
wrapperCol: { | |||||
xs: { span: 24 }, | |||||
sm: { span: 18 }, | |||||
}, | |||||
}); | |||||
//表单赋值 | |||||
const [registerModal, {setModalProps, closeModal}] = useModalInner(async (data) => { | |||||
//重置表单 | |||||
await resetFields(); | |||||
expandedRowKeys.value = []; | |||||
setModalProps({confirmLoading: false, minHeight: 80}); | |||||
isUpdate.value = !!data?.isUpdate; | |||||
if (data?.record) { | |||||
//表单赋值 | |||||
await setFieldsValue({ | |||||
...data.record, | |||||
}); | |||||
} | |||||
//父级节点树信息 | |||||
treeData.value = await loadTreeData({'async': false,'pcode':''}); | |||||
updateSchema({ | |||||
field: 'pid', | |||||
componentProps: {treeData}, | |||||
}); | }); | ||||
//设置标题 | |||||
const title = computed(() => (!unref(isUpdate) ? '新增' : '编辑')); | |||||
//表单提交事件 | |||||
async function handleSubmit(v) { | |||||
try { | |||||
let values = await validate(); | |||||
setModalProps({confirmLoading: true}); | |||||
//提交表单 | |||||
await saveOrUpdate(values, isUpdate.value); | |||||
//关闭弹窗 | |||||
closeModal(); | |||||
//刷新列表 | |||||
emit('success'); | |||||
} finally { | |||||
setModalProps({confirmLoading: false}); | |||||
}); | |||||
//设置标题 | |||||
const getTitle = computed(() => (!unref(isUpdate) ? '新增字典' : '编辑字典')); | |||||
/** | |||||
* 根据pid获取展开的节点 | |||||
* @param pid | |||||
* @param arr | |||||
*/ | |||||
function getExpandKeysByPid(pid,arr){ | |||||
if(pid && arr && arr.length>0){ | |||||
for(let i=0;i<arr.length;i++){ | |||||
if(arr[i].key==pid && unref(expandedRowKeys).indexOf(pid)<0){ | |||||
expandedRowKeys.value.push(arr[i].key); | |||||
getExpandKeysByPid(arr[i]['parentId'],unref(treeData)) | |||||
}else{ | |||||
getExpandKeysByPid(pid,arr[i].children) | |||||
} | } | ||||
} | |||||
} | |||||
} | |||||
//表单提交事件 | |||||
async function handleSubmit() { | |||||
try { | |||||
let values = await validate(); | |||||
setModalProps({confirmLoading: true}); | |||||
//提交表单 | |||||
await saveOrUpdateDict(values, isUpdate.value); | |||||
//关闭弹窗 | |||||
closeModal(); | |||||
//展开的节点信息 | |||||
await getExpandKeysByPid(values['pid'],unref(treeData)) | |||||
//刷新列表(isUpdate:是否编辑;values:表单信息;expandedArr:展开的节点信息) | |||||
emit('success', {isUpdate: unref(isUpdate), values:{...values},expandedArr: unref(expandedRowKeys).reverse()}); | |||||
} finally { | |||||
setModalProps({confirmLoading: false}); | |||||
} | } | ||||
} | |||||
</script> | </script> | ||||
<style lang="less" scoped> | |||||
</style> |