小程式自定義元件的備忘
阿新 • • 發佈:2018-12-09
File:threecolgrid.js
// components/threeColumnGrid/threecolgrid.js Component({ /** * 元件的屬性列表 */ properties: { // 傳入的資料 booksData:{ type: Array, value:[] }, // grid的外邊距 mainMargin:{ type: Number, value: 5 }, // grid的內邊距 mainPadding:{ type:Number, value:10 }, // 行間距 rowSpace:{ type:Number, value:20 }, // 列間距 colSpace:{ type:Number, value:7 } }, /** * 元件的初始資料 */ data: { }, /** * 元件的方法列表 */ methods: { checkIn:function(e){ console.log(e.currentTarget.dataset) // 觸發action事件,triggerEvent函式接受三個值:事件名稱、資料、選項值 this.triggerEvent('action', e.currentTarget.dataset, {}) } } })
<!--components/threeColumnGrid/threecolgrid.wxml--> <!-- <view class='main-content'> --> <view class='bookbox' style='padding:{{mainPadding}}rpx;margin:{{mainMargin}}rpx;'> <block wx:for="{{booksData}}" wx:key="{{item.id}}"> <view class='bookinfo' style='margin-bottom: {{rowSpace}}rpx;'> <image src='{{item.url}}' mode="widthFix" style='width:{{(740 - 2*mainPadding - 2*mainMargin)/3 - colSpace*3}}rpx;'></image> <view class="title" style='width:{{(740 - 2*mainPadding - 2*mainMargin)/3 - colSpace*3}}rpx;'>{{item.title}}</view> <view class='actionBar' style='width:{{(740 - 2*mainPadding - 2*mainMargin)/3 - colSpace*3}}rpx;'> <block wx:if="{{item.status == 0}}"> <view class='bookstatus disable'>暫缺貨</view> <view bindtap='checkIn' data-bookid='{{item.id}}' data-status='{{item.status}}' class='iconfont icon-219-heart icon'></view> </block> <block wx:else> <view class='bookstatus enable'>可借閱</view> <view bindtap='checkIn' data-bookid='{{item.id}}' data-status='{{item.status}}' class='iconfont icon-059-cart icon'></view> </block> </view> </view> </block> </view> <!-- </view> -->
/* components/threeColumnGrid/threecolgrid.wxss */ @import "../../iconfont.wxss"; /* .main-content{ background: #FFF; margin-top: 155rpx; padding:10rpx; } */ .bookbox{ display: flex; flex-direction: row; justify-content: space-between; flex-wrap:wrap; background: #FFF; } .bookbox .bookinfo{ display: flex; flex-direction: column; justify-content: center; align-items: center; /* border: 1rpx solid #f5a32d; */ } .bookbox .bookinfo image{ display: flex; /* 調節圖書列 */ /* width:220rpx; */ border-top-left-radius: 10rpx; border-top-right-radius: 10rpx } .bookbox .bookinfo .title{ display: flex; flex-wrap: wrap; font-size: small; margin-bottom: 5rpx; /* 調節圖書列 */ /* width:300rpx; */ /* width:220rpx; */ height: 70rpx; } .bookbox .bookinfo .actionBar{ display: flex; justify-content: space-between; align-items: center; padding: 0rpx 5rpx 5rpx 5rpx; font-size: smaller; /* width:200rpx; */ } .bookbox .bookinfo .actionBar .bookstatus{ display: flex; border-radius: 10rpx; padding:0rpx 5rpx; } .bookbox .bookinfo .actionBar .enable{ color: #FFF; background-color: #f5a32d; } .bookbox .bookinfo .actionBar .disable{ color: #FFF; background-color: #727171; } .bookbox .bookinfo .actionBar .icon{ font-size: 15pt; color: #facea7; } .bookinfo-empty{ display: flex; flex-direction: column; align-items: center; padding: 5rpx; width:230rpx; }
使用元件
//事件處理函式
getCheckValues: function(e){
console.log('checkbox發生change事件,攜帶value值為:', e.detail)
},
{
"usingComponents": {
"three-col-grid":"/components/threeColumnGrid/threecolgrid"
},
}
<view class='test'>
<three-col-grid booksData="{{BookList}}" bind:action="getCheckValues"></three-col-grid>
</view>
.test{
display: flex;
justify-content: center;
/* border: 1rpx #444 solid; */
}