clolumn.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import * as FormatterFn from '@/common/constants/enumsName';
  2. import { ColumnType, getTableHead } from '@/common/methods/table';
  3. import { TableKey } from '@/common/methods/table/interface';
  4. import { ref } from "vue";
  5. /**
  6. * 获取动态表头数据
  7. * ant-design 表格数据的过滤功能与表头绑定在一起
  8. */
  9. export function getTableColumns() {
  10. // 表头数据
  11. const columns = ref<ColumnType[]>([]);
  12. // 过滤信息
  13. const filteredInfo = ref();
  14. // 缓存 注册表头回调
  15. let cacheColumnCB: Function = (): void => { };
  16. // 缓存 动态表头 key
  17. let cacheTableKey: keyof TableKey = 'table_pcweb_userinfo';
  18. let cacheFilterKeyList: string[] = []
  19. /**
  20. * 注册表头
  21. * @param tableName 动态表头数据key
  22. * @param filterKeyList 添加过滤属性的key
  23. * @param cb 回调函数,处理添加不同表格过滤,排序等功能
  24. */
  25. function registerColumn(tableName: keyof TableKey, filterKeyList: string[], cb?: Function) {
  26. cacheTableKey = tableName;
  27. cacheColumnCB = cb ? cb : () => { };
  28. cacheFilterKeyList = filterKeyList;
  29. const list = getTableHead(tableName);
  30. const filtered = filteredInfo.value || {};
  31. columns.value.length = 0;
  32. list.forEach((e, i) => {
  33. const { columnfield, columntitle, columnwidth, aligntype, formatterstring } = e;
  34. const item: ColumnType = {
  35. key: String(i + 'th'),
  36. dataIndex: columnfield, // 表格数据对应的key
  37. title: columntitle,
  38. align: aligntype === 1 ? 'center' : aligntype === 2 ? 'left' : 'right',
  39. slots: { customRender: columnfield },
  40. };
  41. // 序号
  42. if (columntitle === '序号') {
  43. item.customRender = (obj: any) => `${obj.index + 1}`
  44. item.width = 30
  45. }
  46. // 设置表格宽度
  47. if (columnwidth && columnwidth !== '0') {
  48. item.width = +columnwidth
  49. } else {
  50. if (columntitle.includes('时间') || columntitle.includes('编号')) {
  51. item.width = 200
  52. } else {
  53. const len = columntitle.length
  54. if (len > 4) {
  55. item.width = 120 + 10 * (len - 4)
  56. } else {
  57. item.width = 120 // 默认120
  58. }
  59. }
  60. }
  61. // 数据格式化
  62. if (formatterstring) {
  63. const fn = getFromatterFn(formatterstring)
  64. if (fn) {
  65. item.customRender = fn
  66. }
  67. }
  68. // 以下添加过滤数据对应的方法
  69. filterKeyList.forEach(el => {
  70. if (e.columnfield === el) {
  71. item.onFilter = (value: string, record: any) => record[el].toString().includes(String(value));
  72. item.filteredValue = filtered[el] || null;
  73. }
  74. })
  75. cb && cb(e, item, filtered);
  76. columns.value.push(item);
  77. });
  78. console.log('columns', columns);
  79. }
  80. /**
  81. * 更新动态表头
  82. */
  83. function updateColumn(value: object) {
  84. console.log('过滤参数', value);
  85. filteredInfo.value = value
  86. registerColumn(cacheTableKey, cacheFilterKeyList, cacheColumnCB)
  87. }
  88. return { columns, registerColumn, updateColumn }
  89. }
  90. /**
  91. * 获取对应的格式化函数
  92. * @param val
  93. * @returns 格式化函数
  94. */
  95. function getFromatterFn(val: string) {
  96. const arr = val.split(',');
  97. const first = arr[0]
  98. let result: Function | null = null
  99. switch (first) {
  100. case 'FormatterUnit': // 处理单位
  101. result = (obj: any) => arr[1] ? obj.text + obj.record[arr[1]] : obj.text
  102. break;
  103. case 'FromatterPercent': // 数值百分比
  104. result = (obj: any) => (Number(obj.text) * 100).toFixed(2) + '%'
  105. break
  106. case 'FromatterDecimal': // 数值小数位
  107. result = (obj: any) => {
  108. const value = obj.text;
  109. return (value && arr[1]) ? value.toFixed(obj.record[arr[1]]) : value
  110. }
  111. break
  112. case 'getContractTypeName': // 合同类型名称
  113. result = FormatterFn.getContractTypeName
  114. break
  115. case 'getBizTypeName': // 业务类型
  116. result = FormatterFn.getBizTypeName
  117. break
  118. case 'getBuyOrSellName': // 持仓方向
  119. result = FormatterFn.getBuyOrSellName
  120. break
  121. case 'getChangeLogType': // 变更流水类型
  122. result = FormatterFn.getChangeLogType
  123. break
  124. case 'getLogType' : // 计划类型
  125. result = FormatterFn.getLogType
  126. break
  127. default:
  128. if (first) {
  129. console.warn(`请开发人员配置${first}格式化函数!`)
  130. }
  131. break
  132. }
  133. return result
  134. }
  135. // table的expandIcon属性,修改默认展开关闭按钮 子表无数据时不显示展开图标
  136. export function expandIcon(props: any) {
  137. return null
  138. }