前端:每天一个前端工作常见技巧前端如何根据数据进行手动分页(Vue3 + elementplus)...

995236d737df66299bac40d513ecb573.gif

前言

9cd1153d7c44b1c58b5eef6388313a37.gif

大家好 我是前端程序员歌谣 已经有很长一段时间没有更新技术文章了 十分
想念大家 想加入前端技术群的小伙伴速度加入

e550f4b4e8bf2375439c7160086be378.gif

案例

c88352d025407aca848127456aaf2ee8.gif

 在我们的日常需求中 我们需要进行手动进行页面的分页 针对市面上的Vue3 +element plus为例子 今天来说说如何实现

18f4c5ae7b845c3abcc8bcbd34abc069.png

b1332978919f51326a8c98b86e4980c8.gif

路由部分

c0a3cabe363ea66baf11afc1c2664fba.gif

children: [
      {
        path: '/index/user',
        component: () => import('../views/User/index.vue'),
        name: 'User',
        meta: {
          hidden: true,
        },
      }
    ]

866ef8f3c92454e0b028036628ff592b.gif

 实现效果

3a24e9f46ce246ce3a97bbe2a5ffd274.gif

b3223cdcf82055a9241886762e6c0633.png

2d57f36076e3e2b1f318530c2259b660.gif

代码逐步讲解

74205be7a4f67f48eef01d42b43cd797.gif

47aef4d2269e8d84753a09745e482b7e.gif

页面渲染部分 

db6c21d4100b76e06e68854c307a4c0c.gif

<template>
  <!-- element plus card -->
    <ElCard>
      <!-- element plus table -->
        <ElTable :data="tableList" style="width: 100%">
            <ElTableColumn :width="500" prop="username" label="Name" width="180" />
            <ElTableColumn :width="500" prop="password" label="Password" />
        </ElTable>
         <!-- element plus 分页 -->
        <ElPagination
      v-model:current-page="currentPage"
      :size="pageSize"
      :page-size="10"
      layout="total, prev, pager, next"
      :total="total"
      @size-change="onSizeChangeFn"
      @current-change="onPageChangeFn"
    />
    </ElCard>

af023d761bbbe8b74ba58caa5f42b0bb.gif

逻辑处理部分

43413020def482fd6f9ac9b047d15402.gif

import { ref } from "vue"
import { getDataList } from "./api/index.ts"
import { ElTable, ElTableColumn, ElCard,ElPagination} from "element-plus"
const tableList = ref([])
const currentPage= ref(1)
const pageSize= ref(10)
const allPageData= ref([])
const total= ref()
// 查询数据
const getList = async () => {
    let params = {
        pageIndex: currentPage.value,
        pageSize: pageSize.value,
    }
    let res = await getDataList(params)
    total.value=res.data.data.total
    tableList.value = res.data.data.list
}
// size改变
const onSizeChangeFn = (val) => {
  currentPage.value = 1
  pageSize.value = val
  getList()
}
// 分页改变
const onPageChangeFn = (val) => {
  currentPage.value = val
  getList()
 
}
getList()

9258642903bf22b15307c1f6153e9399.gif

数据格式

b13af0c474c0556fa60d78af21e4d587.gif

const List: {
  username: string
  password: string
}[] = [
  {
    username: 'admin',
    password: 'admin',
  },
  {
    username: 'test',
    password: 'test',
  },
  {
    username: 'geyao',
    password: 'geyao',
  },
  {
    username: 'lanlan',
    password: 'lanlan',
  },
  {
    username: 'junjun',
    password: 'junjun',
  },
  {
    username: 'taotao',
    password: 'taotao',
  },
  {
    username: 'huahua',
    password: 'huahua',
  },
  {
    username: 'gege',
    password: 'gege',
  },
  {
    username: 'meimei',
    password: 'meimei',
  },
  {
    username: 'haha',
    password: 'haha',
  },
  {
    username: 'dada',
    password: 'dada',
  },
  {
    username: 'xiexie',
    password: 'xiexie',
  },
  {
    username: 'gugu',
    password: 'gugu',
  }
]

33feb6a194daafb1e1475057079d743b.gif

总代码

e30f361797459a455e04e45365e4baf3.gif

import { SUCCESS_CODE } from '../../src/constants'
const timeout = 1000
const List: {
  username: string
  password: string
}[] = [
  {
    username: 'admin',
    password: 'admin',
  },
  {
    username: 'test',
    password: 'test',
  },
  {
    username: 'geyao',
    password: 'geyao',
  },
  {
    username: 'lanlan',
    password: 'lanlan',
  },
  {
    username: 'junjun',
    password: 'junjun',
  },
  {
    username: 'taotao',
    password: 'taotao',
  },
  {
    username: 'huahua',
    password: 'huahua',
  },
  {
    username: 'gege',
    password: 'gege',
  },
  {
    username: 'meimei',
    password: 'meimei',
  },
  {
    username: 'haha',
    password: 'haha',
  },
  {
    username: 'dada',
    password: 'dada',
  },
  {
    username: 'xiexie',
    password: 'xiexie',
  },
  {
    username: 'gugu',
    password: 'gugu',
  }
]
export default [
  // 列表接口
  {
    url: '/mock/user/list',
    method: 'get',
    response: ({ query }) => {
      const { username, pageIndex, pageSize } = query
      const mockList = List.filter((item) => {
        if (username && item.username.indexOf(username) < 0) return false
        return true
      })
      const pageList = mockList.filter(
        (_, index) => index < pageSize * pageIndex && index >= pageSize * (pageIndex - 1)
      )
      return {
        code: SUCCESS_CODE,
        data: {
          total: mockList.length,
          list: pageList
        }
      }
    }
  },
  // 登录接口
  {
    url: '/mock/user/login',
    method: 'post',
    timeout,
    response: ({ body }) => {
      const data = body
      let hasUser = false
      for (const user of List) {
        if (user.username === data.username && user.password === data.password) {
          hasUser = true
          return {
            code: SUCCESS_CODE,
            data: user
          }
        }
      }
      if (!hasUser) {
        return {
          code: 500,
          message: '账号或密码错误'
        }
      }
    }
  },
  // 退出接口
  {
    url: '/mock/user/loginOut',
    method: 'get',
    timeout,
    response: () => {
      return {
        code: SUCCESS_CODE,
        data: null
      }
    }
  }
]

96798bac7692cca4cb295e4c2d6fc81b.gif

仓库地址

0315e0d0ab73248598ae674ff9915fbb.gif

https://github.com/geyaoisnice/geyao_vuets_eleplus

905f06048d42cd4a4e3df2e58bf377f3.gif

总结

0bf1e0b5ade4500f8a5bb8cf62499cd1.gif

我是歌谣 以上就是关于Vue3 +element plus实现手动分页的逻辑 可以将整个
数组对象进行手动分页 想加入前端不闲聊交流群可私信我获取联系方式 氛围好
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值