From e152896d51f1399c0b4973a160ef293079f5653a Mon Sep 17 00:00:00 2001 From: Ryan <1965086437@qq.com> Date: Wed, 9 Oct 2024 22:57:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=8C=89=E7=A7=AF=E5=88=86?= =?UTF-8?q?=E4=BB=8E=E9=AB=98=E5=88=B0=E4=BD=8E=E6=8E=92=E5=BA=8F=E5=92=8C?= =?UTF-8?q?=E6=8C=89=E5=AD=A6=E5=8F=B7=E4=BB=8E=E5=B0=8F=E5=88=B0=E5=A4=A7?= =?UTF-8?q?=E6=8E=92=E5=BA=8F=E7=9A=84=E6=8C=89=E9=92=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.html | 3 +++ index.js | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) 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