Fork me on GitHub

mint-ui上拉加载下拉刷新踩坑记

vue之mint-ui自行记录~~~

1、loadmore组件伪代码

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
<template>
<div class="page-loadmore-wrapper" ref="wrapper" :style="{ height: wrapperHeight }">
<mt-loadmore :top-method="loadTop"
@translate-change="translateChange"
@top-status-change="handleTopChange"
:bottom-method="loadBottom"
@bottom-status-change="handleBottomChange"
:bottom-all-loaded="allLoaded"
:autoFill="false"
ref="loadmore">
<slot></slot>
<div slot="top" class="mint-loadmore-top">
<span v-show="topStatus !== 'loading'" :class="{ 'is-rotate': topStatus === 'drop' }"></span>
<span v-show="topStatus === 'loading'">
<span v-show="topStatus === 'drop'">释放更新</span>
<mt-spinner type="snake"></mt-spinner>
</span>
</div>
<div slot="bottom" class="mint-loadmore-bottom">
<span v-show="bottomStatus !== 'loading'" :class="{ 'is-rotate': bottomStatus === 'drop' }"></span>
<span v-show="bottomStatus === 'loading'">
<mt-spinner type="snake"></mt-spinner>
</span>
</div>
</mt-loadmore>
</div>
</template>
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
<script type="text/babel">
export default {
name: "load-More",
data() {
return {
list: [],
allLoaded: false,
bottomStatus: '',
wrapperHeight: 0,
topStatus: '',
translate: 0,
moveTranslate: 0
};
},
props: {
height: ''
},
mounted() {
this.$nextTick(()=>{
this.wrapperHeight = this.height;
})
},
methods: {
handleBottomChange(status) {
this.bottomStatus = status;
},
//上拉加载更多
loadBottom() {
this.$emit("loadBottom");
},
handleTopChange(status) {
this.moveTranslate = 1;
this.topStatus = status;
},
onTopLoaded() {
this.$refs.loadmore.onTopLoaded();
},
onBottomLoaded() {
this.$refs.loadmore.onBottomLoaded();
},
setAllLoaded(val) {
this.allLoaded = val;
},
translateChange(translate) {
const translateNum = +translate;
this.translate = translateNum.toFixed(2);
this.moveTranslate = (1 + translateNum / 70).toFixed(2);
},
loadTop() {
this.$emit("loadTop");
},
},

};
</script>
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
<style lang="scss" scoped>
.loading-background, .mint-loadmore-top span {
-webkit-transition: .2s linear;
transition: .2s linear
}
.mint-loadmore-top span {
display: inline-block;
vertical-align: middle
}
.mint-loadmore-top span.is-rotate {
-webkit-transform: rotate(180deg);
transform: rotate(180deg)
}
.page-loadmore .mint-spinner {
display: inline-block;
vertical-align: middle
}
.page-loadmore-desc {
text-align: center;
color: #666;
padding-bottom: 5px
}
.page-loadmore-desc:last-of-type,
.page-loadmore-listitem {
border-bottom: 1px solid #eee
}
.page-loadmore-listitem {
height: .5rem;
line-height: .5rem;
text-align: center
}
.page-loadmore-listitem:first-child {
border-top: 1px solid #eee
}
.page-loadmore-wrapper {
overflow: scroll
}
.mint-loadmore-bottom span {
display: inline-block;
-webkit-transition: .2s linear;
transition: .2s linear;
vertical-align: middle
}
.mint-loadmore-bottom span.is-rotate {
-webkit-transform: rotate(180deg);
transform: rotate(180deg)
}
</style>

以上就是loadmore组件代码

2、使用loadmore组件

这里是一个页面有三个loadmore组件,每个组件的挂载ref分别取不同的名字,避免数据串
template部分

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
<template>
<div class="wraper">
<div class="header">
<div v-for="(item, index) in tabs" class="lis " @click='changeTab(index)'>
<div class="bottomBorder" :class="numIndex == index? 'active' : ''">
<span>{{ item }}</span>
</div>
</div>
</div>

