vue antd design 无分页 长列表 虚拟滚动懒加载数据

<template>
  <div>
    <div style="width: 1000px">
      <a-table
        ref="tableRef"
        :scroll="{ y: 418 }"
        :data-source="sliceTable"
        :columns="columns"
        :rowKey="(row) => row.id"
        :row-selection="rowSelection"
        :pagination="false"
      >
      </a-table>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      startIndex: 0,
      selectedRows: [],
      vEle: undefined,
      columns: [
        {
          title: "姓名",
          dataIndex: "name",
          key: "name",
          width: 130,
        },
        {
          title: "年龄",
          dataIndex: "age",
          key: "age",
          width: 130,
        },
        {
          title: "住址",
          dataIndex: "address",
          key: "address",
          width: 130,
        },
      ],
    };
  },

  computed: {
    rowSelection() {
      return {
        onChange: (selectedRowKeys, selectedRows) => {
          console.log(
            `selectedRowKeys: ${selectedRowKeys}`,
            "selectedRows: ",
            selectedRows
          );
        },
        getCheckboxProps: (record) => ({
          props: {
            disabled: record.name === "Disabled User",
            name: record.name,
          },
        }),
      };
    },
    sliceTable() {
      return this.tableData.slice(this.startIndex, this.startIndex + 9);
    },
  },

  created() {
    this.vEle = document.createElement("div");
    this.loadData();
  },

  mounted() {
    this.$refs.tableRef.$el
      .querySelector(".ant-table-body")
      .addEventListener("scroll", this.tableScroll, {
        passive: true,
      });
  },

  methods: {
    loadData() {
      for (let i = 0; i < 100000; i++) {
        this.tableData.push({
          id: i,
          name: "zhangsan" + i,
          age: 12,
          address: "china",
        });
      }
      // for (let i = start_i; i < start_i + 20; i++) {
      //   this.tableData.push({
      //     id: i,
      //     name: "zhangsan" + i,
      //     age: 12,
      //     address: "china",
      //   });
      // }
      // this.$nextTick(() => {
      //   this.$refs.tableRef.$el.querySelector(
      //     ".ant-table-tbody"
      //   ).style.position = "absolute";
      //   this.vEle.style.height = this.tableData.length * 48 + "px";
      //   this.$refs.tableRef.$el
      //     .querySelector(".ant-table-body")
      //     .appendChild(this.vEle);
      // });
    },

    tableScroll() {
      let bodyWrapperEle =
        this.$refs.tableRef.$el.querySelector(".ant-table-body");
      let scrollTop = bodyWrapperEle.scrollTop;
      this.startIndex = Math.floor(scrollTop / 48);
      bodyWrapperEle.querySelector(
        ".ant-table-tbody"
      ).style.transform = `translateY(${this.startIndex * 48}px)`;
      if (
        bodyWrapperEle.scrollHeight <=
        scrollTop + bodyWrapperEle.clientHeight
      ) {
        this.loadData();
      }
    },
  },
};
</script>
<style lang="less" scoped>
</style>
Vue3 中,结合 Ant Design Vue (ant-design-vue) 实现分表格通常需要以下几个步骤: 1. 安装依赖:首先确保已经安装了 Vueantd-design-vue,可以使用 `npm` 或 `yarn` 来安装: ```bash npm install vue@^3.0 antd @ant-design/icons # 或者 yarn add vue@next antd @ant-design/icons ``` 2. 导入组件:在你的 `.vue` 文件中导入需要的组件,如 `Table`, `Pagination` 和 `Icon`: ```javascript import { Table, Pagination } from 'ant-design-vue'; import PlusOutlined from '@ant-design/icons/PlusOutlined'; ``` 3. 创建数据源:你可以创建一个虚拟的数据数组,模拟分效果: ```javascript const dataSource = Array.from({ length: 50 }, (_, index) => ({ key: `${index}`, name: `Name ${index + 1}`, age: `Age ${index + 1}`, address: `Address ${index + 1}`, })); ``` 4. 使用 `v-model` 绑定分插件,并监听它的变化事件来动态加载数据: ```html <template> <div> <a-icon :type="currentMode === 'prev' ? 'chevron-left' : 'chevron-right'" slot="prev" @click="changePage(-1)" /> <Pagination :total="total" :current="currentPage" @on-change="changePage" /> <Table :data="filteredData" :columns="columns" :pagination="pagination" /> <a-icon type="plus" @click="addRow" /> </div> </template> // ... data() { return { currentPage: 1, pageSize: 10, dataSource, total: dataSource.length, columns: [ // ...你的列配置 ], pagination: { pageSizes: [10, 20, 30], // 可选分大小选项 }, }; }, methods: { changePage(page) { this.currentPage = page; }, filteredData() { return this.dataSource.slice((this.currentPage - 1) * this.pageSize, this.currentPage * this.pageSize); }, addRow() { this.dataSource.push({ // ...添加新的行数据 }); this.total++; } } ``` 5. 根据需求定制表头、排序、搜索等功能,如果需要。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值