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

<script>
export default {
  data() {
    return {
      scrollPage: 1,
      treePageSize: 10,
      xnTable: [],
      tableData: [],
      startIndex: 0,
      selectedRows: [],
      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.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",
        });
      }
      debugger;
      this.xnTable = this.tableData.slice(this.startIndex, this.startIndex + 10);
    },

    tableScroll() {
      let target = this.$refs.tableRef.$el.querySelector(".ant-table-body");

      const scrollHeight = target.scrollHeight - target.scrollTop;
      const clientHeight = target.clientHeight;
      if (scrollHeight === 0 && clientHeight === 0) {
        this.scrollPage = 1;
      } else {
        if (scrollHeight < clientHeight + 5) {
          this.scrollPage = this.scrollPage + 1;
          const scrollPage = this.scrollPage;
          const treePageSize = this.treePageSize * (scrollPage || 1);
          const newData = [];
          let max = "";
          if (this.tableData.length > treePageSize) {
            max = treePageSize;
          } else {
            max = this.tableData.length;
          }
          this.tableData.forEach((item, index) => {
            if (index < max) {
              newData.push(item);
            }
          });
          this.xnTable = newData;
        }
      }

      // debugger;

      // 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.xnTable.push(
      //     this.tableData.slice(this.startIndex, this.startIndex + 9)
      //   );
      //   debugger
      //   // this.loadData();
      // }
    },
  },
};
</script>
<style lang="less" scoped>
</style>
GitHub 加速计划 / vu / vue
207.54 K
33.66 K
下载
vuejs/vue: 是一个用于构建用户界面的 JavaScript 框架,具有简洁的语法和丰富的组件库,可以用于开发单页面应用程序和多页面应用程序。
最近提交(Master分支:2 个月前 )
73486cb5 * chore: fix link broken Signed-off-by: snoppy <michaleli@foxmail.com> * Update packages/template-compiler/README.md [skip ci] --------- Signed-off-by: snoppy <michaleli@foxmail.com> Co-authored-by: Eduardo San Martin Morote <posva@users.noreply.github.com> 4 个月前
e428d891 Updated Browser Compatibility reference. The previous currently returns HTTP 404. 5 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