useElTable.ts 991 B

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