list.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { queryResultLoadingAndInfo } from '@/common/methods/request/resultInfo';
  2. import { getDecimalsNum } from '@/utils/number';
  3. import { ref } from 'vue';
  4. /**
  5. * 请求表格数据通用方法
  6. * @param ishandleFloatErr 是否处理浮点失真问题
  7. * @returns
  8. */
  9. export function queryTableList<T>(ishandleFloatErr = false, num?: number) {
  10. // 加载状态
  11. const loading = ref<boolean>(false);
  12. // 表格数据
  13. const tableList = ref<T[]>([]);
  14. function queryTable(fn: Function, param?: any): Promise<T[]> {
  15. console.log('表格数据查询参数:', param);
  16. return queryResultLoadingAndInfo(fn, loading, param)
  17. .then(res => {
  18. if (!res) return []
  19. tableList.value.length = 0;
  20. const result = res.map((e: any, i: number) => {
  21. // 统一把空字符转 --
  22. Object.keys(e).forEach(el => {
  23. if (e[el] === '') {
  24. e[el] = '--'
  25. }
  26. })
  27. // 统一处理key,表格需要key,单击事件和双击需要依据就是这个key,如果服务返回了key这个字段,表格事情需要展开列表需要额外处理
  28. if (Reflect.has(e, 'key')) {
  29. return e
  30. } else {
  31. return { ...e, key: String(i) };
  32. }
  33. })
  34. if (ishandleFloatErr) { // 折中方案:处理浮点失真,如果页面卡顿,则需要服务处理
  35. result?.forEach((e: T, i: number) => {
  36. for (const item in e) {
  37. const decimal = (e as any).decimalplace
  38. const count = decimal ? decimal : num
  39. e[item] = getDecimalsNum(e[item], count, decimal)
  40. }
  41. });
  42. }
  43. tableList.value = result
  44. return result
  45. })
  46. }
  47. return { loading, tableList, queryTable }
  48. }