打分評分效果是很常用的功能,比如豆瓣評分里面經常出現的五星評分等,今天就給大家做一個類似的評分效果。
寫在前面
在制作一個醫院類小程序的前端時,有一個功能是對醫院進行評價,通過查找資料并結合自身的知識花了一個下午終于解決了。(由于知識欠缺的原因,中間一個問題阻礙了幾個小時,即為下文所陳列問題的第二條)。
效果預覽
分別截取了無評分狀態和評價狀態的頁面:
未選中
選中
代碼實現
1、feedback.js
-
// pages/more/feedback.js
-
Page({
-
-
/**
-
* 頁面的初始數據
-
*/
-
data: {
-
// 評價圖片
-
evaluationImgUrl: "https://s1.ax1x.com/2018/08/05/PDM8Bj.png",
-
starCheckedImgUrl: "https://s1.ax1x.com/2018/08/05/PDQ0it.png",
-
starUnCheckedImgUrl: "https://s1.ax1x.com/2018/08/05/PDQdII.png",
-
-
// 建議內容
-
opinion: "",
-
-
starMap: [
-
'非常差',
-
'差',
-
'一般',
-
'好',
-
'非常好',
-
],
-
-
evaluations: [
-
{
-
id: 0,
-
name: "醫院環境",
-
image: "https://s1.ax1x.com/2018/08/05/PDMaCV.png",
-
star: 0,
-
note: ""
-
},
-
{
-
id: 1,
-
name: "醫生專業技術",
-
image: "https://s1.ax1x.com/2018/08/05/PDMd3T.png",
-
star: 0,
-
note: ""
-
},
-
{
-
id: 2,
-
name: "醫生態度",
-
image: "https://s1.ax1x.com/2018/08/05/PDMN40.png",
-
star: 0,
-
note: ""
-
}
-
]
-
},
-
-
/**
-
* 評分
-
*/
-
chooseStar: function (e) {
-
const index = e.currentTarget.dataset.index;
-
const star = e.target.dataset.star;
-
let evaluations = this.data.evaluations;
-
let evaluation = evaluations[index];
-
// console.log(evaluation)
-
evaluation.star = star;
-
evaluation.note = this.data.starMap[star-1];
-
this.setData({
-
evaluations: evaluations
-
})
-
}
-
})
2、feedback.wxml
-
<!--pages/more/feedback.wxml-->
-
<view class='container'>
-
-
<view class='card'>
-
-
<!-- 為方便數據定位,自定義了wx:for-item為i -->
-
<block wx:for='{{evaluations}}' wx:for-item='i' wx:key=''>
-
<view class='card-item'>
-
<view class='item-title'>
-
<view class='image-container title-image'>
-
<image src='{{i.image}}'></image>
-
</view>
-
<view class='title-text'>{{i.name}}</view>
-
</view>
-
<view class='item-content'>
-
<view class='image-container content-image'>
-
<image src='{{evaluationImgUrl}}'></image>
-
</view>
-
<view class='contet-text content-body'>
-
<!-- 為方便數據定位,自定義了wx:for-item為j -->
-
<block wx:for='{{starMap}}' wx:for-item='j' wx:key=''>
-
<view class='image-container' data-index='{{i.id}}' bindtap='chooseStar'>
-
<image wx:if='{{i.star >= index + 1}}' data-star='{{index + 1}}' src='{{starCheckedImgUrl}}' bin></image>
-
<image wx:if='{{i.star < index + 1}}' data-star='{{index + 1}}' src='{{starUnCheckedImgUrl}}'></image>
-
</view>
-
</block>
-
<text class='note'>{{i.note}}</text>
-
</view>
-
</view>
-
</view>
-
</block>
-
-
</view>
-
-
<view class='submit' bindtap='submit'>提交反饋</view>
-
-
</view>
只貼出關鍵代碼,不附樣式代碼。
問題及解決方案
1、image圖片沒有點擊事件。
給image圖片加上一層外部容器,然后在這一層容器加上點擊事件。
2、三個評價內容是通過列表渲染實現的,而每個評價內容中的五個星星圖片也是通過列表渲染實現的,這樣就存在一個嵌套循環。此時,如何清楚地知道到底點了哪一個評價項的第幾顆星?
默認的當前元素變量名都是item,這樣的話,在里層循環里使用item指向的是里層的數組,而無法找到外層循環里的內容。于是我對兩層數組當前元素的變量名進行重命名(分別命名為 i 和 j ),這樣在里層使用i.*也可以訪問到外層數組的數據項。
3、如何動態顯示星級?
在wx:for里面嵌套wx:if或者hidden都可以實現效果。當星星下標+1小于或者等于當前星級的時候,就顯示為被選中的星星;當星星下標+1大于當前星級的時候,就顯示為被選中的星星。當星星被點中時,就將相應項的星級改為被點擊的星星的下標+1。