event.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { ref } from "vue";
  2. import { TableEventCB } from './interface';
  3. /**
  4. * 表格事件
  5. * @param param TableEventCB
  6. * @returns
  7. */
  8. export function getTableEvent<T>(param: TableEventCB) {
  9. // 表格展开行
  10. const expandedRowKeys = ref<string[]>([]);
  11. // 表格选中的数据
  12. const selectedRow = ref({})
  13. function Rowclick(record: T, index: number) {
  14. return {
  15. onClick: () => { // 表格点击
  16. selectedRow.value = record
  17. const value = expandedRowKeys.value;
  18. if (value.length) {
  19. const key = value[0];
  20. expandedRowKeys.value = key === index.toString() ? [] : [`${index}`];
  21. } else {
  22. expandedRowKeys.value = [`${index}`]
  23. }
  24. param.clickCB && param.clickCB()
  25. },
  26. // onDblclick: () => { // 双击
  27. // console.log('onDblclick');
  28. // },
  29. onContextmenu: () => { // 表格右键
  30. selectedRow.value = record
  31. param.contextmenuCB && param.contextmenuCB()
  32. },
  33. };
  34. }
  35. function btnClick(record: T) {
  36. selectedRow.value = record
  37. }
  38. return { expandedRowKeys, selectedRow, Rowclick, btnClick }
  39. }