Files
DjangoAdmin_Django_NaiveVue/public/templates/ui2/detail.vue.tpl
T
2026-09-12 12:07:15 +08:00

216 lines
5.2 KiB
Smarty

<template>
<basicModal
@register="modalRegister"
ref="modalRef"
class="basicModal basicFormModal"
>
<template #default>
<n-descriptions
class="margin-top"
:column="2"
bordered
:label-style="{ width: '120px' }"
label-placement="left"
>
{% for field in form_fields %}
<n-descriptions-item label="{{ field.label }}:">
{% if field.is_image %}
<n-image
v-if="formData.{{ field.camel_name }}"
:src="formData.{{ field.camel_name }}"
width="80"
height="80"
fit="cover"
:preview-disabled="false"
/>
<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' %}
<n-tag :type="formData.{{ field.camel_name }} ? 'success' : 'error'" :bordered="false">
{{ '{{ formData.' + field.camel_name + ' ? "是" : "否" }}' }}
</n-tag>
{% elif field.type == 'IntegerField' and field.choices %}
<span>{{ '{{ getChoiceLabel(\'' + field.camel_name + '\', formData.' + field.camel_name + ') }}' }}</span>
{% else %}
<span>{{ '{{ formData.' + field.camel_name + ' || \'-\' }}' }}</span>
{% endif %}
</n-descriptions-item>
{% endfor %}
</n-descriptions>
</template>
<template #action>
<n-button @click="dialogClose">关闭</n-button>
</template>
</basicModal>
</template>
<script lang="ts" setup>
import { get{{ model_class_name }}Detail } from '@/api/{{ api_path }}';
import { onMounted, reactive, watch } from 'vue';
import { useModal } from '@/components/Modal';
{% if has_image_field %}
import { NImage } from 'naive-ui';
{% 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 [modalRegister, { openModal, closeModal }] = useModal({
title: '{{ module_comment }}详情',
width: 750,
});
/**
* 获取选项字段的标签
*/
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 = () => {
closeModal();
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();
openModal();
} else if (newVal && !props.{{ primary_key }}) {
resetFormData();
openModal();
}
},
);
/**
* 钩子函数
*/
onMounted(() => {
if (props.visible) {
if (props.{{ primary_key }}) {
setFormData();
} else {
resetFormData();
}
openModal();
}
});
/**
* 暴露openModal方法
*/
defineExpose({
openModal,
});
</script>
<style lang="less" scoped>
.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%;
}
}
:deep(.n-descriptions-table) {
.n-descriptions-table-body {
.n-descriptions-table-row {
.n-descriptions-table-body-cell {
word-break: break-all;
}
}
}
}
</style>