Vue Ant Design Vue table pagination - 表格分页 - 附完整示例

目录

效果

一、介绍

1、官方文档:Ant Design Vue — An enterprise-class UI components based on Ant Design and Vue.js

2、官方示例

二、准备工作

1、安装依赖包

2、示例版本

三、使用步骤

1、在main.ts引入

2、具体使用

四、完整示例

1、main.ts

2、example.vue

 欢迎扫描下方二维码关注VX公众号


效果

一、介绍

1、官方文档:Ant Design Vue — An enterprise-class UI components based on Ant Design and Vue.js

Ant Design Vue — An enterprise-class UI components based on Ant Design and Vue.jsAn enterprise-class UI components based on Ant Design and Vueicon-default.png?t=N7T8https://3x.antdv.com/components/pagination-cn/

2、官方示例

二、准备工作

1、安装依赖包

# 选择一个你喜欢的包管理器

# NPM
$npm install ant-design-vue --save

#Yarn
$ yarn add ant-design-vue

注:也可以在浏览器中使用 script 和 link 标签直接引入文件,并使用全局变量 antd

Ant Design Vue — An enterprise-class UI components based on Ant Design and Vue.js

2、示例版本

"ant-design-vue": "^3.2.12",

三、使用步骤

1、在main.ts引入

import Antd from 'ant-design-vue'
import 'ant-design-vue/dist/antd.css'

app.use(Antd);

2、具体使用

  const page = ref(1);
  const pageSize = ref(2);
  const totals = ref(0);
  const paginationProp = ref({
    showSizeChanger: true, // 是否展示 pageSize 切换器,当 total 大于 50 时默认为 true
    showQuickJumper: true, // 是否可以快速跳转至某页
    pageSize, // 每页条数
    current: page, // 当前页数
    total: totals, // 数据总数
    showTotal: (totals) => `总 ${totals} 条数据`, // 用于显示数据总量和当前数据顺序
    onChange: pageChange, // 页码或 pageSize 改变的回调,参数是改变后的页码及每页条数
    onShowSizeChange: pageSizeChange, // pageSize 变化的回调
    pageSizeOptions: ['2', '3'], // 指定每页可以显示多少条
  });

四、完整示例

1、main.ts

import { createApp } from 'vue';
import App from './App.vue';
import Antd from 'ant-design-vue'
import 'ant-design-vue/dist/antd.css'

const app = createApp(App);
app.use(Antd);
app.mount("#app");

2、example.vue

<template>
  <a-table
    :columns="columns"
    :data-source="tableData"
    bordered
    size="small"
    :pagination="paginationProp" />
</template>

<script lang="ts" setup>
  import { ref } from 'vue';
  const columns = [
    {
      title: '姓名',
      dataIndex: 'name',
      ellipsis: true,
    },
    {
      title: '身高',
      dataIndex: 'height', 
      ellipsis: true,
    },
    {
      title: '体重',
      dataIndex: 'weight',
      ellipsis: true,
    },
  ];
  const tableData = ref<any>([
    {
      key: '1',
      name: '张三',
      height: 180,
      weight: 165,
    },
    {
      key: '2',
      name: '李四',
      height: 178,
      weight: 146,
    },
    {
      key: '3',
      name: '王五',
      height: 182,
      weight: 168,
    },
    {
      key: '4',
      name: '赵六',
      height: 185,
      weight: 160,
    },
  ]);

  // 分页相关
  const page = ref(1);
  const pageSize = ref(2);
  const totals = ref(0);
  const paginationProp = ref({
    showSizeChanger: true, // 是否展示 pageSize 切换器,当 total 大于 50 时默认为 true
    showQuickJumper: true, // 是否可以快速跳转至某页
    pageSize, // 每页条数
    current: page, // 当前页数
    total: totals, // 数据总数
    showTotal: (totals) => `总 ${totals} 条数据`, // 用于显示数据总量和当前数据顺序
    onChange: pageChange, // 页码或 pageSize 改变的回调,参数是改变后的页码及每页条数
    onShowSizeChange: pageSizeChange, // pageSize 变化的回调
    pageSizeOptions: ['2', '3'], // 指定每页可以显示多少条
  });

  function pageChange(p, pz) {
    page.value = p;
    pageSize.value = pz;
    getDataList();
  }
  function pageSizeChange(current, size) {
    page.value = current;
    pageSize.value = size;
    getDataList();
  }

  function getDataList() {
    // 获取表格数据的接口地址({pageNo: page.value, pageSize: pageSize.value }).then((data) => {
    //   const { current, pages, size, total, records } = data
    //   if(current > pages) {
    //     page.value = pages;
    //     getDataList();
    //   } else {
    //     tableData.value = records;
    //     pageSize.value = size;
    //     totals.value = total;
    //   }      
    // })
  }

  // getDataList();

</script>

<style scoped>
  :deep() .ant-table-cell {
    text-align: center !important;
  }
  
  :deep() .ant-table-pagination.ant-pagination {
    margin: 10px 0 0 0 !important;
  }
</style>

Ant Design Vue — An enterprise-class UI components based on Ant Design and Vue.js

 欢迎扫描下方二维码关注VX公众号

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Vue中使用Ant Design TablePagination组件,你可以按照以下步骤操作: 1. 安装Ant Design VueVue Router: ``` npm install ant-design-vue vue-router --save ``` 2. 在你的Vue应用程序中导入Ant Design VueVue Router: ```javascript import Vue from 'vue' import Antd from 'ant-design-vue' import 'ant-design-vue/dist/antd.css' import VueRouter from 'vue-router' Vue.use(Antd) Vue.use(VueRouter) ``` 3. 创建一个包含TablePagination组件的Vue组件: ```javascript <template> <div> <a-table :columns="columns" :data-source="dataSource" :pagination="false"></a-table> <a-pagination :current="currentPage" :total="total" :page-size="pageSize" @change="onPageChange"></a-pagination> </div> </template> <script> export default { data() { return { columns: [ { title: '姓名', dataIndex: 'name', key: 'name', }, { title: '年龄', dataIndex: 'age', key: 'age', }, { title: '地址', dataIndex: 'address', key: 'address', }, ], dataSource: [ { key: '1', name: 'John Brown', age: 32, address: 'New York No. 1 Lake Park', }, { key: '2', name: 'Jim Green', age: 42, address: 'London No. 1 Lake Park', }, { key: '3', name: 'Joe Black', age: 32, address: 'Sidney No. 1 Lake Park', }, ], currentPage: 1, pageSize: 10, total: 3, } }, methods: { onPageChange(page) { this.currentPage = page }, }, } </script> ``` 4. 在Vue Router中定义路由: ```javascript import Vue from 'vue' import VueRouter from 'vue-router' import TablePage from './TablePage.vue' Vue.use(VueRouter) const routes = [ { path: '/table', component: TablePage }, ] const router = new VueRouter({ routes, }) export default router ``` 5. 在应用程序中使用组件: ```javascript <template> <div> <a-menu :default-selected-keys="['1']"> <a-menu-item key="1"> <router-link to="/table">表格</router-link> </a-menu-item> </a-menu> <router-view></router-view> </div> </template> <script> export default { name: 'App', } </script> ``` 这样就可以在Vue应用程序中使用Ant Design TablePagination组件了。在上面的示例中,我们定义了一个包含表格分页器的组件,然后使用Vue Router将其添加到应用程序中。在菜单中点击“表格”链接,就可以展示TablePagination组件了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值