useElTableData.ts 1.0 KB

123456789101112131415161718192021222324252627282930313233
  1. import { nextTick, Ref } from 'vue';
  2. import { response } from '/@/utils/tools';
  3. /**
  4. * 获取 El-Table的数据并处理total和loading
  5. * @param apiFunction 请求的接口函数
  6. * @param query 请求参数
  7. * @param tableData El-Table的数据
  8. * @param total El-Table数据的总条数
  9. * @param loading El-Table的loading状态
  10. */
  11. export async function useElTableData(apiFunction: Function, query: any, tableData: Ref<any[]>, total: Ref<number>, loading: Ref<boolean>) {
  12. try {
  13. loading.value = true;
  14. const response = await apiFunction(query);
  15. let responseData = response.data;
  16. if (!Array.isArray(responseData)) {
  17. responseData = [ responseData ];
  18. }
  19. total.value = response.total;
  20. tableData.value = responseData;
  21. return { success: true, data: responseData, code: response.code, msg: response.msg };
  22. } catch (error) {
  23. console.error('Error in useElTableData==> ', error);
  24. return { success: false, error };
  25. } finally {
  26. loading.value = false;
  27. await nextTick();
  28. window.dispatchEvent(new Event('resize'));
  29. }
  30. }