505 lines
15 KiB
Vue
505 lines
15 KiB
Vue
<template>
|
||
<div class="property-form">
|
||
<div class="form-header">
|
||
<h3>{{ title }}</h3>
|
||
<el-button
|
||
v-if="showAddButton"
|
||
type="primary"
|
||
size="small"
|
||
icon="el-icon-plus"
|
||
@click="handleAddProperty"
|
||
>
|
||
添加属性
|
||
</el-button>
|
||
</div>
|
||
|
||
<!-- 属性列表 -->
|
||
<el-table
|
||
v-if="propertyList.length > 0"
|
||
:data="propertyList"
|
||
style="width: 100%; margin-bottom: 20px;"
|
||
border
|
||
stripe
|
||
size="small"
|
||
>
|
||
<el-table-column
|
||
prop="name"
|
||
label="属性名称"
|
||
min-width="150"
|
||
>
|
||
<template #default="{ row }">
|
||
<el-input
|
||
v-if="row.editing"
|
||
v-model="row.name"
|
||
size="small"
|
||
/>
|
||
<span v-else>{{ row.name }}</span>
|
||
</template>
|
||
</el-table-column>
|
||
|
||
<el-table-column
|
||
prop="type"
|
||
label="属性类型"
|
||
min-width="120"
|
||
>
|
||
<template #default="{ row }">
|
||
<el-select
|
||
v-if="row.editing"
|
||
v-model="row.type"
|
||
size="small"
|
||
style="width: 100%;"
|
||
>
|
||
<el-option label="文本" value="text" />
|
||
<el-option label="数字" value="number" />
|
||
<el-option label="日期" value="date" />
|
||
<el-option label="选项" value="select" />
|
||
<el-option label="布尔" value="boolean" />
|
||
</el-select>
|
||
<span v-else>{{ getTypeLabel(row.type) }}</span>
|
||
</template>
|
||
</el-table-column>
|
||
|
||
<el-table-column
|
||
prop="description"
|
||
label="描述"
|
||
min-width="200"
|
||
>
|
||
<template #default="{ row }">
|
||
<el-input
|
||
v-if="row.editing"
|
||
v-model="row.description"
|
||
type="textarea"
|
||
:rows="1"
|
||
size="small"
|
||
/>
|
||
<span v-else>{{ row.description || '--' }}</span>
|
||
</template>
|
||
</el-table-column>
|
||
|
||
<el-table-column
|
||
prop="required"
|
||
label="必填"
|
||
min-width="80"
|
||
align="center"
|
||
>
|
||
<template #default="{ row }">
|
||
<el-checkbox
|
||
v-if="row.editing"
|
||
v-model="row.required"
|
||
:disabled="row.system"
|
||
/>
|
||
<span v-else>
|
||
<el-tag
|
||
v-if="row.required"
|
||
type="danger"
|
||
size="small"
|
||
>是</el-tag>
|
||
<el-tag
|
||
v-else
|
||
type="info"
|
||
size="small"
|
||
>否</el-tag>
|
||
</span>
|
||
</template>
|
||
</el-table-column>
|
||
|
||
<el-table-column
|
||
prop="system"
|
||
label="系统属性"
|
||
min-width="100"
|
||
align="center"
|
||
>
|
||
<template #default="{ row }">
|
||
<el-tag
|
||
v-if="row.system"
|
||
type="warning"
|
||
size="small"
|
||
>是</el-tag>
|
||
<span v-else>--</span>
|
||
</template>
|
||
</el-table-column>
|
||
|
||
<el-table-column
|
||
label="操作"
|
||
width="180"
|
||
align="center"
|
||
fixed="right"
|
||
>
|
||
<template #default="{ row }">
|
||
<template v-if="!row.system">
|
||
<el-button
|
||
v-if="row.editing"
|
||
type="success"
|
||
size="small"
|
||
@click="saveProperty(row)"
|
||
>保存</el-button>
|
||
<el-button
|
||
v-else
|
||
type="primary"
|
||
size="small"
|
||
@click="editProperty(row)"
|
||
>编辑</el-button>
|
||
|
||
<el-button
|
||
v-if="!row.editing"
|
||
type="danger"
|
||
size="small"
|
||
@click="deleteProperty(row)"
|
||
>删除</el-button>
|
||
<el-button
|
||
v-else
|
||
type="warning"
|
||
size="small"
|
||
@click="cancelEdit(row)"
|
||
>取消</el-button>
|
||
</template>
|
||
<span v-else>--</span>
|
||
</template>
|
||
</el-table-column>
|
||
</el-table>
|
||
|
||
<!-- 空状态 -->
|
||
<el-empty
|
||
v-else
|
||
:description="emptyText"
|
||
:image-size="100"
|
||
/>
|
||
|
||
<!-- 添加属性对话框 -->
|
||
<el-dialog
|
||
title="添加属性"
|
||
v-model:visible="addDialogVisible"
|
||
width="500px"
|
||
@close="resetAddForm"
|
||
>
|
||
<el-form
|
||
ref="addFormRef"
|
||
:model="addForm"
|
||
:rules="addFormRules"
|
||
label-width="100px"
|
||
>
|
||
<el-form-item label="属性名称" prop="name">
|
||
<el-input
|
||
v-model="addForm.name"
|
||
placeholder="请输入属性名称"
|
||
/>
|
||
</el-form-item>
|
||
|
||
<el-form-item label="属性类型" prop="type">
|
||
<el-select
|
||
v-model="addForm.type"
|
||
placeholder="请选择属性类型"
|
||
style="width: 100%;"
|
||
>
|
||
<el-option label="文本" value="text" />
|
||
<el-option label="数字" value="number" />
|
||
<el-option label="日期" value="date" />
|
||
<el-option label="选项" value="select" />
|
||
<el-option label="布尔" value="boolean" />
|
||
</el-select>
|
||
</el-form-item>
|
||
|
||
<el-form-item label="描述" prop="description">
|
||
<el-input
|
||
v-model="addForm.description"
|
||
type="textarea"
|
||
:rows="3"
|
||
placeholder="请输入属性描述"
|
||
/>
|
||
</el-form-item>
|
||
|
||
<el-form-item label="是否必填" prop="required">
|
||
<el-switch
|
||
v-model="addForm.required"
|
||
active-text="是"
|
||
inactive-text="否"
|
||
/>
|
||
</el-form-item>
|
||
|
||
<!-- 如果是选项类型,显示选项设置 -->
|
||
<el-form-item
|
||
v-if="addForm.type === 'select'"
|
||
label="选项值"
|
||
prop="options"
|
||
>
|
||
<div
|
||
v-for="(option, index) in addForm.options"
|
||
:key="index"
|
||
style="display: flex; align-items: center; margin-bottom: 10px;"
|
||
>
|
||
<el-input
|
||
v-model="option.label"
|
||
placeholder="选项标签"
|
||
style="margin-right: 10px; flex: 1;"
|
||
/>
|
||
<el-input
|
||
v-model="option.value"
|
||
placeholder="选项值"
|
||
style="flex: 1;"
|
||
/>
|
||
<el-button
|
||
v-if="addForm.options.length > 1"
|
||
type="link"
|
||
icon="el-icon-delete"
|
||
@click="removeOption(Number(index))"
|
||
/>
|
||
</div>
|
||
<el-button
|
||
type="link"
|
||
icon="el-icon-plus"
|
||
@click="addOption"
|
||
>
|
||
添加选项
|
||
</el-button>
|
||
</el-form-item>
|
||
</el-form>
|
||
|
||
<template #footer>
|
||
<el-button @click="addDialogVisible = false">取 消</el-button>
|
||
<el-button type="primary" @click="submitAddForm">确 定</el-button>
|
||
</template>
|
||
</el-dialog>
|
||
</div>
|
||
</template>
|
||
|
||
<script setup lang="ts">
|
||
import { ref, reactive, computed, watch } from 'vue'
|
||
import { ElMessage, ElMessageBox } from 'element-plus'
|
||
|
||
const props = defineProps({
|
||
title: { type: String, default: '属性管理' },
|
||
spaceId: { type: [String, Number], default: null },
|
||
showAddButton: { type: Boolean, default: true }
|
||
})
|
||
|
||
// 数据
|
||
const propertyList = ref<any[]>([
|
||
{
|
||
id: 1,
|
||
name: '名称',
|
||
type: 'text',
|
||
description: '空间名称',
|
||
required: true,
|
||
system: true,
|
||
editing: false
|
||
},
|
||
{
|
||
id: 2,
|
||
name: '编号',
|
||
type: 'text',
|
||
description: '空间编号',
|
||
required: true,
|
||
system: true,
|
||
editing: false
|
||
}
|
||
])
|
||
|
||
const addDialogVisible = ref(false)
|
||
const addForm = reactive<any>({
|
||
name: '',
|
||
type: 'text',
|
||
description: '',
|
||
required: false,
|
||
options: [{ label: '', value: '' }]
|
||
})
|
||
|
||
const addFormRules = reactive({
|
||
name: [
|
||
{ required: true, message: '请输入属性名称', trigger: 'blur' },
|
||
{ min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
|
||
],
|
||
type: [
|
||
{ required: true, message: '请选择属性类型', trigger: 'change' }
|
||
]
|
||
})
|
||
|
||
const addFormRef = ref(null)
|
||
|
||
const emptyText = computed(() => '暂无属性数据,点击"添加属性"按钮创建')
|
||
|
||
// 监听 spaceId
|
||
watch(() => props.spaceId, (newVal) => {
|
||
if (newVal) {
|
||
loadProperties()
|
||
}
|
||
}, { immediate: true })
|
||
|
||
// 方法
|
||
async function loadProperties() {
|
||
try {
|
||
if (props.spaceId) {
|
||
propertyList.value.push({
|
||
id: Date.now(),
|
||
name: '自定义属性',
|
||
type: 'text',
|
||
description: '用户自定义属性',
|
||
required: false,
|
||
system: false,
|
||
editing: false
|
||
})
|
||
}
|
||
} catch (error) {
|
||
ElMessage.error('加载属性数据失败')
|
||
console.error('加载属性数据失败:', error)
|
||
}
|
||
}
|
||
|
||
function handleAddProperty() {
|
||
addDialogVisible.value = true
|
||
}
|
||
|
||
function submitAddForm() {
|
||
if (!addFormRef.value) return
|
||
;(addFormRef.value as any).validate().then(async () => {
|
||
try {
|
||
const newProperty = {
|
||
id: Date.now(),
|
||
name: addForm.name,
|
||
type: addForm.type,
|
||
description: addForm.description,
|
||
required: addForm.required,
|
||
system: false,
|
||
editing: false,
|
||
options: addForm.type === 'select' ? [...addForm.options] : null
|
||
}
|
||
|
||
propertyList.value.push(newProperty)
|
||
ElMessage.success('添加属性成功')
|
||
addDialogVisible.value = false
|
||
} catch (error) {
|
||
ElMessage.error('添加属性失败')
|
||
console.error('添加属性失败:', error)
|
||
}
|
||
}).catch(() => {})
|
||
}
|
||
|
||
function editProperty(property: any) {
|
||
property.editing = true
|
||
property.originalData = { ...property }
|
||
}
|
||
|
||
async function saveProperty(property: any) {
|
||
try {
|
||
property.editing = false
|
||
delete property.originalData
|
||
ElMessage.success('保存成功')
|
||
} catch (error) {
|
||
ElMessage.error('保存失败')
|
||
console.error('保存失败:', error)
|
||
}
|
||
}
|
||
|
||
function cancelEdit(property: any) {
|
||
if (property.originalData) {
|
||
Object.assign(property, property.originalData)
|
||
delete property.originalData
|
||
}
|
||
property.editing = false
|
||
}
|
||
|
||
function deleteProperty(property: any) {
|
||
ElMessageBox.confirm('确定删除该属性吗?', '提示', {
|
||
confirmButtonText: '确定',
|
||
cancelButtonText: '取消',
|
||
type: 'warning'
|
||
}).then(async () => {
|
||
try {
|
||
const index = propertyList.value.findIndex(item => item.id === property.id)
|
||
if (index > -1) {
|
||
propertyList.value.splice(index, 1)
|
||
}
|
||
ElMessage.success('删除成功')
|
||
} catch (error) {
|
||
ElMessage.error('删除失败')
|
||
console.error('删除失败:', error)
|
||
}
|
||
}).catch(() => {})
|
||
}
|
||
|
||
function getTypeLabel(type: string) {
|
||
const typeMap: any = {
|
||
text: '文本',
|
||
number: '数字',
|
||
date: '日期',
|
||
select: '选项',
|
||
boolean: '布尔'
|
||
}
|
||
return typeMap[type] || type
|
||
}
|
||
|
||
function addOption() {
|
||
addForm.options.push({ label: '', value: '' })
|
||
}
|
||
|
||
function removeOption(index: number) {
|
||
addForm.options.splice(index, 1)
|
||
}
|
||
|
||
function resetAddForm() {
|
||
if (addFormRef.value) {
|
||
;(addFormRef.value as any).resetFields()
|
||
}
|
||
addForm.options = [{ label: '', value: '' }]
|
||
}
|
||
|
||
function getProperties() {
|
||
return propertyList.value.map(item => ({
|
||
id: item.id,
|
||
name: item.name,
|
||
type: item.type,
|
||
description: item.description,
|
||
required: item.required,
|
||
system: item.system,
|
||
options: item.options
|
||
}))
|
||
}
|
||
|
||
defineExpose({ getProperties })
|
||
</script>
|
||
|
||
<style scoped lang="scss">
|
||
.property-form {
|
||
height: 100%;
|
||
display: flex;
|
||
flex-direction: column;
|
||
|
||
.form-header {
|
||
display: flex;
|
||
justify-content: space-between;
|
||
align-items: center;
|
||
margin-bottom: 20px;
|
||
padding-bottom: 10px;
|
||
border-bottom: 1px solid #ebeef5;
|
||
|
||
h3 {
|
||
margin: 0;
|
||
font-size: 16px;
|
||
font-weight: 600;
|
||
color: #303133;
|
||
}
|
||
}
|
||
|
||
.el-table {
|
||
flex: 1;
|
||
overflow: auto;
|
||
|
||
::v-deep .el-table__body-wrapper {
|
||
overflow: auto;
|
||
}
|
||
}
|
||
|
||
.el-empty {
|
||
flex: 1;
|
||
display: flex;
|
||
flex-direction: column;
|
||
justify-content: center;
|
||
}
|
||
}
|
||
|
||
// 对话框样式调整
|
||
::v-deep .el-dialog__body {
|
||
padding: 20px;
|
||
max-height: 60vh;
|
||
overflow-y: auto;
|
||
}
|
||
</style> |