useFormatter.ts 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { Ref } from 'vue'
  2. import { CellRenderProps } from '../../type'
  3. export function useFormatter(flag: Ref<boolean>) {
  4. const tableColumnFormatter = (columnLabel: string) => {
  5. return unref(flag) ? columnLabel : columnLabel.replace('/件', '')
  6. }
  7. const tableDataFormatter = ({
  8. cellData,
  9. column: { columnName, columnLabel },
  10. }: CellRenderProps) => {
  11. if (!unref(flag)) {
  12. const matched = columnLabel.match(/(?<=\()\S+(?=\))/)
  13. if (matched) {
  14. const machedStr = matched[0]
  15. const countIndex = machedStr.split('/').findIndex(str => str === '件')
  16. if (
  17. countIndex > -1 &&
  18. typeof cellData === 'string' &&
  19. cellData.split('/')[countIndex]
  20. ) {
  21. return cellData
  22. .split('/')
  23. .filter((_, index) => index !== countIndex)
  24. .join('/')
  25. }
  26. }
  27. }
  28. if (columnName.includes('Time') && typeof cellData === 'string') {
  29. return cellData.slice(5, -3).replace('T', '\n')
  30. }
  31. return cellData
  32. }
  33. return {
  34. tableColumnFormatter,
  35. tableDataFormatter,
  36. }
  37. }