vue-scroller

最近用vue写移动端开发,导致很多页面的展示需要用到下拉分页,所以将自己用到的知识点进行总结一下.

Quick Start

安装 vue-scorller

1
$ npm install vue-scroller -d

引用main.js

1
2
3
$ import VueScroller from 'vue-scroller'
$ import Vue from 'vue'
$ Vue.use(VueScroller)

HTML模板

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<template>
<v-layout row class="activity">
<v-flex xs12 sm12 md12>
<v-card>
<v-list two-line>
<scroller :on-refresh="refresh" :on-infinite="infinite" ref="scrolling">
<div v-for="(item, index) in items" class="row" :class="{'grey-bg': index % 2 == 0}">
<v-list-tile avatar :key="item.title" @click="toggle(item)">
<v-list-tile-avatar>
<img :src="item.logo">
</v-list-tile-avatar>
<v-list-tile-content>
<v-list-tile-title>{{ item.title }}</v-list-tile-title>
<v-list-tile-sub-title>{{item.address}}</v-list-tile-sub-title>
<v-list-tile-sub-title>{{item.startdate}} - {{item.enddate}}</v-list-tile-sub-title>
</v-list-tile-content>
</v-list-tile>
</div>
</scroller>
</v-list>
</v-card>
</v-flex>
</v-layout>
</template>
  • on-refresh 是一开始加载的时候下拉刷新的时候请求数据
  • on-infinite 是每当向上滑动的时候去请求数据

JS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<script>
import crypto from "crypto";

export default {
data: function () {
return {
items: [],
province: '',
city: '',
src: 'inveno',
sge: '2017-12-12',
sle: '2018-05-08',
pi: 1,
ps: 10,
count: 0,
noData: '',
pagecount: 0
}
},

mounted() {
let self = this;

self.noData = '';
let pagination = {
pi: self.pi,
ps: self.ps
};
self.initialize(pagination);
self.top = 1;
self.bottom = 10;
},

created () {
let self = this;
self.items = [];
self.$store.commit('changeTitle', '查活动');
},

methods: {
queryActivity(params) {
let self = this;

return new Promise((resolve, reject) => {
self.$request.post(self.$store.state.queryActivity, params)
.then((response) => {
if (response.status == '200' && response.data.code == '200') {
resolve(response.data.content);
} else {
reject(response.data.msg)
}
}).catch((error) => {
reject(error);
});
});
},

initialize(pagination) {
let self = this;
let params = {
src: 'inveno',
province: '北京',
city: '朝阳',
sge: '2017-12-12',
sle: '2018-05-08'
};
let optionList = [];

self.queryActivity(Object.assign(params, pagination))
.then((result) => {
let data = result.list;
let list = [];

data.forEach((item) => {
let startdate = self.$utils.formatDate(new Date(item.startdate), 'yyyy-MM-dd hh:mm:ss');
self.$set(item, 'startdate', startdate);

let enddate = self.$utils.formatDate(new Date(item.enddate), 'yyyy-MM-dd hh:mm:ss');
self.$set(item, 'enddate', enddate);

list.push(item);
});
self.items = self.items.concat(list);
self.pagecount = result.pagecount;
self.count = Math.ceil(result.pagecount / self.ps);
}).catch(error => {
let info = {
alertInfo: error ? error.toString() : '',
color: 'error'
};
self.$Snackbar(info);
});
},

refresh(done) {
let self = this;

setTimeout(function () {
done(true);
console.log('refresh')
}, 1500);
},

infinite(done) {
let self = this;

if(this.noData) {
setTimeout(()=>{
this.$refs.scrolling.finishInfinite(2);
});
return false;
}

if(Math.ceil(self.bottom / self.ps) > self.count) {
setTimeout(() => {
done(true)
}, 1500)
return false;
}

setTimeout(function () {
var start = self.bottom + 1;
let pagination = {
pi: start,
ps: 10
};
self.initialize(pagination);

self.bottom = self.bottom + 10;
setTimeout(() => {
if(start > self.pagecount - self.ps) {
self.noData = "没有更多数据";
}
self.$refs.scrolling.resize();
done();
});
}, 1500);
},

toggle(item) {
let self = this;

if(item.url && !!item.url) {
window.open(item.url);
}
}
}
}
</script>

CSS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<style lang="scss" scoped>
@import '../../assets/css/color.scss';

.activity {
.row {
width: 100%;
height: 90px;
padding: 10px 0;
font-size: 16px;
line-height: 30px;
text-align: center;
color: #444;
background-color: #fff;
}

.grey-bg {
background-color: #eee;
}
}
</style>

More info:

坚持原创技术分享,您的支持将鼓励我继续创作!