|
| 1 | +<template> |
| 2 | + <div class="container mx-auto p-4 md:p-8 bg-gray-50 min-h-screen"> |
| 3 | + <div class="max-w-4xl mx-auto bg-white rounded-xl shadow-lg overflow-hidden"> |
| 4 | + <div class="p-6 md:p-8"> |
| 5 | + <h2 class="text-2xl md:text-3xl font-bold text-gray-800 mb-6 text-center"> |
| 6 | + 👨🎓 學生名單與成績 |
| 7 | + </h2> |
| 8 | + |
| 9 | + <div class="grid grid-cols-1 md:grid-cols-2 gap-4 mb-6 text-center"> |
| 10 | + <div class="bg-blue-100 text-blue-800 p-4 rounded-lg"> |
| 11 | + <p class="text-lg font-semibold">平均分數</p> |
| 12 | + <p class="text-3xl font-bold">{{ averageScore }}</p> |
| 13 | + </div> |
| 14 | + <div class="bg-green-100 text-green-800 p-4 rounded-lg"> |
| 15 | + <p class="text-lg font-semibold">最高分數</p> |
| 16 | + <p class="text-3xl font-bold">{{ highestScore }}</p> |
| 17 | + </div> |
| 18 | + </div> |
| 19 | + |
| 20 | + <div class="overflow-x-auto"> |
| 21 | + <table class="min-w-full text-left text-sm font-light"> |
| 22 | + <thead class="border-b bg-gray-800 text-white font-medium"> |
| 23 | + <tr> |
| 24 | + <th scope="col" class="px-6 py-4">學號</th> |
| 25 | + <th scope="col" class="px-6 py-4">姓名</th> |
| 26 | + <th scope="col" class="px-6 py-4 text-center">分數</th> |
| 27 | + <th scope="col" class="px-6 py-4 text-center">評級</th> |
| 28 | + </tr> |
| 29 | + </thead> |
| 30 | + <tbody> |
| 31 | + <tr |
| 32 | + v-for="student in students" |
| 33 | + :key="student.id" |
| 34 | + class="border-b transition duration-300 ease-in-out hover:bg-gray-100" |
| 35 | + > |
| 36 | + <td class="whitespace-nowrap px-6 py-4 font-mono">{{ student.id }}</td> |
| 37 | + <td class="whitespace-nowrap px-6 py-4 font-medium">{{ student.name }}</td> |
| 38 | + <td class="whitespace-nowrap px-6 py-4 text-center"> |
| 39 | + <span |
| 40 | + :class="getScoreClass(student.score)" |
| 41 | + class="px-3 py-1 text-base font-bold rounded-full" |
| 42 | + > |
| 43 | + {{ student.score }} |
| 44 | + </span> |
| 45 | + </td> |
| 46 | + <td class="whitespace-nowrap px-6 py-4 text-center text-2xl"> |
| 47 | + {{ getGradeEmoji(student.score) }} |
| 48 | + </td> |
| 49 | + </tr> |
| 50 | + </tbody> |
| 51 | + </table> |
| 52 | + </div> |
| 53 | + </div> |
| 54 | + </div> |
| 55 | + </div> |
| 56 | +</template> |
| 57 | + |
1 | 58 | <script setup> |
2 | | -import { ref } from 'vue' |
| 59 | +import { ref, computed } from 'vue' |
| 60 | +
|
3 | 61 | const students = ref([ |
4 | 62 | { id: 's001', name: '洧杰', score: 90 }, |
5 | 63 | { id: 's002', name: '小花', score: 85 }, |
6 | 64 | { id: 's003', name: '阿明', score: 78 }, |
7 | 65 | { id: 's004', name: '佩佩', score: 92 }, |
| 66 | + { id: 's005', name: '志強', score: 65 }, |
| 67 | + { id: 's006', name: '美玲', score: 58 }, |
8 | 68 | ]) |
| 69 | +
|
| 70 | +// 計算平均分數 |
| 71 | +const averageScore = computed(() => { |
| 72 | + if (students.value.length === 0) return 0 |
| 73 | + const total = students.value.reduce((acc, student) => acc + student.score, 0) |
| 74 | + return (total / students.value.length).toFixed(1) // 取到小數點後一位 |
| 75 | +}) |
| 76 | +
|
| 77 | +// 計算最高分數 |
| 78 | +const highestScore = computed(() => { |
| 79 | + if (students.value.length === 0) return 0 |
| 80 | + return Math.max(...students.value.map((s) => s.score)) |
| 81 | +}) |
| 82 | +
|
| 83 | +// 根據分數回傳對應的 CSS class |
| 84 | +function getScoreClass(score) { |
| 85 | + if (score >= 90) return 'bg-green-200 text-green-800' |
| 86 | + if (score >= 80) return 'bg-blue-200 text-blue-800' |
| 87 | + if (score >= 70) return 'bg-yellow-200 text-yellow-800' |
| 88 | + if (score >= 60) return 'bg-orange-200 text-orange-800' |
| 89 | + return 'bg-red-200 text-red-800' |
| 90 | +} |
| 91 | +
|
| 92 | +// 根據分數回傳對應的表情符號 |
| 93 | +function getGradeEmoji(score) { |
| 94 | + if (score >= 90) return '🏆' |
| 95 | + if (score >= 80) return '👍' |
| 96 | + if (score >= 60) return '✅' |
| 97 | + return '😥' |
| 98 | +} |
9 | 99 | </script> |
10 | 100 |
|
11 | | -<template> |
12 | | - <div class="p-6"> |
13 | | - <h2 class="text-xl font-bold mb-4">👨🎓 學生名單</h2> |
14 | | - <ul> |
15 | | - <li v-for="student in students" :key="student.id" class="mb-2 p-2 border rounded"> |
16 | | - <p>🆔 學號:{{ student.id }}</p> |
17 | | - <p>📛 姓名:{{ student.name }}</p> |
18 | | - <p>📈 分數:{{ student.score }}</p> |
19 | | - </li> |
20 | | - </ul> |
21 | | - </div> |
22 | | -</template> |
| 101 | +<style scoped> |
| 102 | +/* 這裡可以放一些無法用 Tailwind 輕鬆實現的自訂樣式,但此範例中我們主要使用 Tailwind */ |
| 103 | +</style> |
0 commit comments