Update score_display.html

main
luoyonghuang 2 months ago
parent a4cf04db29
commit 1fa4bf0c55

@ -118,6 +118,7 @@
<thead> <thead>
<tr> <tr>
<th>姓名</th> <th>姓名</th>
<th>学号</th>
<th>积分</th> <th>积分</th>
</tr> </tr>
</thead> </thead>
@ -135,61 +136,112 @@
const studentList = document.getElementById('studentList'); const studentList = document.getElementById('studentList');
const sortButton = document.getElementById('sortButton'); const sortButton = document.getElementById('sortButton');
const clearButton = document.getElementById('clearButton'); const clearButton = document.getElementById('clearButton');
let ascending = false; // 默认降序 let ascend = false; // 默认降序
// 预定义学生名单
const predefinedStudents = [ //删除表
{ name: '张三', points: 0 }, async function deleteTable(tableName) {
{ name: '李四', points: 0 }, try {
{ name: '王五', points: 0 }, const response = await fetch('/api/delete-table', {
{ name: '赵六', points: 0 }, method: 'POST',
]; headers: {
'Content-Type': 'application/json',
// 获取所有学生,包括预定义名单 },
function getAllStudents() { body: JSON.stringify({ tableName }),
const students = predefinedStudents.map(student => {
const storedPoints = localStorage.getItem(student.name);
return {
name: student.name,
points: storedPoints !== null ? parseInt(storedPoints) : student.points
};
}); });
return students;
if (!response.ok) {
throw new Error('删除表失败');
}
const data = await response.json();
alert(data.message); // 成功后显示消息
} catch (error) {
console.error('删除表操作失败:', error);
alert('删除表失败');
}
}
// 获取按分数降序排列的学生排名
async function fetchDescendRanking() {
try {
const response = await fetch('/api/descend_ranking');
if (!response.ok) {
alert('无法获取降序排名');
return null;
}
const ranking = await response.json(); // 获取并解析为JSON数组
return ranking; // 返回排名数组
} catch (error) {
console.error('请求失败:', error);
return null;
}
}
// 获取按分数升序排列的学生排名
async function fetchAscendRanking() {
try {
const response = await fetch('/api/ascend_ranking');
if (!response.ok) {
alert('无法获取升序排名');
return null;
}
const ranking = await response.json(); // 获取并解析为JSON数组
return ranking; // 返回排名数组
} catch (error) {
console.error('请求失败:', error);
return null;
}
} }
// 显示学生积分 async function displayStudents() {
function displayStudents() { let ranking; // 声明 ranking 变量
const students = getAllStudents(); if (ascend) {
students.sort((a, b) => ascending ? a.points - b.points : b.points - a.points); ranking = await fetchAscendRanking(); // 获取升序排名
studentList.innerHTML = ''; } else {
students.forEach(student => { ranking = await fetchDescendRanking(); // 获取降序排名
}
// 检查 ranking 是否为空
if (!ranking) {
alert('空 ranking');
return; // 退出函数
}
studentList.innerHTML = ''; // 清空表格内容
// 只显示前五名学生
ranking.slice(0, 5).forEach(student => {
studentList.innerHTML += ` studentList.innerHTML += `
<tr> <tr>
<td>${student.name}</td> <td>${student.student_name}</td> <!-- 学生姓名 -->
<td>${student.points}</td> <td>${student.student_id}</td> <!-- 学生ID -->
<td>${student.score}</td> <!-- 学生分数 -->
</tr> </tr>
`; `;
}); });
} }
// 切换排序
sortButton.addEventListener('click', () => { sortButton.addEventListener('click', () => {
ascending = !ascending; ascend = !ascend;
sortButton.textContent = `切换排序 (${ascending ? '升序' : '降序'})`; sortButton.textContent = `切换排序 (${ascend ? '升序' : '降序'})`;
displayStudents(); displayStudents();
}); });
// 清除所有数据
clearButton.addEventListener('click', () => { clearButton.addEventListener('click', () => {
const confirmation = confirm("确定要清除所有学生数据吗?此操作无法撤销!"); const confirmation = confirm("确定要清除所有学生数据吗?此操作无法撤销!");
if (confirmation) { if (confirmation) {
localStorage.clear(); deleteTable('students');
alert("所有数据已清除!"); alert("所有信息已清除!");
displayStudents(); // 重新显示清空后的数据 displayStudents(); // 重新显示清空后的数据
} }
}); });
// 初次加载时显示学生积分
displayStudents(); displayStudents();
</script> </script>

Loading…
Cancel
Save