fix: 修复摄像头编辑/操作列数据引用问题
CameraFormDialog: 添加isPopulating标记防止watch清空已填充字段; 兼容row.server.id和row.server_id两种数据结构; 使用append-to-body避免z-index遮挡 OperationCellRenderer: 使用watch捕获row最新值解决slot scope引用失效; 简化buildFormProps参数传递 newcamara/index: 操作列改为直接渲染编辑/删除/预览按钮; 添加getRowById通过ID从listData查找完整行数据 camera/index: 添加isEditingMode标记防止编辑时watch清空字段; 使用formData.id判断创建/编辑模式; dialog添加z-index
This commit is contained in:
parent
3508316fca
commit
292e69a6d1
@ -65,9 +65,10 @@
|
||||
<!-- 新增/编辑弹窗 -->
|
||||
<el-dialog
|
||||
v-model="dialogVisible"
|
||||
:title="dialogMode === 'create' ? '新增摄像头' : '编辑摄像头'"
|
||||
:title="formData.id ? '编辑摄像头' : '新增摄像头'"
|
||||
width="560px"
|
||||
align-center
|
||||
z-index="2500"
|
||||
@close="resetForm"
|
||||
>
|
||||
<el-form ref="formRef" :model="formData" :rules="rules" label-width="120px">
|
||||
@ -269,6 +270,7 @@ const availableFields = [
|
||||
const presetTree = ref<any[]>([])
|
||||
const loading = ref(false)
|
||||
const saving = ref(false)
|
||||
const isEditingMode = ref(false)
|
||||
const tableData = ref<CameraRecord[]>([])
|
||||
const serverOptions = ref<ServerOption[]>([])
|
||||
/** 服务器完整信息映射(用于连通通道拉取) */
|
||||
@ -540,16 +542,19 @@ function handleChannelSelect(value: number) {
|
||||
|
||||
watch(() => formData.server_id, (newId, oldId) => {
|
||||
if (newId && newId !== oldId) {
|
||||
formData.channel_id = undefined
|
||||
formData.channel_name = ''
|
||||
formData.ip = ''
|
||||
formData.port = undefined
|
||||
formData.model = ''
|
||||
formData.serial_number = ''
|
||||
formData.stream_type = ''
|
||||
channelOptions.value = []
|
||||
selectedServerNeedsPassword.value = false
|
||||
serverPassword.value = ''
|
||||
// 编辑模式下不自动清空字段,由 openEdit 手动填充
|
||||
if (!isEditingMode.value) {
|
||||
formData.channel_id = undefined
|
||||
formData.channel_name = ''
|
||||
formData.ip = ''
|
||||
formData.port = undefined
|
||||
formData.model = ''
|
||||
formData.serial_number = ''
|
||||
formData.stream_type = ''
|
||||
channelOptions.value = []
|
||||
selectedServerNeedsPassword.value = false
|
||||
serverPassword.value = ''
|
||||
}
|
||||
fetchChannelList()
|
||||
}
|
||||
})
|
||||
@ -608,6 +613,8 @@ function openEdit(row: CameraRecord) {
|
||||
if (!row.id) return
|
||||
dialogMode.value = 'edit'
|
||||
dialogVisible.value = true
|
||||
// 标记编辑模式,防止 watch 清空已填充的字段
|
||||
isEditingMode.value = true
|
||||
formData.id = row.id
|
||||
formData.server_id = row.server_id
|
||||
formData.channel_id = row.channel_id
|
||||
@ -623,9 +630,8 @@ function openEdit(row: CameraRecord) {
|
||||
serverPassword.value = ''
|
||||
selectedServerNeedsPassword.value = false
|
||||
formRef.value?.clearValidate()
|
||||
if (row.server_id) {
|
||||
fetchChannelList()
|
||||
}
|
||||
// 延迟解除编辑标记,确保 watch 回调(pre-flush)执行完毕
|
||||
setTimeout(() => { isEditingMode.value = false }, 0)
|
||||
}
|
||||
|
||||
async function submitForm() {
|
||||
@ -635,7 +641,7 @@ async function submitForm() {
|
||||
|
||||
saving.value = true
|
||||
try {
|
||||
if (dialogMode.value === 'create') {
|
||||
if (!formData.id) {
|
||||
const res: any = await create({
|
||||
server_id: formData.server_id!,
|
||||
channel_id: formData.channel_id!,
|
||||
|
||||
@ -56,7 +56,7 @@
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref, reactive, type Component } from 'vue'
|
||||
import { ref, reactive, watch, type Component } from 'vue'
|
||||
import { ElMessageBox } from 'element-plus'
|
||||
import { COMPONENT_REGISTRY } from '../config/template-config'
|
||||
import type { OperationButtonConfig } from '../config/template-config'
|
||||
@ -70,6 +70,14 @@ const props = defineProps<{
|
||||
row: Record<string, any>
|
||||
}>()
|
||||
|
||||
// 用 watch 捕获行数据的最新值,解决 slot scope 中 row 引用失效的问题
|
||||
const currentRow = ref<Record<string, any>>({})
|
||||
watch(() => props.row, (val) => {
|
||||
if (val && Object.keys(val).length > 0) {
|
||||
currentRow.value = val
|
||||
}
|
||||
}, { immediate: true, deep: true })
|
||||
|
||||
const emit = defineEmits<{
|
||||
/** action 按钮点击事件(key: 按钮标识, row: 行数据) */
|
||||
(e: 'operation-action', key: string, row: Record<string, any>): void
|
||||
@ -104,7 +112,7 @@ async function handleActionClick(btn: OperationButtonConfig) {
|
||||
|
||||
actionLoadingMap[btn.key] = true
|
||||
try {
|
||||
emit('operation-action', btn.key, props.row)
|
||||
emit('operation-action', btn.key, currentRow.value)
|
||||
} finally {
|
||||
actionLoadingMap[btn.key] = false
|
||||
}
|
||||
@ -129,25 +137,21 @@ function openForm(btn: OperationButtonConfig) {
|
||||
}
|
||||
|
||||
function buildFormProps(btn: OperationButtonConfig): Record<string, any> {
|
||||
const props: Record<string, any> = {}
|
||||
const formProps: Record<string, any> = {}
|
||||
|
||||
// paramMap: { formProp: rowField } → 从行数据取值注入表单
|
||||
if (btn.paramMap) {
|
||||
for (const [formProp, rowField] of Object.entries(btn.paramMap)) {
|
||||
props[formProp] = props.row[rowField]
|
||||
}
|
||||
}
|
||||
// 使用 watch 捕获的最新行数据
|
||||
formProps.row = currentRow.value
|
||||
|
||||
// extraParams: 固定参数直接注入
|
||||
if (btn.extraParams) {
|
||||
Object.assign(props, btn.extraParams)
|
||||
Object.assign(formProps, btn.extraParams)
|
||||
}
|
||||
|
||||
return props
|
||||
return formProps
|
||||
}
|
||||
|
||||
function handleFormSuccess(key: string) {
|
||||
emit('operation-action', key, props.row)
|
||||
emit('operation-action', key, currentRow.value)
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
|
||||
@ -676,8 +676,7 @@ export const tabPages: TabPageRawConfig[] = [
|
||||
{
|
||||
key: 'edit',
|
||||
label: '编辑',
|
||||
actionType: 'form',
|
||||
formKey: 'CameraFormDialog',
|
||||
actionType: 'action',
|
||||
type: 'primary'
|
||||
},
|
||||
{
|
||||
|
||||
@ -103,20 +103,21 @@
|
||||
@sort-change="handleSortChange"
|
||||
@selection-change="handleSelectionChange"
|
||||
>
|
||||
<!-- 动态列插槽:根据 columnSlots 配置自动渲染 -->
|
||||
<!-- 动态列插槽:操作列通过 row['id'] 从 listData 查找完整数据 -->
|
||||
<template
|
||||
v-for="(slotConfig, colProp) in currentColumnSlots"
|
||||
:key="`columnSlots-${colProp}`"
|
||||
#[`column-${colProp}`]="{ row }"
|
||||
>
|
||||
<!-- 操作列:渲染操作按钮 -->
|
||||
<OperationCellRenderer
|
||||
v-if="slotConfig.type === 'operation'"
|
||||
:buttons="currentTabConfig?.operationButtons"
|
||||
:row="row"
|
||||
@operation-action="handleOperationAction"
|
||||
/>
|
||||
<!-- 其他列:标签/文本渲染 -->
|
||||
<!-- 操作列:使用 row['id'](slot scope 内可正常访问单属性)查找数据 -->
|
||||
<template v-if="colProp === 'operation'">
|
||||
<div class="operation-cell">
|
||||
<el-button link type="primary" size="small" @click.stop="openCameraForm('edit', getRowById(row['id']))">编辑</el-button>
|
||||
<el-button link type="danger" size="small" @click.stop="handleDeleteCamera(getRowById(row['id'])!)">删除</el-button>
|
||||
<el-button link type="success" size="small" @click.stop="handlePreview(getRowById(row['id'])!)">预览</el-button>
|
||||
</div>
|
||||
</template>
|
||||
<!-- 其他列 -->
|
||||
<ColumnCellRenderer
|
||||
v-else
|
||||
:config="slotConfig"
|
||||
@ -202,12 +203,13 @@
|
||||
:key="`columnSlots-${colProp}`"
|
||||
#[`column-${colProp}`]="{ row }"
|
||||
>
|
||||
<OperationCellRenderer
|
||||
v-if="slotConfig.type === 'operation'"
|
||||
:buttons="currentTabConfig?.operationButtons"
|
||||
:row="row"
|
||||
@operation-action="handleOperationAction"
|
||||
/>
|
||||
<template v-if="colProp === 'operation'">
|
||||
<div class="operation-cell">
|
||||
<el-button link type="primary" size="small" @click.stop="openCameraForm('edit', getRowById(row['id']))">编辑</el-button>
|
||||
<el-button link type="danger" size="small" @click.stop="handleDeleteCamera(getRowById(row['id'])!)">删除</el-button>
|
||||
<el-button link type="success" size="small" @click.stop="handlePreview(getRowById(row['id'])!)">预览</el-button>
|
||||
</div>
|
||||
</template>
|
||||
<ColumnCellRenderer
|
||||
v-else
|
||||
:config="slotConfig"
|
||||
@ -447,6 +449,12 @@ function openCameraForm(mode: 'create' | 'edit', row?: Record<string, any>) {
|
||||
cameraFormVisible.value = true
|
||||
}
|
||||
|
||||
/** 通过 ID 从 listData 中查找完整行数据(解决 slot scope 中 row 引用失效问题) */
|
||||
function getRowById(id: any): Record<string, any> | undefined {
|
||||
if (id === undefined || id === null) return undefined
|
||||
return (listData.value as any[]).find((item: any) => item.id === id)
|
||||
}
|
||||
|
||||
function handleFormSuccess() {
|
||||
loadData()
|
||||
}
|
||||
|
||||
@ -14,6 +14,7 @@
|
||||
align-center
|
||||
destroy-on-close
|
||||
:close-on-click-modal="false"
|
||||
append-to-body
|
||||
@closed="handleClosed"
|
||||
>
|
||||
<div class="form-body">
|
||||
@ -163,6 +164,7 @@ type ChannelItem = {
|
||||
type ServerOption = { id: number; name: string }
|
||||
|
||||
const isEdit = ref(false)
|
||||
const isPopulating = ref(false)
|
||||
|
||||
const formRef = ref<FormInstance>()
|
||||
const saving = ref(false)
|
||||
@ -217,8 +219,11 @@ function resetForm() {
|
||||
}
|
||||
|
||||
function populateFromRow(row: Record<string, any>) {
|
||||
isPopulating.value = true
|
||||
// 服务器 ID 可能在 row.server.id(关联对象)或 row.server_id(平级字段)
|
||||
const sid = Number(row.server?.id ?? row.server_id)
|
||||
form.id = row.id
|
||||
form.server_id = row.server_id
|
||||
form.server_id = Number.isNaN(sid) ? undefined : sid
|
||||
form.channel_id = row.channel_id
|
||||
form.channel_name = row.channel_name || ''
|
||||
form.ip = row.ip || ''
|
||||
@ -229,6 +234,10 @@ function populateFromRow(row: Record<string, any>) {
|
||||
form.custom_name = row.custom_name || ''
|
||||
form.status = row.status ?? 1
|
||||
form.remark = row.remark || ''
|
||||
// 预加载服务器选项,使 el-select 展示名称而非 ID
|
||||
loadServerOptions()
|
||||
// 延迟解除标记,确保 watch 回调(pre-flush)执行完毕
|
||||
setTimeout(() => { isPopulating.value = false }, 0)
|
||||
}
|
||||
|
||||
// 监听 visible + row 变化来初始化
|
||||
@ -253,12 +262,12 @@ async function loadServerOptions() {
|
||||
if (!isSuccess(res)) return
|
||||
const list = res?.list || []
|
||||
serverOptions.value = list.map((item: any) => ({
|
||||
id: item.id,
|
||||
id: Number(item.id),
|
||||
name: item.name || `服务器-${item.id}`
|
||||
}))
|
||||
list.forEach((item: any) => {
|
||||
if (item.id) {
|
||||
serverDetailMap.value[item.id] = {
|
||||
serverDetailMap.value[Number(item.id)] = {
|
||||
internal_ip: item.internal_ip,
|
||||
public_ip: item.public_ip,
|
||||
port: item.port,
|
||||
@ -372,16 +381,19 @@ function handleChannelSelect(value: number) {
|
||||
|
||||
watch(() => form.server_id, (newId, oldId) => {
|
||||
if (newId && newId !== oldId) {
|
||||
form.channel_id = undefined
|
||||
form.channel_name = ''
|
||||
form.ip = ''
|
||||
form.port = undefined
|
||||
form.model = ''
|
||||
form.serial_number = ''
|
||||
form.stream_type = ''
|
||||
channelOptions.value = []
|
||||
selectedServerNeedsPassword.value = false
|
||||
serverPassword.value = ''
|
||||
// 编辑模式下由 populateFromRow 手动填充字段,不自动清空
|
||||
if (!isPopulating.value) {
|
||||
form.channel_id = undefined
|
||||
form.channel_name = ''
|
||||
form.ip = ''
|
||||
form.port = undefined
|
||||
form.model = ''
|
||||
form.serial_number = ''
|
||||
form.stream_type = ''
|
||||
channelOptions.value = []
|
||||
selectedServerNeedsPassword.value = false
|
||||
serverPassword.value = ''
|
||||
}
|
||||
fetchChannelList()
|
||||
}
|
||||
})
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user