<!-- 历史账单 -->
<div v-show="numIndex == 0" class="content">
<div class="contentHeader contentHeaderHistory" @click="date">
<div>{{year}}年{{month}}月</div>
<div class="tr"></div>
<div><span class="fl">已还:¥{{totalReturnAmount || '0.00'}}</span><span class="fr">待还:¥{{restAmount || '0.00'}}</span>
</div>
<div class="tr">{{repayDate || ''}}</div>
</div>
<mt-datetime-picker
ref="picker"
type="date"
v-model="value"
:startDate="startDate"
:endDate="endDate"
@confirm="handleChange">
</mt-datetime-picker>
<loadmore :height="height" @loadBottom="loadBottom0" @loadTop="loadTop0" ref="loadmoreHistory">
<div v-for="(item, index) in tab0.list" class="contentList">
<div>{{item.customerName}}</div>
<div class="tr">¥{{item.amount}}</div>
<div></div>
<div class="tr"><span>{{item.dueDate}}</span> <span class="periods">({{item.indexNum}}期)</span>
</div>
</div>
</loadmore>
</div>

<!-- 本期账单 -->
<div v-show="numIndex == 1" class="content">
<div class="contentHeader">
<div>应还金额</div>
<div class="tr">¥{{repayMoney1 || '0.00'}}</div>
<div></div>
<div class="tr">{{repayDate1 || ''}}</div>
</div>
<loadmore :height="height" @loadBottom="loadBottom1" @loadTop="loadTop1" ref="loadmoreCurrent">
<div v-for="(item, index) in tab1.list" class="contentList">
<div>{{item.customerName || '暂无'}}</div>
<div class="tr">¥{{item.amount}}</div>
<div></div>
<div class="tr"><span>{{item.dueDate}}</span> <span class="periods">({{item.indexNum}}期)</span>
</div>
</div>
</loadmore>
</div>

