totalRow.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <script setup lang="ts">
  2. interface Product {
  3. id: string;
  4. name: string;
  5. amount1: string;
  6. amount2: string;
  7. amount3: number;
  8. }
  9. interface SummaryMethodProps<T = Product> {
  10. columns: any[];
  11. data: T[];
  12. }
  13. const getSummaries = (param: SummaryMethodProps) => {
  14. const { columns, data } = param;
  15. const sums: string[] = [];
  16. columns.forEach((column, index) => {
  17. if (index === 0) {
  18. sums[index] = "Total Cost";
  19. return;
  20. }
  21. const values = data.map(item => Number(item[column.property]));
  22. if (!values.every(value => Number.isNaN(value))) {
  23. sums[index] = `$ ${values.reduce((prev, curr) => {
  24. const value = Number(curr);
  25. if (!Number.isNaN(value)) {
  26. return prev + curr;
  27. } else {
  28. return prev;
  29. }
  30. }, 0)}`;
  31. } else {
  32. sums[index] = "N/A";
  33. }
  34. });
  35. return sums;
  36. };
  37. const tableData: Product[] = [
  38. {
  39. id: "12987122",
  40. name: "Tom",
  41. amount1: "234",
  42. amount2: "3.2",
  43. amount3: 10
  44. },
  45. {
  46. id: "12987123",
  47. name: "Tom",
  48. amount1: "165",
  49. amount2: "4.43",
  50. amount3: 12
  51. },
  52. {
  53. id: "12987124",
  54. name: "Tom",
  55. amount1: "324",
  56. amount2: "1.9",
  57. amount3: 9
  58. },
  59. {
  60. id: "12987125",
  61. name: "Tom",
  62. amount1: "621",
  63. amount2: "2.2",
  64. amount3: 17
  65. },
  66. {
  67. id: "12987126",
  68. name: "Tom",
  69. amount1: "539",
  70. amount2: "4.1",
  71. amount3: 15
  72. }
  73. ];
  74. const columns: TableColumnList = [
  75. {
  76. label: "ID",
  77. prop: "id"
  78. },
  79. {
  80. label: "Name",
  81. prop: "name"
  82. },
  83. {
  84. label: "Amount 1",
  85. prop: "amount1",
  86. sortable: true
  87. },
  88. {
  89. label: "Amount 2",
  90. prop: "amount2",
  91. sortable: true
  92. },
  93. {
  94. label: "Amount 3",
  95. prop: "amount3",
  96. sortable: true
  97. }
  98. ];
  99. const columns1: TableColumnList = [
  100. {
  101. label: "ID",
  102. prop: "id"
  103. },
  104. {
  105. label: "Name",
  106. prop: "name"
  107. },
  108. {
  109. label: "Cost 1 ($)",
  110. prop: "amount1"
  111. },
  112. {
  113. label: "Cost 2 ($)",
  114. prop: "amount2"
  115. },
  116. {
  117. label: "Cost 3 ($)",
  118. prop: "amount3"
  119. }
  120. ];
  121. </script>
  122. <template>
  123. <pure-table
  124. :data="tableData"
  125. :columns="columns"
  126. border
  127. show-summary
  128. class="mb-6"
  129. />
  130. <pure-table
  131. :data="tableData"
  132. :columns="columns1"
  133. border
  134. :summary-method="getSummaries"
  135. show-summary
  136. />
  137. </template>