vue滚动加载列表

admin3年前vue385
new Vue({
   el: '#app',
   data: {
      error: '',
      loading: 1,
      back: 1,
      title: '',
      page: 1,
      currentItem: {},
      list: [],
      detail: {},
      currentPage: 1,
      totalPages: 0,
      result: 0,
      resultDes: ''
   },
   watch: {
      page(v) {
         switch(parseInt(v)) {
            case 1:
               document.title = title;
               this.list = [];
               this.currentPage = 1;
               this.totalPages = 0;
               break;
            case 2:
               document.title = '往期PK列表';
               break;
            case 3:
               document.title = 'PK详情';
               break;
         }
      }
   },
   mounted() {
      this.loading = 0;
      this.back = 0;
      if(this.page == 1) {
         this.getIndex();
      }else if(this.page == 2) {
         this.getList();
         this.$refs.list.addEventListener('scroll', this.lazyLoading);
      }
   },
   methods: {
      getIndex() {
         var t = this;
         $.ajax({
            type : 'post',
            url : indexUrl,
            data : {},
            dataType : 'json',
            success : function(res) {
                if (res.code == 1) {
                    t.currentItem = res.msg;
                }else{
                    t.error = res.msg;
                }
                t.loading = 0;
            },
            error: function(e) {
               t.loading = 0;
               layer.msg('网络错误,请重试');
            }
         });
      },
      vote(aid, val) {
         var t = this;
         aid = parseInt(aid);
         if(isNaN(aid)) {
            this.loading = 0;
            this.error = 'ID编号出错';
         }
         val = parseInt(val);
         if(isNaN(val)) {
            this.loading = 0;
            this.error = '请选择赞成或不赞成';
         }
         t.loading = 1;
         $.ajax({
            type : 'post',
            url : voteUrl,
            data : {
               aid: aid,
               val: val
            },
            dataType : 'json',
            success : function(res){
                if (res.code == 1) {
                  layer.msg('投票成功');
                  t.currentItem = res.msg;
                  t.currentItem.active = 1;
                }else{
                    t.error = res.msg;
                    t.back = 1;
                }
                t.loading = 0;
            },
            error: function(e) {
               t.loading = 0;
               layer.msg('网络错误,请重试');
            }
         });
      },
      getPage2() {
         var t = this;
         t.page = 2;
         t.getList();
         t.$nextTick(function() {
            t.$refs.list.addEventListener('scroll', t.lazyLoading);
         })
         
      },
      getList() {
         var t = this;
         t.loading = 1;
         $.ajax({
            type : 'post',
            url : listUrl,
            data : {p: t.currentPage},
            dataType : 'json',
            success : function(res){
                if (res.code == 1) {
                    var list = t.list;
                    var newlist = list.concat(res.msg.list);
                    t.list = newlist;
                    t.totalPages = res.msg.totalPages;
                    if (t.currentPage == t.totalPages) {
                       t.result = 1;
                       t.resultDes = '已全部加载';
                    }
                }else{
                    t.error = res.msg;
                }
                t.loading = 0;
            },
            error: function(e) {
               t.loading = 0;
               layer.msg('网络错误,请重试');
            }
         });
      },
      // 滚动加载
      lazyLoading (e) {
         var t = this;
         const scrollTop = e.target.scrollTop;
         const windowHeight = e.target.clientHeight;
         const scrollHeight = e.target.scrollHeight;
         // 滚动条到底部
         if (scrollTop + windowHeight === scrollHeight) {
            t.currentPage++;
            if (t.currentPage > t.totalPages) {
               return;
            }
            t.getList();
         }
      },
      showDetail(aid) {
         var t = this;
         aid = parseInt(aid);
         if(isNaN(aid)) {
            this.loading = 0;
            this.error = 'ID编号出错';
         }
         t.loading = 1;
         $.ajax({
            type : 'post',
            url : detailUrl,
            data : {aid: aid},
            dataType : 'json',
            success : function(res){
                if (res.code == 1) {
                    t.page = 3;
                    t.currentItem = res.msg;
                }else{
                    t.error = res.msg;
                    t.back = 1;
                }
                t.loading = 0;
            },
            error: function(e) {
               t.loading = 0;
               layer.msg('网络错误,请重试');
            }
         });
      }
   }
})
//往期PK列表
    public function list () {
        if(IS_AJAX) {
            $map['a.typeid'] = 1;
            $map['a.is_del'] = 0;
            $pagesize = 20;
            $count   = M('archives')->alias('a')->where($map)->count('aid');
            $totalPages = ceil($count/$pagesize);
            $pageObj = new Page($count, $pagesize);
            $list = M('archives')->field('a.aid, a.title')
                ->alias('a')
                ->where($map)
                ->order('a.sort_order,a.aid desc')
                ->limit($pageObj->firstRow . ',' . $pageObj->listRows)
                ->select();
            $this->success(['list' => $list, 'totalPages' => $totalPages ]);
        }      
    }


相关文章

vue2使用富文本编辑器

https://blog.csdn.net/m0_61257622/article/details/127331859...

vue输入框绑定回车事件

@keyup.enter.native=“...

el-date-picker禁用当天前的日期

<el-date-picker type="date" placeholder="请选择最早提货日期" v-model="addForm.pic...

vue2+thinkphp5表单提交

<style>             #app, #a...

在Vue中实现扫码枪读取条形码数据

https://blog.51cto.com/u_16145366/6397981...

vue elementui动态生成表格数据

<el-table :data="tableData" border class="mb30">       ...

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。