<!-- 修改父级对话框 -->
|
<template>
|
<ywDialog
|
v-model="modelValue"
|
title="修改父级"
|
headerIcon="ele-Menu"
|
width="400"
|
@closed="closeParentDialog"
|
@submit="submitParentForm"
|
@close="closeParentDialog"
|
>
|
<el-form :model="parentForm" ref="parentFormRef" label-width="80px" label-position="top">
|
<el-form-item label="父级" prop="ParentID">
|
<el-tree-select
|
filterable
|
class="w100"
|
v-model="parentForm.ParentID"
|
:props="{
|
id: 'ID',
|
label: 'Name',
|
children: 'Children',
|
}"
|
:data="parentSelectOptions"
|
node-key="ID"
|
:clearable="true"
|
:accordion="false"
|
:expandNode="false"
|
:check-strictly="true"
|
placeholder="请选择父级"
|
>
|
</el-tree-select>
|
</el-form-item>
|
</el-form>
|
</ywDialog>
|
</template>
|
|
<script setup lang="ts">
|
import type { Ref } from 'vue';
|
import { toRefs, ref, computed, watch } from 'vue';
|
import ywDialog from '/@/components/dialog/yw-dialog.vue';
|
import { travelTree } from '/@/utils/util';
|
import type { FormInstance } from 'element-plus';
|
import { ElMessage } from 'element-plus';
|
import { deepClone } from '/@/utils/other';
|
import { maxInArray } from '/@/utils/istation/common';
|
import request from '/@/utils/request';
|
import { requestProps } from '/@/projectCom/common';
|
|
const props = defineProps({
|
/** @description 当前行数据 */
|
rowData: Object,
|
/** @description 可选父级(树结构数据) */
|
data: Array,
|
modelValue: {
|
type: Boolean,
|
},
|
api: Function,
|
...requestProps,
|
});
|
|
const emits = defineEmits<{
|
(event: 'update:modelValue', value): void;
|
/** @description 提交成功回调 */
|
(event: 'submitSuccess', res): void;
|
/** @description 提交失败回调 */
|
(event: 'submitError', res): void;
|
}>();
|
|
const { modelValue, api, data, req, rowData } = toRefs(props);
|
const isShow = computed({
|
get: () => {
|
return modelValue.value;
|
},
|
set: (value) => {
|
emits('update:modelValue', value);
|
},
|
});
|
const parentForm = ref(null);
|
const parentFormRef = ref<FormInstance>(null);
|
const parentSelectOptions = computed(() => {
|
const cloneTreeData = deepClone(data.value);
|
travelTree(cloneTreeData, (value, index, array, parent) => {
|
if ((value as any).ID === parentForm.value.ID) {
|
if (parent === array) {
|
parent?.splice(index, 1);
|
} else {
|
parent?.Children?.splice(index, 1);
|
}
|
}
|
});
|
return cloneTreeData;
|
});
|
|
const closeParentDialog = () => {
|
isShow.value = false;
|
parentFormRef.value.clearValidate();
|
};
|
|
const setSortCode = (formValue: Ref<any>, parentID: string) => {
|
if (!parentID || parentID === '0') {
|
formValue.value.SortCode = 1;
|
} else {
|
let parent = null;
|
travelTree(parentSelectOptions.value, (value) => {
|
if ((value as any).ID === parentID) {
|
parent = value;
|
return true;
|
}
|
});
|
let parentChildren = [];
|
if (parent) {
|
parentChildren = parent.Children;
|
// 没有 parent,为根
|
} else {
|
parentChildren = parentSelectOptions.value;
|
}
|
let sort = 1;
|
if (parentChildren && parentChildren?.length > 0) {
|
let sorts = parentChildren.map((x) => {
|
return x.SortCode;
|
});
|
let maxSort = maxInArray(sorts);
|
sort = maxSort + 1;
|
}
|
formValue.value.SortCode = sort;
|
}
|
};
|
|
const submitParentForm = async () => {
|
const valid = await parentFormRef.value.validate().catch(() => {});
|
if (!valid) return;
|
const sendParentID = parentForm.value.ParentID ?? '0';
|
|
setSortCode(parentForm, sendParentID);
|
const res = await api.value({ ...parentForm.value, ParentID: sendParentID }, req.value);
|
if (res?.Code === 0) {
|
if (res.Data) {
|
emits('submitSuccess', res);
|
isShow.value = false;
|
ElMessage.success('修改成功');
|
} else {
|
emits('submitError', res);
|
ElMessage.error('添加失败');
|
}
|
} else {
|
emits('submitError', res);
|
|
ElMessage.error('修改失败' + (res?.Message ? `,${JSON.stringify(res.Message)}` : ''));
|
}
|
};
|
|
watch(
|
() => modelValue.value,
|
(val) => {
|
if (val) {
|
const { SortCode, ID, ParentID } = rowData.value;
|
parentForm.value = { SortCode, ID, ParentID: ParentID === '0' ? '' : ParentID };
|
}
|
}
|
);
|
</script>
|
<style scoped lang="scss"></style>
|