RankList.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <template>
  2. <div class="rank-list-container">
  3. <div class="rank-list-header">
  4. <span class="header-label">{{ headerLabel }}</span>
  5. <span class="header-name">{{ headerName }}</span>
  6. <span class="header-count">{{ headerCount }}</span>
  7. </div>
  8. <div class="rank-list-body">
  9. <div v-for="(item, index) in rankedData" :key="index" class="rank-item" :class="getItemClass(item.rank, index)">
  10. <span class="rank-number" :class="'rank-number-' + getRankClass(item.rank)">NO.{{ item.rank }}</span>
  11. <div class="rank-info">
  12. <span class="rank-name">{{ item.name }}</span>
  13. <div class="rank-bar">
  14. <div class="rank-bar-fill" :class="'rank-bar-fill-' + getRankClass(item.rank)" :style="{ width: getProgressWidth(item.value, maxValue) + '%' }"></div>
  15. </div>
  16. </div>
  17. <span class="rank-count">{{ item.value }}</span>
  18. </div>
  19. </div>
  20. </div>
  21. </template>
  22. <script setup>
  23. import { computed } from 'vue'
  24. const props = defineProps({
  25. rankData: {
  26. type: Array,
  27. default: () => []
  28. },
  29. title: {
  30. type: String,
  31. default: '排行榜'
  32. },
  33. headerLabel: {
  34. type: String,
  35. default: '排名'
  36. },
  37. headerName: {
  38. type: String,
  39. default: '分管班组长'
  40. },
  41. headerCount: {
  42. type: String,
  43. default: '计数'
  44. }
  45. })
  46. const maxValue = computed(() => {
  47. if (props.rankData.length === 0) return 100
  48. return Math.max(...props.rankData.map(item => item.value))
  49. })
  50. // 计算并列排名(中国式排名:并列后下一个排名+1)
  51. const rankedData = computed(() => {
  52. if (props.rankData.length === 0) return []
  53. // 按value降序排序
  54. const sorted = [...props.rankData].sort((a, b) => b.value - a.value)
  55. let currentRank = 1
  56. let previousValue = null
  57. return sorted.map((item) => {
  58. // 如果当前value小于前一个value,排名+1
  59. if (previousValue !== null && item.value < previousValue) {
  60. currentRank++
  61. }
  62. previousValue = item.value
  63. return {
  64. ...item,
  65. rank: currentRank
  66. }
  67. })
  68. })
  69. const getRankClass = (rank) => {
  70. if (rank === 1) return 'first'
  71. if (rank === 2) return 'second'
  72. if (rank === 3) return 'third'
  73. return 'default'
  74. }
  75. const getProgressWidth = (value, max) => {
  76. return (value / max) * 100
  77. }
  78. const getItemClass = (rank, index) => {
  79. const baseClass = 'rank-item rank-item-' + getRankClass(rank)
  80. if (rank <= 3) return baseClass
  81. const isGray = (index - 3) % 2 === 0
  82. return baseClass + (isGray ? ' rank-item-gray' : '')
  83. }
  84. </script>
  85. <style lang="less" scoped>
  86. .rank-list-container {
  87. width: 100%;
  88. height: 300px; /* 固定高度 */
  89. background: #fff;
  90. border-radius: 8px;
  91. border: 1px solid #e5e7eb;
  92. display: flex;
  93. flex-direction: column;
  94. overflow: hidden; /* 防止容器溢出 */
  95. }
  96. .rank-list-header {
  97. display: flex;
  98. align-items: center;
  99. padding: 8px;
  100. border-bottom: 1px solid #e5e7eb;
  101. flex-shrink: 0; /* 防止头部被压缩 */
  102. }
  103. .header-label {
  104. width: 80px;
  105. font-size: 16px;
  106. font-weight: 600;
  107. color: #374151;
  108. }
  109. .header-name {
  110. flex: 1;
  111. font-size: 16px;
  112. font-weight: 600;
  113. color: #374151;
  114. }
  115. .header-count {
  116. width: 80px;
  117. text-align: right;
  118. font-size: 16px;
  119. font-weight: 600;
  120. color: #374151;
  121. }
  122. .rank-list-body {
  123. flex: 1;
  124. overflow-y: auto; /* 启用垂直滚动 */
  125. /* 自定义滚动条样式 */
  126. &::-webkit-scrollbar {
  127. width: 6px;
  128. }
  129. &::-webkit-scrollbar-track {
  130. background: #f1f1f1;
  131. border-radius: 3px;
  132. }
  133. &::-webkit-scrollbar-thumb {
  134. background: #c1c1c1;
  135. border-radius: 3px;
  136. }
  137. &::-webkit-scrollbar-thumb:hover {
  138. background: #a8a8a8;
  139. }
  140. }
  141. .rank-item {
  142. display: flex;
  143. align-items: center;
  144. padding: 10px;
  145. border-radius: 4px;
  146. min-height: 40px; /* 最小高度确保项目可见 */
  147. }
  148. .rank-item:last-child {
  149. margin-bottom: 0;
  150. }
  151. .rank-item-first {
  152. background-color: #fef9e7;
  153. }
  154. .rank-item-second {
  155. background-color: #f3f4f6;
  156. }
  157. .rank-item-third {
  158. background-color: #fef2f2;
  159. }
  160. .rank-item:hover {
  161. background-color: #f9fafb;
  162. }
  163. .rank-item-gray {
  164. background-color: #f3f4f6;
  165. }
  166. .rank-number {
  167. font-weight: bold;
  168. font-size: 15px;
  169. padding: 4px 8px;
  170. border-radius: 4px;
  171. margin-right: 8px;
  172. min-width: 60px;
  173. text-align: center;
  174. }
  175. .rank-number-first {
  176. background: linear-gradient(135deg, #EFB63D, #F8CA4D);
  177. color: #fff;
  178. }
  179. .rank-number-second {
  180. background: linear-gradient(135deg, #C4CAE1, #E0E4ED);
  181. color: #fff;
  182. }
  183. .rank-number-third {
  184. background: linear-gradient(135deg, #D6A089, #E9C0AF);
  185. color: #fff;
  186. }
  187. .rank-number-default {
  188. background-color: #e5e7eb;
  189. color: #6b7280;
  190. }
  191. .rank-info {
  192. flex: 1;
  193. display: flex;
  194. flex-direction: row;
  195. align-items: center;
  196. gap: 8px;
  197. }
  198. .rank-name {
  199. font-size: 15px;
  200. color: #1f2937;
  201. font-weight: 500;
  202. min-width: 80px;
  203. overflow: hidden;
  204. text-overflow: ellipsis;
  205. white-space: nowrap;
  206. }
  207. .rank-bar {
  208. flex: 1;
  209. height: 12px;
  210. background-color: #e5e7eb;
  211. border-radius: 6px;
  212. overflow: hidden;
  213. }
  214. .rank-bar-fill {
  215. height: 100%;
  216. border-radius: 6px;
  217. transition: width 0.5s ease-out;
  218. }
  219. .rank-bar-fill-first {
  220. background: #FFCA00;
  221. }
  222. .rank-bar-fill-second {
  223. background: #C4C9DF;
  224. }
  225. .rank-bar-fill-third {
  226. background: #D99B86;
  227. }
  228. .rank-bar-fill-default {
  229. background: linear-gradient(90deg, #60a5fa, #3b82f6);
  230. }
  231. .rank-count {
  232. width: 80px;
  233. text-align: right;
  234. font-size: 15px;
  235. font-weight: 600;
  236. color: #1f2937;
  237. margin-left: 8px;
  238. }
  239. </style>