emis-sapp/components/tpx-scroll-view/tpx-scroll-view.vue

159 lines
3.0 KiB
Vue
Raw Normal View History

2023-12-06 18:53:44 +00:00
<template>
2024-05-31 10:42:15 +00:00
<scroll-view :scroll-y="true" @scrolltolower="onScrollBottom()" :style="customStyle">
2023-12-06 18:53:44 +00:00
<slot></slot>
2024-05-31 10:42:15 +00:00
<!-- 加载更多 -->
<u-loadmore v-if="showLoadMore" icon-size="28" font-size="28" margin-top="20" margin-bottom="20"
status="loading" />
<!-- 没有更多了 -->
<view v-if="showLoadedAll && loadedAll && resObject.list.length !== 0" class="pt-20 pb-20">
<u-loadmore icon-size="28" font-size="28"
status="nomore" />
</view>
2023-12-06 18:53:44 +00:00
<!-- 空数据页 -->
2024-05-31 10:42:15 +00:00
<view v-if="resObject.list.length == 0">
<tpx-empty :size="size"></tpx-empty>
2023-12-06 18:53:44 +00:00
</view>
</scroll-view>
</template>
<script>
2024-05-31 10:42:15 +00:00
import { debounce } from "@/common/util"
2023-12-06 18:53:44 +00:00
export default {
2024-05-31 10:42:15 +00:00
watch: {
searchParam: {
handler: function(cval, oval) {
this.getDataList(true)
},
deep: true
}
},
computed: {
customStyle() {
let stl = `height: 100%;`
if(this.maxHeight) {
stl += `max-height:${this.maxHeight}rpx;`
}
return stl
}
},
2023-12-06 18:53:44 +00:00
props: {
2024-05-31 10:42:15 +00:00
searchParam: {
default () {
return {}
}
},
showLoadedAll: {
default () {
return true
}
},
getListFun: {
2023-12-06 18:53:44 +00:00
default () {
2024-05-31 10:42:15 +00:00
return null
}
},
resObject: {
default () {
return {
list: []
}
}
},
size: {
default () {
return 'dfl'
}
},
maxHeight: {
default () {
return ''
2023-12-06 18:53:44 +00:00
}
}
},
data() {
return {
2024-05-31 10:42:15 +00:00
reqLoading: false,
page: { // 分页参数
pageNum: 1,
pageSize: 20,
},
showLoadMore: false, // 是否显示加载更多的loading
loadedAll: false, // 是否加载了所有数据
2023-12-06 18:53:44 +00:00
}
},
created() {
2024-05-31 10:42:15 +00:00
this.getDataList(true)
2023-12-06 18:53:44 +00:00
},
onHide() {
},
2024-01-30 03:09:49 +00:00
unmounted() {},
2024-05-31 10:42:15 +00:00
2023-12-06 18:53:44 +00:00
methods: {
2024-05-31 10:42:15 +00:00
async onScrollBottom() {
2023-12-06 18:53:44 +00:00
this.$emit('onScrollBottom')
2024-05-31 10:42:15 +00:00
if (this.loadedAll || this.showLoadMore) {
return
}
this.showLoadMore = true
this.page.pageNum++
2024-06-20 05:43:56 +00:00
try{
await this.getDataList()
}catch(e){
// 滚动下一页加载失败,重置 page.pageNum
this.page.pageNum --
}
2024-05-31 10:42:15 +00:00
this.showLoadMore = false
},
async getDataList(reload) {
if(!this.getListFun) {
return
}
if (reload) {
2024-07-10 05:33:52 +00:00
this.$emit('onListReload', this.searchParam)
2024-05-31 10:42:15 +00:00
this.page.pageNum = 1
this.showLoading()
}
this.reqLoading = true
const res = await this.getListFun({
...this.page,
...this.searchParam
})
this.reqLoading = false
let list = res.rows || []
if (reload) {
this.hideLoading()
} else {
list = this.resObject.list.concat(list)
}
this.loadedAll = res.total <= list.length
this.updateRes({
list,
total: res.total
})
},
updateRes(obj) {
const res = {
...this.resObject,
page: this.page,
showLoadMore: this.showLoadMore,
loadedAll: this.loadedAll,
reqLoading: this.reqLoading,
...obj,
}
this.$emit('update:resObject', res)
this.$emit('onListChange', res)
2023-12-06 18:53:44 +00:00
}
}
}
</script>
<style scoped lang="scss">
2024-05-31 10:42:15 +00:00
.sch-cont {
2023-12-06 18:53:44 +00:00
position: relative;
}
</style>