首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >vue.js3+element-plus+typescript table Pagination

vue.js3+element-plus+typescript table Pagination

作者头像
geovindu
发布2026-06-18 20:40:46
发布2026-06-18 20:40:46
270
举报
代码语言:javascript
复制
<!--
 *                        _oo0oo_
 *                       o8888888o
 *                       88" . "88
 *                       (| -_- |)
 *                       0\  =  /0
 *                     ___/`---'\___
 *                   .' \\|     |// '.
 *                  / \\|||  :  |||// \
 *                 / _||||| -:- |||||- \
 *                |   | \\\  - /// |   |
 *                | \_|  ''\---/''  |_/ |
 *                \  .-\__  '-'  ___/-. /
 *              ___'. .'  /--.--\  `. .'___
 *           ."" '<  `.___\_<|>_/___.' >' "".
 *          | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 *          \  \ `_.   \_ __\ /__ _/   .-` /  /
 *      =====`-.____`.___ \_____/___.-`___.-'=====
 *                        `=---='
 * 
 * 
 *      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * 
 *            佛祖保佑     永不宕机     永无BUG
 * 
 * @Author: geovindu
 * @Date: 2024-08-26 20:02:51
 * @LastEditors: geovindu
 * @LastEditTime: 2024-08-26 20:12:11
 * @FilePath: \vue\vuetest\src\components\tablepage.vue
 * @Description: geovindu
 * @lib,packpage: element plus
 * @pageing 分页
 * @IDE: vscode
 * @jslib: node 20 vue.js 3.0
 * @OS: windows10
 * @database: mysql 8.0  sql server 2019 postgreSQL 16
 * Copyright (c) geovindu 2024 by geovindu@163.com, All Rights Reserved. 
 -->
 <template>  
    <div class="Tutorial">  
    <ElTable :data="paginatedData" style="width: 100%">  
      <ElTableColumn type="index" width="50" />  
      <ElTableColumn prop="id" label="ID" width="180" />  
      <ElTableColumn prop="title" label="Title" width="180" />  
      <ElTableColumn prop="description" label="Description" />  
      <ElTableColumn prop="createdAt" label="Created At" />  
    </ElTable>  
      <div class="pagination-block">  
        <ElPagination  
          background  
          layout="prev, pager, next, jumper, total, sizes"  
          :current-page="state.page"  
          :page-size="state.limit"  
          :page-sizes="[10, 20, 30, 40]"  
          :total="total"  
          @current-change="handleCurrentChange"  
          @size-change="handleSizeChange"  
        />  
      </div>  
    </div>  
  </template>      
<script lang="ts" setup>  
import { defineComponent, reactive, ref, computed } from "vue";  
import TutorialDataService from "../services/TutorialDataService";  //和前的操作各数据库的程序关联,从数据库读的数据    
import { ElTableColumn,ElTable } from "element-plus";

  const tableData = ref([]);  
  const total = ref(0); // 响应式引用,用于存储总数据项数  
  //
  const state = reactive({  
    page: 1,  
    limit: 10,  
  });  

  // 加载数据  
  TutorialDataService.getAll().then(response => {  
    console.log("数据加载成功");  
    tableData.value = response.data;  
    total.value = response.data.length; // 更新总数据项数  
  }).catch(error => {  
    console.error("数据加载失败", error);  
  });  
    
  // 计算属性用于分页  
  const paginatedData = computed(() => {  
    const start = (state.page - 1) * state.limit;  
    const end = start + state.limit;  
    return tableData.value.slice(start, end);  
  });  
    
  // 改变页码  
  const handleCurrentChange = (e: number) => {  
    state.page = e;  
  };      
  // 改变页数限制  
  const handleSizeChange = (e: number) => {  
    state.limit = e;  
  };  
  </script>  
    
  <style scoped>  
  /* ... 您的样式 ... */  
  </style>

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-08-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档