欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

微信小程序使用template標簽實現(xiàn)五星評分功能

 更新時間:2018年11月03日 11:10:21   作者:T丶快樂知己丬  
這篇文章主要為大家詳細介紹了微信小程序使用template標簽實現(xiàn)五星評分功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下

前言

微信小程序特點“無須安裝,無須卸載,觸手可及,用完即走”,適合輕量級應用快速開發(fā)。小程序模版思想是它開發(fā)的核心思想,可以幫助開發(fā)者減少很多重復代碼,我們來看看怎么在小程序中實現(xiàn)市面上常用的評分組件, 看看效果圖:

創(chuàng)建模版

wxml文件:

以<template>為根節(jié)點,添加name屬性用來區(qū)分不同模版:

<template name="starsTemplate">
 <view class='stars-container'>
  <view class='stars'>
  <block wx:for="{{stars}}" wx:for-item="i" wx:key="position">
   <image wx:if="{{i}}" src='/images/icon/star.png'></image>
    <!-- <image wx:elif="{{i==2}}" src='/images/icon/none-star.png'></image> -->
    <image wx:else src='/images/icon/none-star.png'></image>
  </block>
  </view>
  <text class='star-score'>{{score}}</text>
 </view>
</template>

wxss文件:

這里我們采用小程序推薦使用flex模型,橫向排列五顆星星圖片。

.stars-container {
 display: flex;
 flex-direction: row;
}
.stars {
 display: flex;
 flex-direction: row;
 height: 17rpx;
 margin-right: 24rpx;
 margin-top: 6rpx;
}
.stars image {
 padding-left: 3rpx;
 height: 17rpx;
 width: 17rpx;
}
.star-score {
 color: #1f3463;
}

模版使用

引用wxml模版:

<import src="../stars/stars-template.wxml" /> <!--這里引用模板文件最好使用相對路徑-->
<template is="starsTemplate" data="{{stars:stars,score:average}}" /> <!--is指定使用模版的名稱-->

引用wxss模版:

在使用模版文件對應的wxss文件中輸入以下表達式即可

@import "../stars/stars-template.wxss";

數(shù)據(jù)裝換:

我看到在使用模版wxml文件時有個data=“{{stars:stars,score:average}}”屬性和數(shù)據(jù),表示指定模版的數(shù)據(jù),我們一般從后臺獲取的評分數(shù)據(jù)一般都是一個數(shù)字,比如:3.5,4,5等等,我們需要把這些數(shù)字轉(zhuǎn)換成五個等級數(shù)組,決定評分星星顯示什么圖片,這里我們用1表示顯示高亮的,0表示灰色星星,我們也可以在一個2表示半顆高亮的星星表示0.5的評分:

function convertToStarsArray(stars) {
 var num = stars.toString().substring(0, 1);
 var array = [];
 for (var i = 1; i <= 5; i++) {
  if (i < num) {
   array.push(1);
  } else {
   array.push(0);
  }
 }
 return array;
}

案例wxml代碼:

<import src="stars/stars-template.wxml" />
<view style='margin:100rpx'>
 <template is="starsTemplate" data="{{stars:stars,score:average}}" />
</view>

到這里基本結(jié)束了,我們這評分轉(zhuǎn)換成我們需要的評分數(shù)組,在使用模版的地方傳入數(shù)據(jù)即可。

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論