【vue前端】列表搜索+分页

3 篇文章 0 订阅

【vue前端】列表搜索+分页

效果图

在这里插入图片描述

分析前后端交互的数据

发送的请求

在这里插入图片描述
向/users接口发送一个get请求。
params中:

  • account_filter 是搜索的内容
  • limit 是每页的条目数
  • offset 是偏移量,即 limit*(currentPage - 1)

返回的数据

在这里插入图片描述
response.data中:

  • list 为当前页的列表具体内容,在el-table标签中进行展示
  • total 为列表的总条目数

使用Element UI的标签

<!-- 搜索框 -->
<el-input
	size="medium"
	placeholder="请输入想要搜索的用户名称"
	v-model="searchInput"
>
	<el-button
		slot="append"
		icon="el-icon-search"
		@click="getUserList()"
	></el-button>
</el-input>

<!-- 列表展示 -->
<el-table>
	<!-- 列表具体内容 -->
</el-table>

<!-- 分页状态栏 -->
<el-pagination
	background
	hide-on-single-page
	@current-change="getUserList"
	:page-size="pageSize"
	:current-page="currentPage"
	layout="prev, pager, next, jumper"
	:total="total"
	style="margin-top: 30px; text-align: center"
>
</el-pagination>
  • searchInput 是搜索框中输入的内容
  • getUserList 是获取列表调用的函数
  • pageSize 是每页的条目数
  • currentPage 是当前页号
  • total 是总条目数

script标签中代码

在data中,存储所需数据

data(){
	return{
		// 搜索输入内容
		searchInput: "",
		// 当前页
		currentPage: 1,
		// 每页记录数
		pageSize: 3,
		// 总记录数
		total: null,
		// 查询到的用户列表
		userlist: [],
	}
}

在created中,初始化页面时获取第一页列表内容

created() {
	this.getUserList();
},

在methods中,获取列表内容函数

methods:{
	// 获取用户列表
	getUserList: function (currentPage = 1) {
		let that = this;
		this.$axios({
			method: "get",
			url: "/users",
			params: {
				id_order: true,
				account_filter: this.searchInput, // 搜索的内容
				limit: this.pageSize, // 每页记录数
				offset: this.pageSize * (currentPage - 1), // 偏移量
			},
		})
		.then(function (response) {
			that.currentPage = currentPage; // 更新页号
			that.userlist = response.data.list; // 返回的当前页列表
			that.total = response.data.total; // 总条目数
		})
		.catch(function (error) {
			console.log(error);
		});
	},
}
  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值