feat(学生): 添加学生课程查询功能
- 后端添加获取学生课程列表的API路由 - 实现Course模型中的findByStudentId方法查询学生课程 - 新增学生控制器的getCourses方法处理课程请求 - 前端添加课程表格展示及刷新功能
This commit is contained in:
@@ -9,8 +9,15 @@ class StudentManager {
|
||||
|
||||
init() {
|
||||
this.initDashboard();
|
||||
this.initCourses();
|
||||
this.updateCurrentTime();
|
||||
setInterval(() => this.updateCurrentTime(), 1000);
|
||||
|
||||
// 绑定刷新按钮
|
||||
const refreshBtn = document.getElementById('refreshCourses');
|
||||
if (refreshBtn) {
|
||||
refreshBtn.addEventListener('click', () => this.initCourses());
|
||||
}
|
||||
}
|
||||
|
||||
updateCurrentTime() {
|
||||
@@ -44,6 +51,51 @@ class StudentManager {
|
||||
console.error('Fetch dashboard data failed:', error);
|
||||
}
|
||||
}
|
||||
|
||||
async initCourses() {
|
||||
// 检查是否在仪表板页面
|
||||
if (!document.getElementById('coursesTableBody')) return;
|
||||
|
||||
try {
|
||||
const response = await fetch(`${this.apiBase}/courses`);
|
||||
const result = await response.json();
|
||||
|
||||
if (result.success) {
|
||||
this.renderCourses(result.data);
|
||||
} else {
|
||||
if (window.authManager) {
|
||||
window.authManager.showNotification(result.message || '获取课程失败', 'error');
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Fetch courses failed:', error);
|
||||
}
|
||||
}
|
||||
|
||||
renderCourses(courses) {
|
||||
const tbody = document.getElementById('coursesTableBody');
|
||||
if (tbody) {
|
||||
if (!courses || courses.length === 0) {
|
||||
tbody.innerHTML = '<tr><td colspan="6" class="text-center py-4 text-muted">暂无课程安排</td></tr>';
|
||||
return;
|
||||
}
|
||||
|
||||
tbody.innerHTML = courses.map(course => `
|
||||
<tr>
|
||||
<td><code>${course.course_code}</code></td>
|
||||
<td class="fw-bold">${course.course_name}</td>
|
||||
<td>${course.credit}</td>
|
||||
<td>${course.teacher_name}</td>
|
||||
<td>${course.semester || '2023-2024 下学期'}</td>
|
||||
<td>
|
||||
<button class="btn btn-sm btn-light border" onclick="studentManager.viewCourseDetails(${course.id})">
|
||||
<i class="fas fa-info-circle text-primary"></i> 详情
|
||||
</button>
|
||||
</td>
|
||||
</tr>
|
||||
`).join('');
|
||||
}
|
||||
}
|
||||
|
||||
renderDashboard(data) {
|
||||
const { grades, statistics } = data;
|
||||
@@ -88,6 +140,11 @@ class StudentManager {
|
||||
}
|
||||
}
|
||||
|
||||
viewCourseDetails(id) {
|
||||
console.log('View details for course:', id);
|
||||
// 可以弹出一个模态框显示更多信息
|
||||
}
|
||||
|
||||
updateElement(id, value) {
|
||||
const el = document.getElementById(id);
|
||||
if (el) el.textContent = value;
|
||||
@@ -132,4 +189,4 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
if (studentClassEl) studentClassEl.textContent = user.class || '未分配';
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user