//工程 import { ref, nextTick } from 'vue' import type { Ref } from 'vue' import { useParentChildLineNestedRowsFalse } from '#/components/db-hst/nestedRows' // 用于存储 projectHstRef 的引用 let projectHstRef: Ref | null = null const selectedRow = ref(null) // 记录当前选中的行 // 回调函数,供 config.vue 使用 let onCellMouseDownCallback: ((row: number | null) => void) | null = null // 初始化函数,由 config.vue 调用 export function initProjectHst(hstRef: Ref, callback?: (row: number | null) => void) { projectHstRef = hstRef onCellMouseDownCallback = callback || null } const { load: originalLoad, codeRenderer, handleRowOperation, initSchema, highlightDeselect } = useParentChildLineNestedRowsFalse({ getHotInstance: () => projectHstRef?.value?.hotInstance, }) // 包装 load 函数,在加载前重置选中行 export function load(data: any[]) { selectedRow.value = null return originalLoad(data) } const columns:any[] = [ {type:'text',data:'number',title:'序号', width: 50, className: 'htCenter'}, {type:'text',data:'code',title:'编号',renderer: codeRenderer, code:true}, {type:'text',data:'name',title:'名称'}, {type:'text',data:'unit',title:'单位', width: 60, className: 'htCenter'}, {type:'text',data:'text1',title:'编码增加数', width: 100}, {type:'text',data:'text2',title:'尾节点', width: 100}, {type:'text',data:'text3',title:'子目编码增加数', width: 110}, ] export let dbSettings = { data: [], dataSchema: initSchema(columns), colWidths: 160, columns: columns, rowHeaders: false, nestedRows: false, bindRowsWithHeaders: true, contextMenu: { items: { custom_row_above: { name: '在上方插入行', callback: function() { handleRowOperation(this, 'above') } }, custom_row_below: { name: '在下方插入行', callback: function() { handleRowOperation(this, 'below') } }, separator1: '---------', custom_add_child: { name: '添加子行', callback: function() { handleRowOperation(this, 'child') } }, separator2: '---------', remove_row: { name: '删除行', callback: function() { handleRowOperation(this, 'delete') } }, // separator3: '---------', // undo: {}, // redo: {} } }, currentRowClassName: 'row-highlight', beforeOnCellMouseDown(event: MouseEvent, coords: any, TD: HTMLTableCellElement){ // 只能单击 if (event.button !== 0) { return // 直接返回,不执行后续逻辑 } selectedRow.value = coords // 触发回调函数 if (onCellMouseDownCallback) { onCellMouseDownCallback(selectedRow.value) } }, afterDeselect(){ highlightDeselect(this, selectedRow.value) } } // 定义 topHstRef 的自定义右键菜单 export const contextMenuItems = [ { key: 'row_above', name: '新增行', callback: (hotInstance: any) => { // 执行新增行操作 handleRowOperation(hotInstance, 'append') // 等待 DOM 更新后重新渲染以应用验证样式 nextTick(() => { hotInstance.render() }) } }, ]