clolumn.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { ColumnType, getTableHead } from '@/common/methods/table';
  2. import { TableKey } from '@/common/methods/table/interface';
  3. import { ref } from "vue";
  4. /**
  5. * 获取动态表头数据
  6. * ant-design 表格数据的过滤功能与表头绑定在一起
  7. */
  8. export function getTableColumns() {
  9. // 表头数据
  10. const columns = ref<ColumnType[]>([]);
  11. // 过滤信息
  12. const filteredInfo = ref();
  13. // 缓存 注册表头回调
  14. let cacheColumnCB: Function = (): void => { };
  15. // 缓存 动态表头 key
  16. let cacheTableKey: keyof TableKey = 'table_pcweb_userinfo';
  17. let cacheFilterKeyList: string[] = []
  18. /**
  19. * 注册表头
  20. * @param tableName 动态表头数据key
  21. * @param filterKeyList 添加过滤属性的key
  22. * @param cb 回调函数,处理添加不同表格过滤,排序等功能
  23. */
  24. function registerColumn(tableName: keyof TableKey, filterKeyList: string[], cb?: Function) {
  25. cacheTableKey = tableName;
  26. cacheColumnCB = cb ? cb : () => { };
  27. cacheFilterKeyList = filterKeyList;
  28. const list = getTableHead(tableName);
  29. const filtered = filteredInfo.value || {};
  30. columns.value.length = 0;
  31. list.forEach((e, i) => {
  32. const { columnfield, columntitle, columnwidth, aligntype } = e;
  33. console.log();
  34. const item: ColumnType = {
  35. key: String(i),
  36. dataIndex: columnfield, // 表格数据对应的key
  37. title: columntitle,
  38. align: aligntype === 1 ? 'center' : aligntype === 2 ? 'left' : 'right',
  39. slots: { customRender: columnfield },
  40. };
  41. if (columntitle === '序号') {
  42. item.render = (text: any, record: any, index: number) => `${index + 1}`
  43. }
  44. if (columnwidth) {
  45. item.width = +columnwidth
  46. }
  47. // 以下添加过滤数据对应的方法
  48. filterKeyList.forEach(el => {
  49. if (e.columnfield === el) {
  50. item.onFilter = (value: string, record: any) => record[el].toString().includes(String(value));
  51. item.filteredValue = filtered[el] || null;
  52. }
  53. })
  54. cb && cb(e, item, filtered);
  55. columns.value.push(item);
  56. });
  57. }
  58. /**
  59. * 更新动态表头
  60. */
  61. function updateColumn() {
  62. registerColumn(cacheTableKey, cacheFilterKeyList, cacheColumnCB)
  63. }
  64. return { columns, registerColumn, updateColumn, filteredInfo }
  65. }