【前端开发】JS+Vuew3请求列表数据并分页

应用技术:原生JavaScript + Vue3

javascript 复制代码
$(function () {
   ini();
});

function ini() {
  const { createApp, ref, onMounted } = Vue;
  createApp({
    setup() {
      const data = ref({
        studentList: [],
        page: 1,
        pageSize: 10,
      });

      const getStudentList = async (page, key) => {
        window.onscroll = function () {};
        var _data = data;

        try {
          var formData = {
            pageIndex: page,
            pageSize: _data.value.pageSize,
          };

          const response = await $.ajax({
            url: handlerURL,
            type: "post",
            data: formData,
            dataType: "json",
          });

          if (response.Status) {
            if (page == 1) {
              _data.value.studentList = response.Data.Data.Items;
            } else {
              _data.value.studentList = _data.value.studentList.concat(
                response.Data.Data.Items
              );
            }

            if (page < response.Data.Data.TotalPage) {
              LoadNextPage(page + 1);
            } else {
              LoadNextPage(0);
            }
          } else {
            weui.alert(response.Message);
          }
        } catch (error) {
          weui.alert("数据加载失败!" + error);
        }
      };

	  // 加载下一页
      const LoadNextPage = (nextPage) => {
        if (nextPage) {
          window.onscroll = function () {
            if (getScrollTop() + getClientHeight() + 10 >= getScrollHeight()) {
              getStudentList(nextPage, data.value.searchForm);
            }
          };
        } else {
          window.onscroll = function () {};
        }
      };
      
	  // 获取滚动条当前的位置
	  const  getScrollTop = () => {
		  var scrollTop = 0;
		  if (document.documentElement && document.documentElement.scrollTop) {
		    scrollTop = document.documentElement.scrollTop;
		  } else if (document.body) {
		    scrollTop = document.body.scrollTop;
		  }
		  return scrollTop;
	  }
	
	  // 获取当前可视范围的高度
	  const getClientHeight = () => {
		  var clientHeight = 0;
		  if (document.body.clientHeight && document.documentElement.clientHeight) {
		    clientHeight = Math.min(
		      document.body.clientHeight,
		      document.documentElement.clientHeight
		    );
		  } else {
		    clientHeight = Math.max(
		      document.body.clientHeight,
		      document.documentElement.clientHeight
		    );
		  }
		  return clientHeight;
	  }
	
	  // 获取文档完整的高度
	  const getScrollHeight = () => {
		  return Math.max(
		    document.body.scrollHeight,
		    document.documentElement.scrollHeight
		  );
	  }

      onMounted(() => {
        getStudentList();
      });

      return {
        data,
      };
    },
  })
    .use(vant)
    .mount("#app");
}
相关推荐
LaughingZhu1 小时前
Product Hunt 每日热榜 | 2026-05-21
前端·人工智能·经验分享·chatgpt·html
怕浪猫1 小时前
Electron 开发实战(一):从零入门核心基础与环境搭建
前端·electron·ai编程
小鹏linux2 小时前
Ubuntu 22.04 部署开源免费具有精美现代web页面的Casdoor账号管理系统
linux·前端·ubuntu·开源·堡垒机
前端若水3 小时前
会话管理:创建、切换、删除对话历史
前端·人工智能·python·react.js
Bigger3 小时前
mini-cc:一个轻量级 AI 编程助手的诞生
前端·ai编程·claude
涵涵(互关)3 小时前
Naive-ui树型选择器只显示根节点
前端·ui·vue
BY组态3 小时前
Ricon组态系统最佳实践:从零开始构建物联网监控平台
前端·物联网·iot·web组态·组态
BY组态3 小时前
Ricon组态系统vs传统组态软件:为什么选择新一代Web组态平台
前端·物联网·iot·web组态·组态
SoaringHeart3 小时前
Flutter进阶:OverlayEntry 插入图层管理器 NOverlayZIndexManager
前端·flutter
放下华子我只抽RuiKe54 小时前
React 从入门到生产(四):自定义 Hook
前端·javascript·人工智能·深度学习·react.js·自然语言处理·前端框架