1. 程式人生 > >微信小程式選項卡、列表動態顯示按鈕

微信小程式選項卡、列表動態顯示按鈕

小程式選項卡功能實現


wxml頁面:

先給選項卡賦值data-current=“待確認” 新增點選事件bindtap=“clickTab”, 在js裡clickTab獲取選項卡的值,e.target.dataset.current 通過that.setData賦值給currentTab,在wxml 選項卡class裡進行判斷{{currentTab==‘待確認’?‘active’:’’}}, 通過 js 呼叫介面賦值來判斷選項卡列表狀態 。
因為swiper高度是固定的,所以用到style=‘height:{{scrollHeight}}px’ bindchange=“swiperTab”
,在js onLoad方法裡通過this.setData 設定高scrollHeight: wx.getSystemInfoSync().windowHeight - (wx.getSystemInfoSync().windowWidth / 750 * 100)
<view class="swiper-tab">
    <view class="swiper-tab-item {{currentTab=='待確認'?'active':''}}" data-current="待確認" bindtap="clickTab">待確認   </view>
    <view class="swiper-tab-item {{currentTab=='運輸中'?'active':''}}" data-current="運輸中" bindtap="clickTab">運輸中         </view>
    <view class="swiper-tab-item {{currentTab=='待結算'?'active':''}}" data-current="待結算" bindtap="clickTab">待結算          </view>
    <view class="swiper-tab-item {{currentTab=='已完成'?'active':''}}" data-current="已完成" bindtap="clickTab">已完成          </view>
    <view class="swiper-tab-item {{currentTab=='已取消'?'active':''}}" data-current="已取消" bindtap="clickTab">已取消         </view>
  </view>
    <swiper current='{{currentTab}}' style='height:{{scrollHeight}}px' duration="300" bindchange="swiperTab">

    <!-- 待確認 -->
    //在這裡我設定了禁止滑動(catchtouchmove='stopTouchMove')
      <swiper-item catchtouchmove='stopTouchMove'><scroll-view  scroll-y style='height:{{scrollHeight}}px' ><view class="main" wx:for='{{myReceive}}' wx:key="item">
      <view data-order-no="{{item.orderNo}}"  bindtap="clickOrder">
        <view class="main-item">
          <view class="flex-row th">發單人:</view> 
          <view class="flex-row td2">{{item.publisherName}}</view> 
          <view class="flex-row th1">箱型:</view> 
          <view class="flex-row td1" style="color:red">{{item.boxType}}</view> 
        </view>
        <view class="main-item">
          <view class="flex-row th">業務型別:</view> 
          <view class="flex-row td">{{item.businessType}}</view> 
          <view class="flex-row th1">箱重:</view> 
          <view class="flex-row td1">{{item.weight}}</view> 
        </view>
        <view class="main-item">
          <view class="flex-row th">到廠時間:</view> 
          <view class="flex-row td">{{item.limitedAt}}</view> 
          <view class="flex-row th1">港區:</view> 
          <view class="flex-row td1">{{item.harbour}}</view> 
        </view>
        <view class="main-item">
          <view class="flex-row th">門點地址:</view>
          <view class="flex-row td">{{item.store}}</view>
          <view class="flex-row th1">提箱:</view>
          <view class="flex-row td1">{{item.fetchAddress}}</view>
        </view>
        <view class="main-item">
          <view class="flex-row th">備註:</view>
          <view class="flex-row td" style="left:140rpx">{{item.state}}</view>
          <view class="flex-row th1">運費:</view>
          <view class="flex-row td1" style="color:red">{{item.freight}}</view>
        </view>    </view>
        <view class='main_solid'></view>
        <view class="main_button">
            <button>{{item.payType== 'online' ? '平臺墊付' : '線下結算'}}</button>
            <button>{{item.cargoType=="import"?'進口':'出口'}}</button>
            <button>不含提箱費</button>
        </view>
        <view class='button1'><button wx:if="{{currentTab =='待確認'}}">待確認</button>
        <button style='background-color: #43cf7c;' wx:elif="{{currentTab =='運輸中'&& item.sealBoxNo==null}}" data-order-no="{{item.orderNo}}" bindtap='clickSeal'>報箱封號</button>
         <button style='background-color: #43cf7c;' wx:elif="{{currentTab =='運輸中'&&item.receiverPrepay==null}}" data-order-no="{{item.orderNo}}" bindtap='clickPrepay'>墊款</button>
         <button style='background-color: #43cf7c;' wx:elif="{{currentTab =='運輸中'&&item.signImg==null}}" data-order-no="{{item.orderNo}}" bindtap='clickReceipt'>回單</button>
         <button style='background-color: #43cf7c;' wx:elif="{{currentTab =='運輸中'&&item.signImg!==null&&item.transitCompleteFlg !==1}}" data-order-no="{{item.orderNo}}" bindtap='completed' >已完成</button>
          <button class='completed' wx:elif="{{currentTab =='運輸中'&&item.transitCompleteFlg==1}}" data-order-no="{{item.orderNo}}" >已完成</button>
         <button style='background-color: #43cf7c;' wx:elif="{{currentTab =='待結算'&&item.withdrawState===null}}" data-order-no="{{item.orderNo}}" bindtap='requestWithdraw'>待結算</button><button wx:elif="{{currentTab =='待結算'&&item.withdrawState=='待稽核'}}">待稽核</button>
         </view>
        <view class="img" data-phone="{{item.publisherPhone}}"  bindtap='phoneCall'><image style='width:48rpx;height:48rpx' src='../../images/phone.png'></image></view>
      </view>
      <view wx:if="{{pageEnd==true}}" class='scrollEnd'>已顯示所有資料...</view>
      </scroll-view>
    </swiper-item>
    </swiper>

