vue antd长列表卡顿懒加载优化

该代码示例展示了如何在Vue.js应用中使用A-Table组件进行数据展示,实现表格滚动时动态加载更多数据和分页功能。通过监听滚动事件,计算新的数据范围并更新表格内容。
摘要由CSDN通过智能技术生成
<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>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为了实现vue antd卡片列表,您可以按照以下步骤进行操作: 1.首先,您需要安装Vue和Ant Design Vue。您可以使用以下命令进行安装: ```shell npm install vue ant-design-vue --save ``` 2.在您的Vue组件中,您需要导入所需的组件并在template中使用它们。以下是一个简单的示例: ```vue <template> <div> <a-card v-for="(item, index) in list" :key="index" @click="handleClick(index)"> <img :src="item.imgUrl" alt="图片"> <div>{{ item.title }}</div> <div>{{ item.description }}</div> </a-card> </div> </template> <script> import { Card } from 'ant-design-vue'; export default { components: { 'a-card': Card, }, data() { return { list: [ { imgUrl: 'https://example.com/image1.jpg', title: '标题1', description: '描述1', }, { imgUrl: 'https://example.com/image2.jpg', title: '标题2', description: '描述2', }, // ... ], activeIndex: -1, }; }, methods: { handleClick(index) { this.activeIndex = index; }, }, }; </script> ``` 3.在上面的示例中,我们使用了Ant Design Vue的Card组件来显示每个项目。我们使用v-for指令循环遍历列表中的每个项目,并使用@click指令将点击事件绑定到每个卡片上。当用户单击卡片时,我们将调用handleClick方法,并将当前卡片的索引作为参数传递给它。 4.在handleClick方法中,我们将当前卡片的索引存储在activeIndex变量中。我们可以使用这个变量来控制显示每张图片的详细信息。例如,我们可以在模板中添加以下代码: ```vue <div v-if="activeIndex === index"> <p>{{ item.details }}</p> </div> ``` 这将在用户单击卡片时显示当前项目的详细信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值