WEB/src/views/material/packageMaterial2/components/OperationCellRenderer.vue
2026-07-22 18:18:30 +08:00

176 lines
4.9 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!--
OperationCellRenderer — 表格操作列渲染组件
根据 operationButtons 配置渲染按钮列表。
支持的行为类型:
- actionType='action' → 触发 @operation-action 事件(可选配置 confirmMessage 弹确认框)
- actionType='form' → 打开 COMPONENT_REGISTRY 中注册的表单组件
使用方式:在 index.vue 中通过插槽 column-operation 引入:
<OperationCellRenderer
:buttons="currentTabConfig?.operationButtons"
:row="row"
@operation-action="handleOperationAction"
/>
-->
<template>
<div class="operation-cell">
<template v-for="btn in buttons" :key="btn.key">
<!-- action 按钮:支持确认弹窗 -->
<el-button
v-if="btn.actionType === 'action'"
link
:type="btn.type || 'primary'"
size="small"
:loading="actionLoadingMap[btn.key]"
:disabled="isDisabled(btn)"
@click.stop="handleActionClick(btn)"
>
{{ btn.label }}
</el-button>
<!-- form 按钮:打开表单弹窗 -->
<template v-else-if="btn.actionType === 'form'">
<el-button
link
:type="btn.type || 'primary'"
size="small"
:disabled="isDisabled(btn)"
@click.stop="openForm(btn)"
>
{{ btn.label }}
</el-button>
<!-- 表单弹窗 -->
<component
:is="resolvedFormComponents[btn.key]"
v-if="resolvedFormComponents[btn.key] && activeFormKey === btn.key"
v-model:visible="formVisibleMap[btn.key]"
v-bind="buildFormProps(btn)"
@success="handleFormSuccess(btn.key)"
/>
</template>
</template>
</div>
</template>
<script setup lang="ts">
import { ref, reactive, type Component } from 'vue'
import { ElMessageBox } from 'element-plus'
import { COMPONENT_REGISTRY } from '../config/template-config'
import type { OperationButtonConfig } from '../config/template-config'
defineOptions({ name: 'OperationCellRenderer' })
const props = defineProps<{
/** 操作按钮配置列表(来自 TabPageConfig.operationButtons) */
buttons?: OperationButtonConfig[]
/** 当前行数据 */
row: Record<string, any>
}>()
const emit = defineEmits<{
/** action 按钮点击事件(key: 按钮标识, row: 行数据) */
(e: 'operation-action', key: string, row: Record<string, any>): void
}>()
// ===== form 弹窗管理 =====
const activeFormKey = ref('')
const formVisibleMap = reactive<Record<string, boolean>>({})
const resolvedFormComponents = ref<Record<string, Component>>({})
const actionLoadingMap = reactive<Record<string, boolean>>({})
// ============================================================
// action 按钮:支持确认弹窗
// ============================================================
async function handleActionClick(btn: OperationButtonConfig) {
if (btn.confirmMessage) {
try {
await ElMessageBox.confirm(
btn.confirmMessage,
btn.confirmTitle || '确认操作',
{
confirmButtonText: btn.confirmButtonText || '确定',
cancelButtonText: '取消',
type: (btn.confirmType || 'warning') as any
}
)
} catch {
return // 取消
}
}
actionLoadingMap[btn.key] = true
try {
emit('operation-action', btn.key, props.row)
} finally {
actionLoadingMap[btn.key] = false
}
}
// ============================================================
// form 按钮:打开表单弹窗
// ============================================================
function openForm(btn: OperationButtonConfig) {
if (!btn.formKey) return
const comp = COMPONENT_REGISTRY[btn.formKey]
if (!comp) {
console.warn(`[OperationCellRenderer] 未注册的表单组件: ${btn.formKey}`)
return
}
activeFormKey.value = btn.key
resolvedFormComponents.value[btn.key] = comp
formVisibleMap[btn.key] = true
}
function buildFormProps(btn: OperationButtonConfig): Record<string, any> {
const props: Record<string, any> = {}
// paramMap: { formProp: rowField } → 从行数据取值注入表单
if (btn.paramMap) {
for (const [formProp, rowField] of Object.entries(btn.paramMap)) {
props[formProp] = props.row[rowField]
}
}
// extraParams: 固定参数直接注入
if (btn.extraParams) {
Object.assign(props, btn.extraParams)
}
return props
}
function handleFormSuccess(key: string) {
emit('operation-action', key, props.row)
}
// ============================================================
// 按钮禁用判断
// ============================================================
function isDisabled(btn: OperationButtonConfig): boolean {
if (btn.disabledOnField) {
const val = props.row[btn.disabledOnField]
if (val === undefined || val === null || val === '' || val === false) {
return true
}
}
return false
}
</script>
<style lang="scss" scoped>
.operation-cell {
display: flex;
flex-wrap: wrap;
gap: 2px;
justify-content: center;
}
</style>