wxss頁面:

/* 選項卡樣式 */
.swiper-tab{
  width: 99%;
  border: 1rpx solid #01C2AB;
  border-radius: 8rpx;
  text-align: center;
  height: 58rpx;
  line-height: 58rpx;
  font-weight: bold;
  background-color: #ffffff;
  margin-top: 10rpx;
}
.swiper-tab-item{
  display: table-cell;
  width: 10%;
  font-size: 29.16rpx;
  text-align: center;
  color:#01C2AB;
}
.swiper-tab-item:not(:last-child){border-right: 4rpx solid #01C2AB;}
.swiper-tab-item.active{
  background-color: #01C2AB;
  color: white;
}
/* 列表資料樣式 */
.main{
  position: relative;
  width:92%;
	height: 320rpx;
	top: 10rpx;
  padding: 20rpx;
	color: rgba(80, 80, 80, 1);
	background-color: rgba(255, 255, 255, 1);
	font-size: 28.08rpx; 
  justify-content: center;
  align-items: stretch;
  margin-top: 10rpx;
  margin: 0px auto;
  overflow: hidden;
  transition: left 0.2s ease-in-out;
  white-space:nowrap;
  text-overflow:ellipsis;
  z-index: 5;
}
/* 訂單內容樣式 */
.main-item{
  width: 750rpx;
  height: 55.52rpx;
}
.th{
  text-align: left;
  margin-right: 40rpx;
  position: absolute;
  left:25rpx;
}
.td{
  position: absolute;
  left:160rpx;
}
.td2{
  position: absolute;
  left:160rpx;
  overflow:hidden; 
  text-overflow:ellipsis;
  white-space:nowrap;  
  width: 200rpx;
}
.th1{
  position: absolute;
  right:29%;
}
.td1{
   position: absolute;
   left:540rpx;
}
.main_solid{
  position: absolute;
  width:100%;
	height: 2.34rpx;
	top: 290rpx;
  right: 2rpx;
	color: #999999;
	background-color:#999999;
	line-height: 150%;
	text-align: center;
}
.main_button{
  width: 854.1rpx;
	height: 40rpx;
  display: flex;
  flex-direction: row;
  margin-top: 15rpx;
  margin-left: 2rpx;
}
/* 按鈕樣式 */
.main_button button{
  height: 41rpx;
  font-size: 28.08rpx;
  padding: 4rpx;
  line-height: 86%;
  color: #ff5733;
  border:2rpx solid #ff5733;
  background-color:white;
  border-radius: 0px;
  margin: 5rpx;
  }
.main_button button::after {
  border: 0px
}
.button1,.button2{
  width: 200rpx;
  position: relative;
  top: -38rpx;
  right: -480rpx;
	height: 42rpx;
  display: flex;
}
.button1 button,.button2 button{
  font-size: 28.08rpx;
  line-height: 150%;
  border-radius: 20rpx;
}
.button1 button{
  background-color: rgb(238, 241, 239);
}
.button2 button{
  background-color: #01C2AB;
}
/* 電話圖示 */
.img{
  width: 42rpx;
  position: relative;
  top: -370rpx;
  right: -350rpx;
}
.scrollEnd{
  margin: 0px auto;
  font-size: 28rpx;
  color: #999999;
  margin-top: 20rpx;
  text-align: center
}

js頁面:

var tool = require('../../utils/util.js'); //引入的js
var api = require('../../utils/api.js');
var baseUrl = '../../';
Page({
  /**
   * 頁面的初始資料
   */
  data: {
    currentTab: '待確認', 
    isactive: true,
    scrollHeight: 0,
    myReceive:[],
    size: 10,
    pageEnd: false
  },
  

  /**
   * 生命週期函式--監聽頁面載入
   */
  onLoad: function (options) {
    var that = this;
    var myReceive ={
      state: '待確認', 
      page: '1',
      size: '10'
    }
    //呼叫介面
   api.getRequest(api.apiUrl.ORDERS_ACCEPTED_GET,myReceive,function(res){
      var datas = res.data.data.items;
      for (let i = 0; i < datas.length; i++) {
        datas[i]["limitedAt"] = tool.toDate(datas[i]["limitedAt"])
        datas[i]["createdAt"] = tool.getDateDiff(datas[i]["createdAt"])
      }
      that.setData({
        myReceive: datas
      })
    })
  },

  /**
   * 生命週期函式--監聽頁面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命週期函式--監聽頁面顯示
   */
  onShow: function () {
    this.setData({
      scrollHeight: wx.getSystemInfoSync().windowHeight - (wx.getSystemInfoSync().windowWidth / 750 * 100),
    })
    var that = this;
    var myReceive = {
      state: this.data.currentTab, //選項卡所選值
      page: '1',
      size: '10'
    }
    api.getRequest(api.apiUrl.ORDERS_ACCEPTED_GET, myReceive, function (res) {
      var datas = res.data.data.items;
      for (let i = 0; i < datas.length; i++) {
        datas[i]["limitedAt"] = tool.toDate(datas[i]["limitedAt"])
        datas[i]["createdAt"] = tool.getDateDiff(datas[i]["createdAt"])
      }
      that.setData({
        myReceive: datas
      })
    })
  },

  /**
   * 生命週期函式--監聽頁面隱藏
   */
  onHide: function () {
  },

  /**
   * 生命週期函式--監聽頁面解除安裝
   */
  onUnload: function () {

  },

  /**
   * 頁面相關事件處理函式--監聽使用者下拉動作
   */
  onPullDownRefresh: function () {
  },

  /**
   * 頁面上拉觸底事件的處理函式
   */
  onReachBottom: function () {
  },

  /**
   * 使用者點選右上角分享
   */
  onShareAppMessage: function () {

  },
  //成功 
  showToast1() {
    wx.showToast({
      title: '提交成功',
      icon: 'success',
      duration: 4000
    })
  },
  //選項卡點選切換
  clickTab: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
    console.log("ffffffffffffffff" + this.data.currentTab)
    var that = this;
    var myReceive = {
      state: this.data.currentTab,
      page: '1',
      size: '10'
    }
    api.getRequest(api.apiUrl.ORDERS_ACCEPTED_GET, myReceive, function (res) {
      var datas = res.data.data.items;
      console.log(datas)
      for (let i = 0; i < datas.length; i++) {
        datas[i]["limitedAt"] = tool.toDate(datas[i]["limitedAt"])
      }
      that.setData({
        myReceive: datas
      })
    })
  },
  //撥打電話
  phoneCall: function (e) {
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.phone,
      success: function () {
        console.log("撥打電話")
      },
      fail:function(){
        console.log("撥打失敗")
      }
    })
  },
  // 跳轉詳情
  clickOrder: function (event){
    var orderNo = event.currentTarget.dataset.orderNo;
    wx.navigateTo({
      url: baseUrl + api.pageUrl.ORDER_PAGE_URL + '?id=' + orderNo
    })
  },
  // 禁止滑動
  stopTouchMove: function () {
    return false;
  },
  // 跳轉報箱封號
  clickSeal: function (event) {
    var orderNo = event.currentTarget.dataset.orderNo;
    wx.navigateTo({
      url: baseUrl + api.pageUrl.SEAL_PAGE_URL+'?id=' + orderNo
    })
  },
  // 跳轉墊款
  clickPrepay: function (event){
    var orderNo = event.currentTarget.dataset.orderNo;
    wx.showModal({
      title: '提示',
      content: '無墊款費用或和發單方線下結算墊款費用請點選跳過!如輸入墊款資訊,費用將由平臺連同運費一起墊付並收取平臺管理費,請謹慎輸入!',
      success(res) {
        if (res.confirm) {
          console.log('使用者點選確定')
          wx.navigateTo({
            url: baseUrl + api.pageUrl.PREPAY_PAGE_URL+'?id=' + orderNo
          })
        } else if (res.cancel) {
          console.log('使用者點選取消')
        }
      }
    })
  },
  // 跳轉回單
  clickReceipt: function (event) {
    var orderNo = event.currentTarget.dataset.orderNo;
    wx.navigateTo({
      url: baseUrl + api.pageUrl.RECEIPT_PAGE_URL+'?pid=' + orderNo
    })
  },
  // 已完成
  completed: function (event) {
    var that = this;
    var orderNo = event.currentTarget.dataset.orderNo;
    var completeData={
      orderNo:""
    }
    api.putRequest('/weChat/orders/' + orderNo + '/transitComplete.do', completeData, function(res){
      console.log(res.data)
      that.onShow()
    })
  },
  // 申請結算
  requestWithdraw: function (event) {
    var that = this;
    var orderNo = event.currentTarget.dataset.orderNo;
    api.putRequest('/weChat/orders/' + orderNo + '/requestWithdraw.do',orderNo,function(res){
      console.log(res.data)
    })
  }
})
也可以不用swiper來製作選項卡,可以直接給選項卡賦值,通過判斷來顯示,如果需要顯示的值不一樣也可以直接來判斷顯示。

動態顯示按鈕:

動態按鈕

通過wx:if來判斷顯示,如果點選後馬上更改狀態,就需要新增點選事件,在js設定方法呼叫介面成功後呼叫==onLoad()onShow()==方法重新整理頁面改變按鈕狀態值

撥打電話可參考api文件 [link]https://developers.weixin.qq.com/miniprogram/dev/api/wx.makePhoneCall.html.