use-table.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { initParams } from "@/utils/init";
  2. import { TABLE } from "@/api/business/purchase/contract";
  3. export default function useTable() {
  4. // 新 增
  5. const add = ({ _this, prop }) => {
  6. const { tableColumns } = _this.tabColumns.find(
  7. (element) => element.key === prop
  8. );
  9. _this.params[prop].push(initParams(tableColumns));
  10. };
  11. // 提 交
  12. const submit = async ({ _this, prop, scope }, done = () => {}) => {
  13. const { row } = scope;
  14. const { ADD, EDIT } = TABLE;
  15. try {
  16. // try
  17. _this.loading = true;
  18. const { contractId } = row;
  19. const { id } = _this.params;
  20. if (contractId) {
  21. await EDIT(row, prop);
  22. } else {
  23. await ADD({ ...row, contractId: id }, prop);
  24. }
  25. await done();
  26. } catch (err) {
  27. // catch
  28. console.error(err);
  29. } finally {
  30. // finally
  31. _this.loading = false;
  32. }
  33. };
  34. // 删 除
  35. const remove = ({ _this, prop, scope }, done = () => {}) => {
  36. const {
  37. row: { id },
  38. $index,
  39. } = scope;
  40. const { ROMVE } = TABLE;
  41. if (id) {
  42. try {
  43. // try
  44. _this.loading = true;
  45. const { code } = ROMVE(id, prop);
  46. if (code === 200) done();
  47. } catch (err) {
  48. // catch
  49. console.error(err);
  50. } finally {
  51. // finally
  52. _this.loading = false;
  53. }
  54. } else {
  55. _this.params[prop].splice($index, 1);
  56. }
  57. };
  58. return { add, submit, remove };
  59. }