vue+ElementUI——表格分页(前端实现方法)

话不多说,直接上代码!!!!

<template>
	<div class="list">
		<div class="tab">
			<el-table
				:data="tableData"
				border
				style="width: 100%">
				<el-table-column
					type="index"
					label="序号"
					align='center'
					sortable
					width="70">
				</el-table-column>
				<el-table-column
					label="标签ID"
					align='center'
					width="120">
					<template slot-scope="scope">
						<span style="margin-left: 10px">{{ scope.row.lable_id }}</span>
					</template>
				</el-table-column>
				<el-table-column
					label="货物类型"
					align='center'
					width="80">
					<template slot-scope="scope">
						<span style="margin-left: 10px">{{ scope.row.goods_name }}</span>
					</template>
				</el-table-column>
				<el-table-column
					label="创建人"
					align='center'
					width="200">
					<template slot-scope="scope">
						<span style="margin-left: 10px">{{ scope.row.create_name }}</span>
					</template>
				</el-table-column>
				<el-table-column
					label="分配用户"
					align='center'
					width="180">
					<template slot-scope="scope">
						
						<span style="margin-left: 10px">{{ scope.row.username }}</span>
					</template>
				</el-table-column>
				<el-table-column
					label="创建时间"
					align='center'>
					<template slot-scope="scope">
						<span style="margin-left: 10px">{{ scope.row.create_time }}</span>
					</template>
				</el-table-column>
				<el-table-column
					label="更新时间"
					align='center'>
					<template slot-scope="scope">
						<span style="margin-left: 10px">{{ scope.row.update_time }}</span>
					</template>
				</el-table-column>
			</el-table>
		</div>
		<div class="page">
			<el-pagination
			  @size-change="handleSizeChange"
			  @current-change="handleCurrentChange"
			  :current-page="paginations.page_index"
			  :page-sizes="paginations.page_sizes"
			  :page-size="paginations.page_size"
			  :layout="paginations.layout"
			  :total="paginations.total">
			</el-pagination>
		</div>
	</div>
</template>
<script>
import{lablelist} from '../../api/api.js'
	export default {
		name:"list",
		data() {
			return {
				tableData:[], //数据
				paginations:{
					page_index:1, //当前页
					total:0, //总数
					page_size:5, //一页显示多少
					page_sizes:[5,10,15,20], //每页显示多少条
					layout:'total, sizes, prev, pager, next, jumper'
				},
				allTableData:[]
			}
		},
		methods: {
            lable_list() {
                var params = {
            };
            lablelist(params).then(res=>{
                console.log(res)
                const data = res;
                this.allTableData = data;
                this.setPaginations()
            })
            },
			setPaginations(){
				this.paginations.total = this.allTableData.length; //数据的数量
				this.paginations.page_index = 1; //默认显示第一页
				this.paginations.page_size = 5; //每页显示多少数据
				
				//显示数据
				this.tableData = this.allTableData.filter((item,index) => {
					return index < this.paginations.page_size;
				})
			},
			handleSizeChange(page_size) {
				this.paginations.page_index = 1; //第一页
				this.paginations.page_size = page_size; //每页先显示多少数据
				this.tableData = this.allTableData.filter((item,index) => {
					return index < page_size
				})
			},
			handleCurrentChange(page){
				// 跳转页数
				//获取当前页
				let index = this.paginations.page_size * (page -1);
				//获取总数
				let allData = this.paginations.page_size * page;
				
				let tablist=[];
				for(let i = index;i<allData;i++) {
					if (this.allTableData[i]) {
						tablist.push(this.allTableData[i])
					}
					this.tableData = tablist
				}
			}
		},
		created(){
			this.lable_list()
		}
	}
</script>

<style scoped>
	.list {
		margin: 20px;
	}
	.page {
		float: right;
		margin-top: 20px;
	}
</style>
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现表格分页,可以使用 ElementUI 提供的 el-pagination 组件,同时结合 el-table 组件使用。 1. 在 template 中添加 el-pagination 组件和 el-table 组件: ``` <template> <div> <el-pagination :current-page="currentPage" :page-size="pageSize" :total="total" @current-change="handleCurrentChange"> </el-pagination> <el-table :data="tableData" border> <el-table-column prop="name" label="姓名"></el-table-column> <el-table-column prop="age" label="年龄"></el-table-column> <el-table-column prop="address" label="地址"></el-table-column> </el-table> </div> </template> ``` 2. 在 data 中定义表格数据、分页相关数据以及每页显示的条目数: ``` <script> export default { data() { return { tableData: [], // 表格数据 currentPage: 1, // 当前页码 pageSize: 10, // 每页显示条目数 total: 0 // 总条目数 } }, methods: { handleCurrentChange(val) { this.currentPage = val; // 根据当前页码获取对应的数据 this.getData(); }, getData() { // 根据当前页码和每页显示条目数获取对应的数据 // 更新表格数据和总条目数 } } } </script> ``` 3. 在 mounted 中调用 getData 方法获取初始数据: ``` <script> export default { mounted() { this.getData(); }, methods: { // ... } } </script> ``` 4. 在 getData 方法中根据当前页码和每页显示条目数获取对应的数据,更新表格数据和总条目数: ``` <script> export default { methods: { async getData() { const res = await axios.get('/api/data', { params: { page: this.currentPage, size: this.pageSize } }); this.tableData = res.data.list; this.total = res.data.total; } } } </script> ``` 这样就可以实现基本的表格分页功能了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值