vue分页之el-pagination 用法

标签属性

<el-pagination
@size-change="handleSizeChange" 
@current-change="handleCurrentChange" 
background layout="total, sizes, prev, pager, next, jumper" 
:total="total" 
:page-size="10" 
:page-sizes="[10, 20, 30, 40, 50, 100]">
</el-pagination>
      


<script>
export default {
   data(){
     return{
       dialogFormVisible:false,
       tableData:[],
       total:0,  //总条数
       pSize:10  //默认10条
     }
   },
   methods:{
   getDataList(index,pageSize){  //获取数据列表
          this.$axios.get('接口地址',{params: {pageSize:pageSize,currentPage:index},
          headers:{accessToken:''}}).then(response=>{
           this.total=response.data.data.rowCount;
           if(response.data){
              this.tableData=response.data.data.data;
            }
          }).catch(err=>{
            alert(err);
          }) 
      }
      ,   
      handleSizeChange(val) {
        console.log('每页'+ val+' 条');
        this.pSize=val;
        this.getDataList(1,val);
      },
      handleCurrentChange(val) {
        console.log('当前页: '+val);
        this.getDataList(val,this.pSize);
      }
   },
   mounted(){
     this.getDataList(1,this.pSize);
   }
}
</script>

Vue.js中使用Element UI库的`el-pagination`组件进行分页查询时,如果你希望将第一页标记为0页而非默认的1页,可以稍微调整一下数据处理部分。假设你在后端API中获取的数据总页数和每页的数量不变,但在前端展示上,你可以这样操作: 1. 当获取到页面数据时,计算出实际的起始索引。如果当前页码是1,则起始索引为0;如果是其他页码,比如0页(即首页),起始索引则为前一页的结束位置加1。 ```javascript data() { return { currentPage: 1, // 初始页码,这里设置为1 totalItems: 0, // 总数据量 perPage: 10, // 每页数量 itemsPerPage: [], // 存储每页的数据 startIndex: null, } }, created() { this.fetchData(); }, methods: { fetchData(page = 1) { const start = page === 1 ? 0 : (page - 1) * this.perPage; // 计算起始索引 this.$axios.get('/api/data', { params: { start, limit: this.perPage } }) .then(response => { this.totalItems = response.data.total; this.itemsPerPage.push(...response.data.items); this.startIndex = start; }); }, prevPage() { if (this.currentPage > 1) { this.currentPage--; } }, nextPage() { if (this.currentPage < this.getTotalPages()) { this.currentPage++; } }, getTotalPages() { return Math.ceil(this.totalItems / this.perPage); }, } ``` 在这个例子中,`fetchData`方法会根据当前页码动态调整起始索引。同时,你需要在模板中显示页码时,传递`startIndex`而不是直接用`currentPage`: ```html <template> <div> <el-pagination :total="totalItems" :current-page.sync="currentPage" :page-size="perPage" @current-change="handleCurrentChange" layout="prev, pager, next" ></el-pagination> <ul> <li v-for="(item, index) in itemsPerPage" :key="index + startIndex">{{ item }}</li> </ul> </div> </template> <script> // ... methods: { handleCurrentChange(currentPage) { this.fetchData(currentPage); } } </script> ```
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值