| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- import { queryResultLoadingAndInfo } from '@/common/methods/request/resultInfo';
- import { getDecimalsNum } from '@/utils/number';
- import { ref } from 'vue';
- /**
- * 请求表格数据通用方法
- * @param ishandleFloatErr 是否处理浮点失真问题
- * @returns
- */
- export function queryTableList<T>(ishandleFloatErr = false, num?: number) {
- // 加载状态
- const loading = ref<boolean>(false);
- // 表格数据
- const tableList = ref<T[]>([]);
- function queryTable(fn: Function, param?: any): Promise<T[]> {
- console.log('表格数据查询参数:', param);
- return queryResultLoadingAndInfo(fn, loading, param)
- .then(res => {
- if (!res) return []
- tableList.value.length = 0;
- const result = res.map((e: any, i: number) => {
- // 统一把空字符转 --
- Object.keys(e).forEach(el => {
- if (e[el] === '') {
- e[el] = '--'
- }
- })
- // 统一处理key,表格需要key,单击事件和双击需要依据就是这个key,如果服务返回了key这个字段,表格事情需要展开列表需要额外处理
- if (Reflect.has(e, 'key')) {
- return e
- } else {
- return { ...e, key: String(i) };
- }
- })
- if (ishandleFloatErr) { // 折中方案:处理浮点失真,如果页面卡顿,则需要服务处理
- result?.forEach((e: T, i: number) => {
- for (const item in e) {
- const decimal = (e as any).decimalplace
- const count = decimal ? decimal : num
- e[item] = getDecimalsNum(e[item], count, decimal)
- }
- });
- }
- tableList.value = result
- return result
- })
- }
- return { loading, tableList, queryTable }
- }
|