event.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import { BtnList } from '@/common/components/btnList/interface';
  2. import { ContextMenuTemp } from "@/common/components/contextMenu/interface";
  3. import { handleContextMenu } from "@/common/components/contextMenu/setup";
  4. import { getTableColumns, initData } from "@/common/export/table";
  5. import { TableKey } from '@/common/methods/table/interface';
  6. import { handleModalComponent } from '@/common/setup/asyncComponent';
  7. import { onUnmounted, ref } from "vue";
  8. import { getBtnList_ } from './button';
  9. import { ButtonListKey, TableEventCB } from './interface';
  10. /**
  11. * 表格事件
  12. * @param param TableEventCB
  13. * @returns
  14. */
  15. export function getTableEvent<T>(param: TableEventCB) {
  16. // 表格展开行
  17. const expandedRowKeys = ref<string[]>([]);
  18. // 表格选中的数据
  19. const selectedRow = ref<T>()
  20. // 右键数据
  21. const contextMenu = ref<ContextMenuTemp>({
  22. position: { clientX: 0, clientY: 0 },
  23. show: false,
  24. })
  25. function Rowclick(record: T, index: number) {
  26. return {
  27. onClick: () => { // 表格点击
  28. selectedRow.value = record
  29. const key = (record as any).key
  30. // 控制表格折腾面板
  31. const value = expandedRowKeys.value;
  32. if (value && value.length && value[0] === key) {
  33. expandedRowKeys.value = []
  34. } else {
  35. expandedRowKeys.value = [key]
  36. }
  37. param.clickCB && param.clickCB(record)
  38. },
  39. // onDblclick: () => { // 双击
  40. // console.log('onDblclick');
  41. // },
  42. onContextmenu: (event: MouseEvent) => { // 表格右键
  43. selectedRow.value = record
  44. const { clientX, clientY } = event;
  45. const value = {
  46. position: { clientX, clientY },
  47. show: true,
  48. }
  49. param.contextmenuCB && param.contextmenuCB(record, value)
  50. },
  51. };
  52. }
  53. // 处理表格展开行 数据不会及时更新问题
  54. function btnClick(record: T) {
  55. selectedRow.value = record
  56. }
  57. // 关闭右键
  58. function closeContext() {
  59. contextMenu.value.show = false
  60. }
  61. return { expandedRowKeys, selectedRow, Rowclick, btnClick, contextMenu, closeContext }
  62. }
  63. export interface ComposeTableParam {
  64. queryFn: Function, // 查询表格数据
  65. menuType: keyof ButtonListKey, // 操作按钮列表key
  66. tableName: keyof TableKey, // 表头key
  67. tableFilterKey: string[], // 表格过滤字段
  68. isDetail: boolean, // 是否需要详情
  69. }
  70. // 表格 + 表头 + 右键 + 单击表格 + 按钮列表 通用处理逻辑
  71. export function handleComposeTable<T>({ queryFn, menuType, isDetail, tableName, tableFilterKey }: ComposeTableParam) {
  72. // 右键逻辑
  73. const { contextMenu, openContext, closeContext: closeContextAction } = handleContextMenu();
  74. // 表头数据
  75. const { columns, registerColumn, updateColumn } = getTableColumns();
  76. // 右键回调函数
  77. const eventsCB: TableEventCB = {
  78. contextmenuCB: (record: T, value: ContextMenuTemp) => {
  79. // 控制打开右键
  80. contextMenu.value = value
  81. }
  82. }
  83. // 表格事件
  84. const { expandedRowKeys, selectedRow, Rowclick } = getTableEvent<T>(eventsCB);
  85. // 表格操作按钮列表
  86. const [firstBtn, secondBtn] = getBtnList_(menuType, isDetail).value;
  87. // 控制异步组件
  88. const { componentId, closeComponent, openComponent } = handleModalComponent(queryFn, selectedRow);
  89. // 关闭右键
  90. function closeContext(value: BtnList | null) {
  91. // 打开对应的弹窗组件
  92. if (value) openComponent(value, selectedRow.value)
  93. // 关闭右键
  94. closeContextAction()
  95. }
  96. onUnmounted(() => {
  97. // 离开组件,手动关闭右键,防止出现bug
  98. closeContextAction()
  99. })
  100. initData(() => {
  101. // 获取列表数据
  102. queryFn();
  103. // 注册表头信息 过滤
  104. registerColumn(tableName, tableFilterKey);
  105. }); return {
  106. contextMenu, openContext, closeContext, // 右键
  107. columns, registerColumn, updateColumn, // 表头
  108. expandedRowKeys, selectedRow, Rowclick, // 表格折腾面板数据与单击、双击事件
  109. componentId, closeComponent, openComponent, // 控制异步组件
  110. firstBtn, secondBtn, // 表格按钮
  111. }
  112. }