微信小程序實現列表分頁功能

微信小程序列表分頁功能(未使用API),供大傢參考,具體內容如下

概述

主要實現功能:

1.列表展示
2.上下頁點擊

效果圖:

知識點:wx:for、bindtap、生命周期函數–監聽頁面加載、.filter、取餘( % )取整(parseInt(x/y) )函數

js

data: {
    frontPage: false,//上一頁 存在true,不存在false
    nextPage: false,//下一頁 存在true,不存在false
    pages: 0,//所有頁
    thisPages: 0,//當前頁
    rows: 6,//每頁條數
    total: 0,//總條數
    pageData: [],//本頁顯示的列表數據
    prizeListItem:[
      {name: "張三", pic: "../../images/1.png", gift:"小蛋糕"}, 
      {name: "李四", pic: "../../images/2.png", gift:"冰淇淋"}, 
      {name: "陳工", pic: "../../images/3.png", gift:"按摩椅"}, 
      {name: "孫悟空", pic: "../../images/3.png", gift:"桃子"}, 
      {name: "豬八戒", pic: "../../images/2.png", gift:"紅燒肉"}, 
      {name: "薩赫尚", pic: "../../images/1.png", gift:"新衣服"}, 
      {name: "程序員", pic: "../../images/2.png", gift:"電腦"}, 
      {name: "甄姬", pic: "../../images/3.png", gift:"口紅"},
      {name: "孫悟空", pic: "../../images/3.png", gift:"桃子"}, 
      {name: "豬八戒", pic: "../../images/2.png", gift:"紅燒肉"}, 
      {name: "薩赫尚", pic: "../../images/1.png", gift:"新衣服"}, 
      {name: "程序員", pic: "../../images/1.png", gift:"電腦"}, 
      {name: "甄姬", pic: "../../images/2.png", gift:"口紅"}
    ],
    myPrize: false,
    tab1: '',
    tab2: 'selected',
  },
/**
   * 生命周期函數--監聽頁面加載
   */
  onLoad: function () {
    this.setList();
  },
  // 初始化列表分頁
  setList() {
    let that = this;
    let thisPages = that.data.thisPages;
    let rows = that.data.rows;
    let prizeListItem = that.data.prizeListItem;
    let pageData = that.data.pageData;
    let pages = that.data.pages;
    if (pageData !== []){
      pageData = prizeListItem.filter(function (item, index, prizeListItem) {
        //元素值,元素的索引,原數組。
        return index >= rows*thisPages && index <= rows*(thisPages+1)-1;  //初始為0,0 < index < 6-1
      });
      let x = 0;
      let y = prizeListItem.length;
      if ( y%rows !== 0){
        x = 1
      };
      pages = parseInt(y/rows) + x; //所有頁
      thisPages = thisPages +1; //當前頁
      if ( pages > 1){
        that.setData({
          nextPage: true,
        })
      }
      that.setData({
        thisPages: thisPages,
        pageData: pageData,
        pages: pages,
        rows: rows,
      })
    }
  },
//點擊下一頁
  clickNext() {
    let that = this;
    let thisPages = that.data.thisPages;
    let prizeListItem = that.data.prizeListItem;
    let pageData = that.data.pageData;
    let pages = that.data.pages;
    let rows = that.data.rows;
    pageData = prizeListItem.filter(function (item, index, prizeListItem) {
      //元素值,元素的索引,原數組。
      return index >= rows*thisPages && index <= rows*(thisPages+1)-1;  
    });
    thisPages = thisPages + 1;
    if ( pages === thisPages){
      that.setData({
        nextPage: false,
      })
    }
    that.setData({
      thisPages: thisPages,
      pageData: pageData,
      frontPage: true,
    })
  },
//點擊上一頁
  clickFront() {
    let that = this;
    let thisPages = that.data.thisPages;
    let prizeListItem = that.data.prizeListItem;
    let pageData = that.data.pageData;
    let rows = that.data.rows;
    pageData = prizeListItem.filter(function (item, index, prizeListItem) {
      //元素值,元素的索引,原數組。
      return index >= rows*(thisPages-2) && index <= rows*(thisPages-1)-1;  
    });
    thisPages = thisPages - 1;
    if ( thisPages === 1){
      that.setData({
        frontPage: false,
      })
    }
    that.setData({
      thisPages: thisPages,
      pageData: pageData,
      nextPage: true,
    })
  },

wxml

<view class="prizelist">
      <view class="info_con">
        <view class="list" wx:for="{{pageData}}">
          <image class="list_bg" src="../../images/wi_listbg.png"></image>
          <view class="list_head">
            <image class="list_headpic" src="{{item.pic}}"></image>
            <view class="list_name">{{item.name}}</view>
          </view>
          <view class="list_prize">{{item.gift}}</view>
        </view>
      </view>   
      <view class="paging">
        <view class="page_btn">
          <view wx:if="{{frontPage}}" bindtap="clickFront">上一頁</view>
        </view>
        <view class="page_num">第{{thisPages}}頁 共{{pages}}頁</view>
        <view class="page_btn">
          <view wx:if="{{nextPage}}" bindtap="clickNext">下一頁</view>
        </view>
      </view>
    </view>