<!-- 未出账单 -->
<div v-show="numIndex == 2" class="content">
<div class="contentHeader">
<div>未出账单总额</div>
<div class="tr">¥{{repayMoney2 || '0.00'}}</div>
<div></div>
<div class="tr"></div>
</div>
<loadmore :height="height" @loadBottom="loadBottom2" @loadTop="loadTop2" ref="loadmoreNotOut">
<div v-for="(item, index) in tab2.list" class="contentList">
<div>{{ item.customerName }}</div>
<div class="tr">¥{{item.amount}}</div>
<div></div>
<div class="tr"><span>{{item.dueDate}}</span> <span class="periods">({{item.indexNum}}期)</span>
</div>
</div>
</loadmore>
</div>
</div>
</template>

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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<script>
export default {
name: "bill",
// components: {Loadmore},
data() {
return {
height: 'calc(100vh - 2.23rem)',
// 时间组件的data
year: new Date().getFullYear(),
month: (new Date().getMonth() + 1) > 10 ? (new Date().getMonth() + 1) : '0' + (new Date().getMonth() + 1),
startDate: new Date('2018'),
endDate: new Date(),
value: new Date(),
//tabs
tabs: ['历史账单', '本期账单', '未出账单'],
// 历史账单
repayMoney: '',
repayDate: '',
restAmount: '', //待还
totalReturnAmount: '', //已还
//本期账单
repayMoney1: '',
repayDate1: '',
//未出账单
repayMoney2: '',
numIndex: 0,
//列表
tab0: {
pageNum: 1,//页码
loader: true,
list: [],//数据
allLoaded: false,//数据是否加载完毕
bottomStatus: '',//底部上拉加载状态
wrapperHeight: 0,//容器高度
topStatus: '',//顶部下拉加载状态
translate: 0,//
moveTranslate: 0,
},
tab1: {
pageNum: 1,//页码
loader: true,
list: [],//数据
allLoaded: false,//数据是否加载完毕
bottomStatus: '',//底部上拉加载状态
wrapperHeight: 0,//容器高度
topStatus: '',//顶部下拉加载状态
translate: 0,//
moveTranslate: 0,
},
tab2: {
pageNum: 1,//页码
loader: true,
list: [],//数据
allLoaded: false,//数据是否加载完毕
bottomStatus: '',//底部上拉加载状态
wrapperHeight: 0,//容器高度
topStatus: '',//顶部下拉加载状态
translate: 0,//
moveTranslate: 0,
}
}
},
created() {
//历史账单
let historyParams = {
pageSize: 10,
currentPage: this.tab0.pageNum,
year: new Date().getFullYear(),
month: new Date().getMonth() + 1
}
this.getHistory(historyParams);

//本期账单
let currentParams = {
pageSize: 10,
currentPage: this.tab1.pageNum
}
this.getCurrent(currentParams);

//未出账单
let notOutBillParams = {
pageSize: 10,
currentPage: this.tab2.pageNum
}
this.getNotOut(notOutBillParams);
},
methods: {
changeTab(index) {
this.numIndex = index;
},
//调用时间插件
date() {
this.$refs.picker.open();
},
// 切换时间
handleChange(value) {
let y = new Date(value).getFullYear(),
m = new Date(value).getMonth() + 1;
this.year = y;
this.month = m < 10 ? '0' + m : m;
this.tab0.pageNum = 1;
this.tab0.loader = true;
let historyParams = {
pageSize: 10,
currentPage: this.tab0.pageNum,
year: y,
month: m
}
this.getHistory(historyParams);
},
//历史账单上拉加载
loadBottom0() {
this.$refs.loadmoreHistory.onBottomLoaded();
if (!this.tab0.loader) return;
let historyParams = {
pageSize: 10,
currentPage: this.tab0.pageNum,
year: this.year,
month: this.month
}
this.getHistory(historyParams);
},
//历史账单下拉刷新
loadTop0() {
let self = this;
this.tab0.loader = true;
this.tab0.pageNum = 1;
let historyParams = {
pageSize: 10,
currentPage: this.tab0.pageNum,
year: this.year,
month: this.month
}
self.$refs.loadmoreHistory.onTopLoaded();
this.getHistory(historyParams);
},
//本期账单上拉加载
loadBottom1() {
this.$refs.loadmoreCurrent.onBottomLoaded();
if (!this.tab1.loader) return;
let currentParams = {
pageSize: 10,
currentPage: this.tab1.pageNum,
}
this.getCurrent(currentParams);
},
//本期账单下拉刷新
loadTop1() {
this.tab1.loader = true;
this.tab1.pageNum = 1;
let currentParams = {
pageSize: 10,
currentPage: this.tab1.pageNum,
}
this.$refs.loadmoreCurrent.onTopLoaded();
this.getCurrent(currentParams);
},
//未出账单上拉加载
loadBottom2() {
this.$refs.loadmoreNotOut.onBottomLoaded();
if (!this.tab2.loader) return;
let currentParams = {
pageSize: 10,
currentPage: this.tab2.pageNum,
}
this.getNotOut(currentParams);
},
//未出账单下拉刷新
loadTop2() {
this.tab2.loader = true;
this.tab2.pageNum = 1;
let notOUutParams = {
pageSize: 10,
currentPage: this.tab2.pageNum,
}
this.$refs.loadmoreNotOut.onTopLoaded();
this.getNotOut(notOUutParams);
},
getHistory(params) {
let pageNum = this.tab0.pageNum;
if (!this.tab0.loader) return;
this.$http.getHistoryBill(params).then(res => {
console.log('historyParams', res);
if (res.result == 'success') {
let data = res.data,
restAmount = data.restAmount,
totalReturnAmount = data.totalReturnAmount,
lister = data.billDetailRecordVOList,
list = this.tab0.list,
l = lister.length;
this.restAmount = restAmount;
this.totalReturnAmount = totalReturnAmount;
if (pageNum > 1) {
list = [...list, ...lister];
} else {
list = lister;
}
this.tab0.list = list;
if (l < 10) {
this.tab0.loader = false;
} else {
this.tab0.pageNum = (pageNum + 1);
this.tab0.loader = true;
}
}
}).catch(err => {
console.log(err);
})
},
getCurrent(params) {
let pageNum = this.tab1.pageNum;
if (!this.tab1.loader) return;
this.$http.getCurrentBill(params).then(res => {
console.log('currentParams', res);
if (res.result == 'success') {
let data = res.data,
repayMoney1 = data.totalAmount,
repayDate1 = data.lastUpateTime,
lister = data.billDetailRecordVOList,
list = this.tab1.list,
l = lister.length;
this.repayMoney1 = repayMoney1;
this.repayDate1 = repayDate1;
if (pageNum > 1) {
list = [...list, ...lister];
} else {
list = lister;
}
this.tab1.list = list;
if (l < 10) {
this.tab1.loader = false;
} else {
this.tab1.pageNum = (pageNum + 1);
console.log(this.tab1.pageNum);
this.tab1.loader = true;
}
}
}).catch(err => {
console.log(err);
})
},
getNotOut(params) {
let pageNum = this.tab2.pageNum;
if (!this.tab2.loader) return;
this.$http.getNotOutBill(params).then(res => {
console.log('notOutBillParams', res);
if (res.result == 'success') {
let data = res.data,
repayMoney2 = data.totalAmount,
repayDate2 = data.lastUpateTime,
lister = data.billDetailRecordVOList,
list = this.tab2.list,
l = lister.length;
this.repayMoney2 = repayMoney2;
this.repayDate2 = repayDate2;
if (pageNum > 1) {
list = [...list, ...lister];
} else {
list = lister;
}
this.tab2.list = list;
if (l < 10) {
this.tab2.loader = false;
} else {
this.tab2.pageNum = (pageNum + 1);
console.log(this.tab2.pageNum);
this.tab2.loader = true;
}
}
}).catch(err => {
console.log(err);
})
},
},
}
</script>

