tableData.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <template>
  2. <view class="table">
  3. <uni-table border stripe emptyText="暂无更多数据">
  4. <!-- 表头行 -->
  5. <uni-tr>
  6. <uni-th align="center" width="50">序号</uni-th>
  7. <uni-th align="center">上级服务类型</uni-th>
  8. <uni-th align="center" width="50">占比(%)</uni-th>
  9. <uni-th align="center">任务类型</uni-th>
  10. <uni-th align="center" width="50">积分值</uni-th>
  11. <uni-th align="center" width="50">任务个数</uni-th>
  12. <uni-th align="center" width="80">任务积分值</uni-th>
  13. </uni-tr>
  14. <!-- 表格数据行 -->
  15. <uni-tr v-for="(item, index) in tableData" :key="item?.taskTypeId" index>
  16. <uni-td>{{ index + 1 }}</uni-td>
  17. <uni-td>{{ showInfo(item, index) }}</uni-td>
  18. <uni-td>
  19. <text
  20. :class="{
  21. error: item.score != 0 && (item.percentage < 25 || item.percentage > 50)
  22. }"
  23. >{{ showPercentage(item, index) }}</text
  24. >
  25. </uni-td>
  26. <uni-td>{{ item.taskTypeName }}</uni-td>
  27. <uni-td>{{ item.score }}</uni-td>
  28. <uni-td>
  29. <input type="number" @blur="(e) => change(e, index, item)" v-model="item.qty" />
  30. </uni-td>
  31. <uni-td>{{ item.subtotal }}</uni-td>
  32. </uni-tr>
  33. <!-- 结尾 -->
  34. <uni-tr>
  35. <uni-td>总计</uni-td>
  36. <uni-td>---</uni-td>
  37. <uni-td>---</uni-td>
  38. <uni-td>---</uni-td>
  39. <uni-td>---</uni-td>
  40. <uni-td>{{ totalQty }}</uni-td>
  41. <uni-td>{{ totalsubtotal }}</uni-td>
  42. </uni-tr>
  43. </uni-table>
  44. </view>
  45. </template>
  46. <script setup lang="ts">
  47. import { getWmtasktypePageApi } from '@/service/modules/objectivesList'
  48. import { onMounted, ref } from 'vue'
  49. const porops = defineProps({
  50. planScore: Number
  51. })
  52. const tableData = ref<any[]>([])
  53. const getList = async () => {
  54. const obj = {
  55. current: 1,
  56. size: 100,
  57. status: '3'
  58. }
  59. const res = await getWmtasktypePageApi(obj)
  60. tableData.value = res?.data?.records.map((item: any) => {
  61. if (item.delFlag == 0) {
  62. const obj = {
  63. qty: 0,
  64. subtotal: 0,
  65. editable: false,
  66. baseId: item.baseId,
  67. percentage: 0,
  68. taskTypeName: item.taskTypeName,
  69. score: item.score,
  70. taskTypeId: item.id
  71. }
  72. return obj
  73. }
  74. })
  75. }
  76. const showInfo = (item: any, index: number) => {
  77. if (index === 0) {
  78. return item.baseId
  79. }
  80. if (tableData.value[index - 1].baseId === item.baseId) {
  81. return ''
  82. } else {
  83. return item.baseId
  84. }
  85. }
  86. const showPercentage = (item: any, index: number) => {
  87. if (index === 0) {
  88. return item.percentage
  89. }
  90. if (tableData.value[index - 1].baseId === item.baseId) {
  91. return ''
  92. } else {
  93. return item.percentage
  94. }
  95. }
  96. onMounted(() => {
  97. getList()
  98. })
  99. const totalQty = ref(0)
  100. const totalsubtotal = ref(0)
  101. const calculatePercentage = (part, total) => {
  102. if (total === 0) return 0 // 避免除以0错误
  103. return Number(((part / total) * 100).toFixed(2))
  104. }
  105. const change = (detail: any, index: number, row: any) => {
  106. tableData.value[index].subtotal = tableData.value[index].qty * tableData.value[index].score
  107. totalQty.value = tableData.value.reduce((sum, row) => Number(sum) + Number(row.qty), 0)
  108. totalsubtotal.value = tableData.value.reduce((sum, row) => Number(sum) + Number(row.subtotal), 0)
  109. const allSubtotal = tableData.value.reduce((accumulator, currentValue) => {
  110. if (currentValue.baseId === row.baseId) {
  111. return accumulator + currentValue.subtotal
  112. }
  113. return accumulator
  114. }, 0)
  115. const percentage = calculatePercentage(allSubtotal, porops.planScore)
  116. tableData.value.forEach((item) => {
  117. if (item.baseId === row.baseId) {
  118. item.percentage = percentage
  119. }
  120. })
  121. }
  122. const getData = () => {
  123. return tableData.value
  124. }
  125. defineExpose({ getData })
  126. </script>
  127. <style lang="scss" scoped>
  128. .table {
  129. width: 700rpx;
  130. min-height: 300rpx;
  131. background-color: #fff;
  132. border-radius: 12rpx;
  133. }
  134. .error {
  135. color: #f78989;
  136. }
  137. </style>