useElTableData.ts 992 B

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