css部分

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
<style lang="scss" scoped>
.wraper {
border-top: 1px solid #dfdfdf;
min-height: 100vh;
box-sizing: border-box;
background: #f6f6f6;
.header {
width: 100%;
height: .9rem;
display: flex;
background: #fff;
margin-bottom: .2rem;
.lis {
display: flex;
flex: 1;
text-align: center;
line-height: .9rem;
justify-content: center;
align-items: center;
.bottomBorder {
width: 1.32rem;
height: 100%;
box-sizing: border-box;
font-size: .3rem;
}
}
}
//主体内容
.content {
background: #fff;
padding: 0 .25rem;
.contentHeaderHistory {
background: url(../../../assets/imgs/icon-date.png) no-repeat right center;
background-size: .46rem .44rem;
}
.contentHeader {
display: flex;
flex-wrap: wrap;
width: 100%;
height: 1.1rem;
div {
width: 50%;
height: 50%;
box-sizing: border-box;
line-height: .55rem;
&:nth-child(2-n) {
font-size: .32rem;
}
&:nth-child(n+3) {
font-size: .24rem;
color: #b2b2b2;
}
span:nth-child(1) {
font-size: .2rem;
color: #b2b2b2;
}
span:nth-child(2) {
font-size: .2rem;
color: #000;
}
}
}
/deep/ .contentList {
display: flex;
flex-wrap: wrap;
width: 100%;
height: 1.34rem;
border-top: 1px solid #f6f6f6;
& > div {
width: 50%;
height: 50%;
box-sizing: border-box;
line-height: .67rem;
font-size: .24rem;
color: #8e8e8e;
&:nth-child(2) {
font-size: .28rem;
color: #000;
}
}
.periods {
color: #f58366;
}
}
}
}
//选中样式
.active {
border-bottom: 5px solid #fed42e;
}
.bottomNomore {
text-align: center;
font-size: .29rem;
color: #bebebe;
padding-top: .2rem;
}
</style>