diff --git a/index.html b/index.html index e1c6ff0..0ed1a56 100644 --- a/index.html +++ b/index.html @@ -115,6 +115,9 @@

学生名单

+

+ +

学号 diff --git a/index.js b/index.js index eb839f9..6c79f94 100644 --- a/index.js +++ b/index.js @@ -223,4 +223,41 @@ document.getElementById('addScoreButton').addEventListener('click', function () } else { alert('输入的分数应在 0.5 到 3 之间且为有效数字。'); } +}); +// 按积分从高到低排序 +document.getElementById('sortByPointsButton').addEventListener('click', function () { + updatedData.sort((a, b) => { + let pointsKeyA = Object.keys(a).find(key => key.includes('积分')); + let pointsKeyB = Object.keys(b).find(key => key.includes('积分')); + return (pointsKeyB ? parseInt(b[pointsKeyB]) : 0) - (pointsKeyA ? parseInt(a[pointsKeyA]) : 0); + }); + var displayDiv = document.getElementById('excelDataDisplay'); + displayDiv.innerHTML = '
学号姓名积分
'; + updatedData.forEach(function (row) { + var rowDiv = document.createElement('div'); + Object.keys(row).forEach(key => { + var cellDiv = document.createElement('span'); + cellDiv.textContent = row[key] + ' '; + rowDiv.appendChild(cellDiv); + }); + displayDiv.appendChild(rowDiv); + }); +}); + +// 按学号从小到大排序 +document.getElementById('sortByStudentIdButton').addEventListener('click', function () { + updatedData.sort((a, b) => { + return a[Object.keys(a)[0]] - b[Object.keys(b)[0]]; + }); + var displayDiv = document.getElementById('excelDataDisplay'); + displayDiv.innerHTML = '
学号姓名积分
'; + updatedData.forEach(function (row) { + var rowDiv = document.createElement('div'); + Object.keys(row).forEach(key => { + var cellDiv = document.createElement('span'); + cellDiv.textContent = row[key] + ' '; + rowDiv.appendChild(cellDiv); + }); + displayDiv.appendChild(rowDiv); + }); }); \ No newline at end of file