wxss

【外框

.con .prizelist{
  width: 100%;
  height: max-content;
  margin-top: 38rpx;
}
.con .prizelist .info_con{
  width: 639rpx;
  height: 787rpx;
  display: inline-block;
}

【list的樣式

.con .prizelist .info_con .list{
  width: 639rpx;
  height: 108rpx;
  position: relative;
  margin: 20rpx 0;
}
.list .wi_prize{
  width: 186rpx;
  height: 69rpx;
  margin: 20rpx 0 0 60rpx;
}
.list .prizeinfo{
  width: 350rpx;
  height: 108rpx;
  float: right;
}
.list .prizeinfo .prize_name{
  font-size: 28rpx;
  color: #87562e;
  line-height: 42rpx;
  margin-top: 20rpx;
}
.list .prizeinfo .prize_state{
  font-size: 20rpx;
  color: #ff2d2d;
  line-height: 25rpx;
}
.list .list_bg{
  width: 639rpx;
  height: 108rpx;
  position: absolute;
  left: 56rpx;
  z-index: -1;
}
.list .list_head{
  height: 100%;
  width: max-content;
  margin-left: 100rpx;
  float: left;
}
.list .list_head .list_headpic{
  border-radius: 50%;
  background-color: rgb(43, 93, 216);
  width: 46rpx;
  height: 46rpx;
  margin: 31rpx 0rpx;
  float: left;
}
.list .list_head .list_name{
  color: #000;
  line-height: 108rpx;
  font-size: 28rpx;
  float: left;
  margin-left: 31rpx;
}
.list .list_prize{
  height: 100%;
  line-height: 108rpx;
  font-size: 28rpx;
  color: #87562e;
  float: right;
}

【上下頁樣式

.con .prizelist .paging{
  width: 580rpx;
  height: 108rpx;
  margin: 30rpx auto;
}
.con .prizelist .paging .page_btn{
  width: 96rpx;
  height: 32rpx;
  font-size: 32rpx;
  font-family: "PingFangSC";
  color: #ffffff;
  line-height: 36rpx;
  float: left;
  margin: auto 23rpx;
}
.con .prizelist .page_num{
  font-size: 24rpx;
  font-family: "PingFangSC";
  color: #ffffff;
  line-height: 36rpx;
  float: left;
  margin: auto 75rpx;
}

結語

有一個可有可無的警告:

Now you can provide attr wx:key for a wx:for to improve performance.

解決辦法:添加wx:key屬性,

①使用循環的 array 中 item 的某個property,比如 wx:key=“item.id”
此時數組的格式應為:

 {id: "1", name: "張三", pic: "../../images/1.png", gift:"小蛋糕"}, 

②使用數組的下標,即 wx:key=“index”

③ wx:key="*this" 我沒太看懂,是

官方文檔說的有一個經歷過的低級錯誤:
錯誤:

onLoad: function () {
    setList();
  },

改正:

onLoad: function () {
    this.setList();
  },

(我都沒眼看瞭,馬虎或則腦子不清楚的錯誤總是次次能碰到——2021年3月9日)

後續

1.被指出 “第 X 頁 共 X 頁”的margin固定,當頁數增加到雙位數後,下一頁被擠到下一行瞭。
方法1(同事腦力成果,擔待瞭):修改class為page_name的margin為百分比。

.con .prizelist .page_num{
  margin: auto 75rpx;
}

改為:

.con .prizelist .page_num{
  margin: auto 10%;
}

方法2(我自己的老辦法):給“上一頁”“共 頁”“下一個”分別定義class:

<view class="paging">
        <view class="up_page" bindtap="up_page" >{{current_page > 1 ? '上一頁' : ''}}</view>
        <view class="down_page" bindtap="next_page">{{current_page < last_page ? '下一頁' : ''}}</view>
        <view class="page_num">第{{current_page}}頁 共{{last_page}}頁</view>
</view>

樣式:

.con .prizelist .paging{
  width: 100%;
  height: 108rpx;
  font-size: 32rpx;
  font-family: "PingFangSC";
  color: #ffffff;
  line-height: 36rpx;
  text-align: center;
}
.con .prizelist .paging .up_page{
  width: 96rpx;
  height: 32rpx;
  float: left;
  margin-left: 72rpx;
}
.con .prizelist .paging .down_page{
  width: 96rpx;
  height: 32rpx;
  float: right;
  margin-right: 72rpx;
}
.con .prizelist .page_num{
  width: 500rpx;
  font-size: 24rpx;
  font-family: "PingFangSC";
  color: #ffffff;
  line-height: 36rpx;
  margin: auto;
}

(時間:2021年3月22日)

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: