1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
| <template> <div class="App"> <h1 class="title">学生管理系统</h1> <div class="button-container"> <el-button type="primary" @click="getStudents">获取学生信息</el-button> <el-button type="warning" @click="dialogVisible = true">添加学生信息</el-button> <el-button type="info" @click="dialogVisiblelogin = true">登 录</el-button> <el-button type="danger" @click="dialogVisibleregister = true">注 册</el-button> </div>
<el-dialog title="提示" :visible.sync="dialogVisible" width="30%" :before-close="handleClose"> <div>添加学生信息</div> <span slot="footer" class="dialog-footer"> <el-button @click="dialogVisible = false">取 消</el-button> <el-button type="primary" @click="addStudent">添加</el-button> </span> <div> <div><span>学号:</span><input v-model="newStudent.number" /></div> <div><span>姓名:</span><input v-model="newStudent.name" /></div> <div><span>年龄:</span><input v-model="newStudent.age" /></div> <div><span>语文:</span><input v-model="newStudent.chi" /></div> <div><span>数学:</span><input v-model="newStudent.math" /></div> <div><span>英语:</span><input v-model="newStudent.eng" /></div> </div> </el-dialog> <el-dialog title="登录" :visible.sync="dialogVisiblelogin" width="30%" :before-close="handleClose"> <div class="form-group"> <div class="form-item"> <span>用户名:</span><input type="text" v-model="user_for_login.username"> </div> <div class="form-item"> <span>密 码:</span><input type="password" v-model="user_for_login.password"> </div> </div> <span slot="footer" class="dialog-footer"> <el-button @click="dialogVisiblelogin = false">取 消</el-button> <el-button type="primary" @click="login">登 录</el-button> </span> </el-dialog>
<el-dialog title="注册" :visible.sync="dialogVisibleregister" width="30%" :before-close="handleClose"> <div class="form-group"> <div class="form-item"> <span>用户名:</span><input type="text" v-model="user_for_register.username"> </div> <div class="form-item"> <span>密 码:</span><input type="password" v-model="user_for_register.password"> </div> <div class="form-item"> <span>确认密码:</span><input type="password" v-model="user_for_register.confirmPassword"> </div> </div> <span slot="footer" class="dialog-footer"> <el-button @click="dialogVisibleregister = false">取 消</el-button> <el-button type="primary" @click="register">注 册</el-button> </span> </el-dialog>
<div class="col-8 offset-2"> <table class="table table-hover"> <thead> <tr> <th scope="col">学号</th> <th scope="col">姓名</th> <th scope="col">年龄</th> <th scope="col">语文</th> <th scope="col">数学</th> <th scope="col">英语</th> <th scope="col">操作</th> </tr> </thead> <tbody> <Student v-for="stu in displayedStudents" :key="stu.id" :student="stu" /> </tbody> </table> <div class="text-center"> <el-button-group> <el-button type="primary" icon="el-icon-arrow-left" @click="prevPage">上一页</el-button> <el-button type="primary" @click="nextPage">下一页<i class="el-icon-arrow-right el-icon--right"></i></el-button> </el-button-group> </div> </div> </div> </template>
<script> import axios from 'axios' import Student from "./components/Student.vue"
export default { name: 'App', components: { Student }, data() { return { user_for_login:{ username:"", password:"", }, user_for_register:{ username:"", password:"", confirmPassword:"", }, page: 1, pageSize: 10, students: [], dialogVisible: false, dialogVisiblelogin:false, dialogVisibleregister:false, newStudent: { number: "", name: "", age: "", chi: "", math: "", eng: "" } } }, computed: { displayedStudents() { const startIdx = (this.page - 1) * this.pageSize; return this.students.slice(startIdx, startIdx + this.pageSize); } }, methods: { getStudents() { if (sessionStorage.getItem("isLogined") == "true") { axios({ url: "http://localhost:8080/students", method: 'GET', }).then(res => { console.log(res.data); this.students = res.data; // 更新 students 数据 }) } else { this.$alert("请先登录"); } }, handleClose(done) { this.$confirm('确认关闭?') .then(_ => { done(); }) .catch(_ => {}); }, addStudent() { axios({ url: "http://localhost:8080/insert", method: 'POST', data: this.newStudent }); this.dialogVisible = false; }, prevPage() { if (this.page > 1) { this.page--; } }, nextPage() { const maxPage = Math.ceil(this.students.length / this.pageSize); if (this.page < maxPage) { this.page++; } }, login(){ axios({ url : "http://localhost:8080/login", data:this.user_for_login, method:"POST" }).then(res =>{ console.log(res.data); localStorage.setItem("login",res.data) if(res.data == "1"){ sessionStorage.setItem("isLogined","true"); } else { alert("用户名密码错误") }
}) this.dialogVisiblelogin = false }, register(){ axios({ url : "http://localhost:8080/register", data:this.user_for_register, method:"POST" }) this.$alert("注册成功") } } };
</script>
<style> .title { text-align: center; }
.col-8.offset-2 { margin-top: 20px; }
.button-container { display: flex; justify-content: center; margin-bottom: 20px; } .form-group { display: flex; flex-direction: column; }
.form-item { display: flex; align-items: center; margin-bottom: 10px; }
.form-item span { min-width: 100px; /* Adjust the width as needed */ display: inline-block; } </style>
|