Files
DjangoAdmin_Django_EleVue/public/templates/ui2/detail.vue.tpl
T
2026-09-11 11:39:07 +08:00

184 lines
4.7 KiB
Smarty

<template>
<el-dialog
v-model="props.visible"
title="{{ module_comment }}详情"
width="750"
:close-on-click-modal="false"
:before-close="dialogClose"
>
<el-descriptions column="2" border>
{% for field in form_fields %}
<el-descriptions-item label="{{ field.label }}:" label-class-name="des-width">
{% if field.is_image %}
<el-image
v-if="formData.{{ field.camel_name }}"
:src="formData.{{ field.camel_name }}"
:preview-src-list="[formData.{{ field.camel_name }}]"
fit="cover"
style="width: 80px; height: 80px"
/>
<span v-else>-</span>
{% elif field.is_rich_text %}
<div class="rich-text-content" v-html="formData.{{ field.camel_name }}"></div>
{% elif field.type == 'BooleanField' %}
<el-tag :type="formData.{{ field.camel_name }} ? 'success' : 'danger'">
{{ '{{ formData.' + field.camel_name + ' ? "是" : "否" }}' }}
</el-tag>
{% elif field.type == 'IntegerField' and field.choices %}
<span>{{ '{{ getChoiceLabel(\'' + field.camel_name + '\', formData.' + field.camel_name + ') }}' }}</span>
{% else %}
{{ '{{ formData.' + field.camel_name + ' || \'-\' }}' }}
{% endif %}
</el-descriptions-item>
{% endfor %}
</el-descriptions>
<template #footer>
<span class="dialog-footer">
<el-button @click="dialogClose">关闭</el-button>
</span>
</template>
</el-dialog>
</template>
<script lang="ts" setup>
import { get{{ model_class_name }}Detail } from '@/api/{{ api_path }}';
import { onMounted, reactive, watch } from 'vue';
{% if has_image_field %}
import { ElImage } from 'element-plus';
{% endif %}
const emit = defineEmits(['update:visible']);
/**
* 定义表单参数
*/
const formData = reactive({
id: '',
{% for field in form_fields %}
{{ field.camel_name }}: {% if field.type == 'CharField' %}''{% elif field.type == 'IntegerField' %}0{% elif field.type == 'BooleanField' %}false{% elif field.type == 'FloatField' %}0{% else %}''{% endif %},
{% endfor %}
});
/**
* 定义接收的参数
*/
const props = defineProps({
visible: {
type: Boolean,
required: true,
default: false,
},
{{ primary_key }}: {
type: Number,
required: true,
default: 0,
},
});
/**
* 获取选项字段的标签
*/
const getChoiceLabel = (fieldName: string, value: any) => {
const choiceMap: Record<string, Record<string, string>> = {
{% for field in form_fields %}
{% if field.type == 'IntegerField' and field.choices %}
{{ field.camel_name }}: {
{% for choice in field.choices %}
{{ choice.value }}: '{{ choice.label }}',
{% endfor %}
},
{% endif %}
{% endfor %}
};
return choiceMap[fieldName]?.[value] || value || '-';
};
/**
* 关闭窗体
*/
const dialogClose = () => {
emit('update:visible', false);
};
/**
* 设置表单数据
*/
const setFormData = async () => {
try {
const data = await get{{ model_class_name }}Detail(props.{{ primary_key }});
for (const key in formData) {
if (data[key] != null && data[key] != undefined) {
//@ts-ignore
formData[key] = data[key];
}
}
} catch (error) {
console.error('获取{{ module_comment }}详情失败', error);
}
};
/**
* 重置表单数据
*/
const resetFormData = () => {
for (const key in formData) {
//@ts-ignore
if (typeof formData[key] === 'string') {
//@ts-ignore
formData[key] = '';
} else if (typeof formData[key] === 'number') {
//@ts-ignore
formData[key] = 0;
} else if (typeof formData[key] === 'boolean') {
//@ts-ignore
formData[key] = false;
}
}
};
/**
* 监听visible变化,重新加载数据
*/
watch(
() => props.visible,
(newVal) => {
if (newVal && props.{{ primary_key }}) {
setFormData();
} else if (newVal && !props.{{ primary_key }}) {
resetFormData();
}
},
{ immediate: true },
);
/**
* 钩子函数
*/
onMounted(() => {
if (props.visible && props.{{ primary_key }}) {
setFormData();
}
});
</script>
<style lang="scss" scoped>
:deep(.des-width) {
width: 120px;
}
:deep(.el-descriptions__body .el-descriptions__table .el-descriptions__cell) {
word-break: break-all;
}
.rich-text-content {
max-height: 300px;
overflow-y: auto;
border: 1px solid #e4e7ed;
padding: 10px;
border-radius: 4px;
background-color: #f8f9fa;
:deep(img) {
max-width: 100%;
}
}
</style>