uni-app之實現分頁

挑戰者V發表於2020-06-19

一、下載庫

官方文件地址為:
https://ext.dcloud.net.cn/plugin?id=32

點選下載zip壓縮包即可,下載完畢後解壓到放置前端相關元件目錄,即components目錄。

二、使用

1.引入

import uniPagination from '../../components/uni-pagination/uni-pagination/uni-pagination.vue'

2.模板區域使用(在裡)

<uni-pagination @change="handlePage" show-icon="true" :total="postCount" :current="pageNum" :pageSize="pageSize"></uni-pagination>

3.編寫handlePage函式

handlePage(params){
            var pageIndex = params.current;
        
            console.log("this.pageNum:"+pageIndex);
        
            this.getPostListInfo(pageIndex)
        }

4.我的完整程式碼如下

<template>
<view class="content">
<!-- <image class="logo" src="/static/logo.png"></image> -->
<view class="text-area">
<text class="title">{{ title }}</text>
</view>
<view class="uni-list">
<view class="uni-list-cell" hover-class="uni-list-cell-hover" v-for="(item, index) in list" :key="index">
<!-- <view class="uni-list-cell-navigate uni-navigate-right">{{ item.Title }}</view> -->

<button type="default" @click="getDetail(item.Title, item.Id, item.Url)">{{ item.Title }}</button>
</view>
<uni-pagination @change="handlePage" show-icon="true" :total="postCount" :current="pageNum" :pageSize="pageSize"></uni-pagination>
</view>
</view>
</template>

<script>
import common from '../../common/common.js';
import uniPagination from '../../components/uni-pagination/uni-pagination/uni-pagination.vue';
export default {
components: { uniPagination },
data() {
return {
title: '文章列表',
postCount: 0,
pageSize: 10,
pageNum: 1,
list: []
};
},

onLoad() {
this.getPostListInfo(this.pageNum);

this.getPersonalBlogInfo();
},
methods: {
getPostListInfo(pageIndex) {
console.log('pageIndex:' + pageIndex);
uni.request({
url: common.website_url + '/cnblogs/getPersonalBlogPostList/' + pageIndex,
method: 'POST',
success: res => {
console.log('isSuccess');
console.log('getPostListInfo:' + JSON.stringify(res.data));
this.list = res.data;
},
fail: function(e) {
console.log('介面呼叫失敗:' + JSON.stringify(e));
}
});
},

getPersonalBlogInfo() {
uni.request({
url: common.website_url + '/cnblogs/getPersonalBlogInfo',
method: 'POST',
success: res => {
//console.log('getPersonalBlogInfo:' + JSON.stringify(res.data));
this.postCount = res.data.postCount;
this.pageSize = res.data.pageSize;
},
fail: function(e) {
console.log('介面呼叫失敗:' + JSON.stringify(e));
}
});
},
getDetail(Title, Id, Url) {
console.log('Title:' + Title + ' Id:' + Id);

uni.navigateTo({
url: '../post/post/post?Title=' + Title + '&Id=' + Id
});
},
handlePage(params) {
var pageIndex = params.current;

console.log('this.pageNum:' + pageIndex);

this.getPostListInfo(pageIndex);
}
}
};
</script>

<style>
.content {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}

.logo {
height: 200rpx;
width: 200rpx;
margin-top: 200rpx;
margin-left: auto;
margin-right: auto;
margin-bottom: 50rpx;
}

.text-area {
display: flex;
justify-content: center;
}

.title {
font-size: 36rpx;
color: #8f8f94;
}
</style>

5.效果圖

介面雖然不好看,但功能是Ok的。

當前頁

圖一

點選下一頁

圖二

相關文章