TableData.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 { ref } from 'vue'
  48. const tableData = ref<any[]>([])
  49. const totalQty = ref(0)
  50. const totalsubtotal = ref(0)
  51. const change = (detail: any, index: number) => {
  52. tableData.value[index].subtotal = tableData.value[index].qty * tableData.value[index].score
  53. totalQty.value = tableData.value.reduce((sum, row) => Number(sum) + Number(row.qty), 0)
  54. totalsubtotal.value = tableData.value.reduce((sum, row) => Number(sum) + Number(row.subtotal), 0)
  55. }
  56. const setData = (data: any[], planDetails: any, planScore: number) => {
  57. totalQty.value = planDetails.qty
  58. totalsubtotal.value = planDetails.total
  59. tableData.value = data
  60. let arr = tableData.value.map((item) => item.baseId)
  61. arr = [...new Set(arr)]
  62. for (let currBaseId of arr) {
  63. const allSubtotal = tableData.value.reduce((accumulator, currentValue) => {
  64. if (currentValue.baseId === currBaseId) {
  65. return accumulator + currentValue.subtotal
  66. }
  67. return accumulator
  68. }, 0)
  69. const percentage = calculatePercentage(allSubtotal, planScore)
  70. tableData.value.forEach((item) => {
  71. if (item.baseId === currBaseId) {
  72. item.percentage = percentage
  73. }
  74. })
  75. }
  76. }
  77. const getData = () => {
  78. return tableData.value
  79. }
  80. const calculatePercentage = (part: any, total: any) => {
  81. if (total === 0) return 0 // 避免除以0错误
  82. return Number(((part / total) * 100).toFixed(2))
  83. }
  84. const showInfo = (item: any, index: number) => {
  85. if (index === 0) {
  86. return item.baseId
  87. }
  88. if (tableData.value[index - 1].baseId === item.baseId) {
  89. return ''
  90. } else {
  91. return item.baseId
  92. }
  93. }
  94. const showPercentage = (item: any, index: number) => {
  95. if (index === 0) {
  96. return item.percentage
  97. }
  98. if (tableData.value[index - 1].baseId === item.baseId) {
  99. return ''
  100. } else {
  101. return item.percentage
  102. }
  103. }
  104. defineExpose({ setData, getData })
  105. </script>
  106. <style lang="scss" scoped>
  107. .table {
  108. margin-top: 20rpx;
  109. width: 640rpx;
  110. min-height: 300rpx;
  111. background-color: #fff;
  112. border-radius: 12rpx;
  113. }
  114. </style>