<template><div><!-- 搜索欄 --><el-card id="search"><el-row><el-col :span="20"><el-input v-model="searchModel.name" placeholder="根據名字查詢"></el-input><el-input v-model="searchModel.email" placeholder="根據郵件查詢"></el-input><el-button @click="searchList" type="primary" round icon="el-icon-search">搜索</el-button></el-col><el-col :span="4"><el-button @click="openEditUI(null)" type="primary" icon="el-icon-plus" circle></el-button></el-col></el-row></el-card><!-- 結果列表 --><el-table :data="userList" style="width: 100%"><el-table-column type="index" label="#" width="180"><template slot-scope="scope">{{ (searchModel.pageNo - 1) * searchModel.pageSize + scope.$index + 1 }}</template></el-table-column><el-table-column prop="id" label="用戶id" width="180"></el-table-column><el-table-column prop="name" label="姓名"></el-table-column><el-table-column prop="age" label="年齡"></el-table-column><el-table-column prop="email" label="電子郵件"></el-table-column><el-table-column label="操作"><template slot-scope="scope"><el-button @click="openEditUI(scope.row.id)" type="primary" icon="el-icon-edit" circle></el-button><el-button type="danger" icon="el-icon-delete" circle></el-button></template></el-table-column></el-table><!-- 分頁功能 --><el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange":current-page="searchModel.pageNo" :page-sizes="[1, 5, 10, 15]" :page-size="searchModel.pageSize"layout="total, sizes, prev, pager, next, jumper" :total="total"></el-pagination><!-- 對話框 --><!-- Form --><el-dialog :title="title" :visible.sync="dialogFormVisible"><el-form :model="userForm" :rules="rules"><el-form-item label="名字" :label-width="formLabelWidth"><el-input v-model="userForm.name" autocomplete="off"></el-input></el-form-item><el-form-item label="age" :label-width="formLabelWidth"><el-input v-model="userForm.age" autocomplete="off"></el-input></el-form-item><el-form-item label="email" :label-width="formLabelWidth"><el-input v-model="userForm.email" autocomplete="off"></el-input></el-form-item></el-form><div slot="footer" class="dialog-footer"><el-button @click="dialogFormVisible = false">取 消</el-button><el-button type="primary" @click="performAction">確 定</el-button></div></el-dialog></div>
</template><script>
import axios from 'axios';
export default {data() {return {title: '添加',userForm: {name: '',age: '',email: ''},dialogFormVisible: false,total: 0,searchModel: {pageNo: 1,pageSize: 5,name: '',email: '',},userList: [],formLabelWidth: '50px', // 設置標簽的寬度rules: {name: [{ required: true, message: '請輸入用戶名', trigger: 'blur' },{ min: 3, max: 5, message: '長度在 3 到 5 個字符', trigger: 'blur' }],},}},methods: {openEditUI(id) {if (id == null) {this.title = "新增用戶";this.userForm = {name: '',age: '',email: ''};} else {this.title = "修改用戶";// 根據id查詢用戶數據axios.get(`/api/sys/demoTable/` + id).then(response => {this.userForm = response.data.data; // 填充編輯數據this.dialogFormVisible = true;}).catch(error => {console.error('獲取編輯數據失敗:', error);});}this.dialogFormVisible = true;},performAction() {if (this.title === '新增用戶') {const newData = {name: this.userForm.name,age: this.userForm.age,email: this.userForm.email};axios.post('/api/sys/demoTable/create', newData).then(response => {if (response.data.code == 1) {this.$message({message: '添加成功',type: 'success'});// 關閉對話框this.dialogFormVisible = false;// 可以更新列表,重新加載數據等操作// 重新加載數據this.getUserList();} else {console.error('數據添加失敗');}}).catch(error => {console.error('添加數據失敗:', error);});} else if (this.title === '修改用戶') {axios.put(`/api/sys/demoTable/update`, this.userForm).then(response => {if (response.data.code === 1) {this.$message({message: '更新成功',type: 'success'});this.dialogFormVisible = false;this.getUserList();} else {console.error('數據更新失敗');}}).catch(error => {console.error('更新數據失敗:', error);});}},// 分頁大小變化handleSizeChange(pageSize) {this.searchModel.pageSize = pageSize;this.getUserList();},// 當前頁碼變化handleCurrentChange(pageNo) {this.searchModel.pageNo = pageNo;this.getUserList();},// 獲取用戶列表getUserList() {axios.get('/api/sys/demoTable/list', { params: this.searchModel }).then(response => {console.log(response);const data = response.data.data;this.userList = data.rows;this.total = data.total;}).catch(error => {console.error('獲取用戶列表失敗:', error);});},searchList() {this.searchModel.pageNo = 1; // 重置頁碼為1,以獲取新的查詢結果// 構建查詢參數const queryParams = {name: this.searchModel.name,email: this.searchModel.email,pageNo: this.searchModel.pageNo,pageSize: this.searchModel.pageSize};// 發送 GET 請求axios.get('/api/sys/demoTable/list', { params: queryParams }).then(response => {const data = response.data.data;this.userList = data.rows;this.total = data.total;}).catch(error => {console.error('獲取用戶列表失敗:', error);});}},mounted() {// 初始化加載用戶列表this.getUserList();}}
</script><style>
#search .el-input {width: 300px;margin-right: 20px;
}
</style>
思路
關于 查詢和分頁的結合分析
1.查詢也就是在頁面上顯示數據,所以用到了分頁。
2. 用Mybatis-plus , 進行分頁用Map返回數據,注意關鍵點pageNo和pageSize,這是前端請求頭請求的數據,后端要返回一個查詢到的結果集和總頁數
@GetMapping("/list")public R<Map<String, Object>> getAllList(@RequestParam(value = "name", required = false) String name,@RequestParam(value = "email", required = false) String email,@RequestParam(value = "pageNo") Long pageNo,@RequestParam(value = "pageSize") Long pageSize) {
3. 關于 添加和修改的功能結合分析
? ? *? 添加按鈕和修改按鈕,共用一個對話框,用data:title,? 的值區分 兩個請求。
? ?* 添加按鈕:id為null 或 undifined,??
? ? ? 而修改按鈕id :肯定是有值的。
? ?*從而分別為title賦值 ,添加 | 修改,
在起一個方法,if ( 添加==添加?){
添加的請求
} else{
? 修改的請求
}