html
<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.查询也就是在页面上显示数据,所以用到了分页。
- 用Mybatis-plus , 进行分页用Map返回数据,注意关键点pageNo和pageSize,这是前端请求头请求的数据,后端要返回一个查询到的结果集和总页数
java
@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) {
- 关于 添加和修改的功能结合分析
* 添加按钮和修改按钮,共用一个对话框,用data:title, 的值区分 两个请求。
* 添加按钮:id为null 或 undifined,
而修改按钮id :肯定是有值的。
*从而分别为title赋值 ,添加 | 修改,
在起一个方法,if ( 添加==添加 ){
添加的请求
} else{
修改的请求
}