微信小程序(8)——五星级评分

野性酷女 2022-05-17 02:46 809阅读 0赞

▍写在前面

在制作一个医院类小程序的前端时,有一个功能是对医院进行评价,通过查找资料并结合自身的知识花了一个下午终于解决了。(由于知识欠缺的原因,中间一个问题阻碍了几个小时,即为下文所陈列问题的第二条)。

▍效果预览

分别截取了无评分状态和评价状态的页面:

70 未选中

70 1 选中

▍代码实现

1、feedback.js

  1. // pages/more/feedback.js
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. // 评价图片
  8. evaluationImgUrl: "https://s1.ax1x.com/2018/08/05/PDM8Bj.png",
  9. starCheckedImgUrl: "https://s1.ax1x.com/2018/08/05/PDQ0it.png",
  10. starUnCheckedImgUrl: "https://s1.ax1x.com/2018/08/05/PDQdII.png",
  11. // 建议内容
  12. opinion: "",
  13. starMap: [
  14. '非常差',
  15. '差',
  16. '一般',
  17. '好',
  18. '非常好',
  19. ],
  20. evaluations: [
  21. {
  22. id: 0,
  23. name: "医院环境",
  24. image: "https://s1.ax1x.com/2018/08/05/PDMaCV.png",
  25. star: 0,
  26. note: ""
  27. },
  28. {
  29. id: 1,
  30. name: "医生专业技术",
  31. image: "https://s1.ax1x.com/2018/08/05/PDMd3T.png",
  32. star: 0,
  33. note: ""
  34. },
  35. {
  36. id: 2,
  37. name: "医生态度",
  38. image: "https://s1.ax1x.com/2018/08/05/PDMN40.png",
  39. star: 0,
  40. note: ""
  41. }
  42. ]
  43. },
  44. /**
  45. * 评分
  46. */
  47. chooseStar: function (e) {
  48. const index = e.currentTarget.dataset.index;
  49. const star = e.target.dataset.star;
  50. let evaluations = this.data.evaluations;
  51. let evaluation = evaluations[index];
  52. // console.log(evaluation)
  53. evaluation.star = star;
  54. evaluation.note = this.data.starMap[star-1];
  55. this.setData({
  56. evaluations: evaluations
  57. })
  58. }
  59. })

2、feedback.wxml

  1. <!--pages/more/feedback.wxml-->
  2. <view class='container'>
  3. <view class='card'>
  4. <!-- 为方便数据定位,自定义了wx:for-item为i -->
  5. <block wx:for='{
  6. {evaluations}}' wx:for-item='i' wx:key=''>
  7. <!-- 单个卡片项 -->
  8. <view class='card-item'>
  9. <!-- 卡片上方标题 -->
  10. <view class='item-title'>
  11. <view class='image-container title-image'>
  12. <image src='{
  13. {i.image}}'></image>
  14. </view>
  15. <view class='title-text'>{
  16. {i.name}}</view>
  17. </view>
  18. <!-- 卡片下方评价区 -->
  19. <view class='item-content'>
  20. <view class='image-container content-image'>
  21. <image src='{
  22. {evaluationImgUrl}}'></image>
  23. </view>
  24. <view class='contet-text content-body'>
  25. <!-- 为方便数据定位,自定义了wx:for-item为j -->
  26. <block wx:for='{
  27. {starMap}}' wx:for-item='j' wx:key=''>
  28. <view class='image-container' data-index='{
  29. {i.id}}' bindtap='chooseStar'>
  30. <image wx:if='{
  31. {i.star >= index + 1}}' data-star='{
  32. {index + 1}}' src='{
  33. {starCheckedImgUrl}}' bin></image>
  34. <image wx:if='{
  35. {i.star < index + 1}}' data-star='{
  36. {index + 1}}' src='{
  37. {starUnCheckedImgUrl}}'></image>
  38. </view>
  39. </block>
  40. <text class='note'>{
  41. {i.note}}</text>
  42. </view>
  43. </view>
  44. </view>
  45. </block>
  46. </view>
  47. <view class='submit' bindtap='submit'>提交反馈</view>
  48. </view>

3、*.wxss(样式由两个部分构成)

  1. /* common.wxss */
  2. .card .card-item{
  3. margin-bottom: 25rpx;
  4. border: 5rpx solid #888;
  5. border-radius: 20rpx;
  6. padding: 25rpx;
  7. background-color: white;
  8. }
  9. .card-item .item-title,
  10. .card-item .item-content{
  11. display: flex;
  12. flex-direction: row;
  13. flex-wrap: nowrap;
  14. }
  15. .card-item .item-title{
  16. border-bottom: 1rpx solid #eee;
  17. padding-bottom: 25rpx;
  18. align-items: center;
  19. line-height: 70rpx;
  20. font-weight: 700;
  21. }
  22. .card-item .item-content{
  23. padding-top: 25rpx;
  24. line-height: 70rpx;
  25. }
  26. .card-item .image-container{
  27. margin-right: 25rpx;
  28. width: 40rpx;
  29. display: flex;
  30. align-items: center;
  31. }
  32. .card-item .title-image{
  33. height: 40rpx;
  34. }
  35. .card-item .content-image{
  36. height: 70rpx;
  37. }
  38. .card-item .image-container image{
  39. width: 40rpx;
  40. height: 40rpx;
  41. }
  42. .card-item .title-text,
  43. .card-item .content-text{
  44. width: 575rpx;
  45. }
  46. /* feedback.wxss */
  47. .card-item .content-body{
  48. display: flex;
  49. flex-direction: row;
  50. align-items: center;
  51. }
  52. .card-item .content-body .note{
  53. line-height: 70rpx;
  54. font-size: 30rpx;
  55. }

▍问题及解决方案

1、image图片没有点击事件。

给image图片加上一层外部容器,然后在这一层容器加上点击事件。

2、三个评价内容是通过列表渲染实现的,而每个评价内容中的五个星星图片也是通过列表渲染实现的,这样就存在一个嵌套循环。此时,如何清楚地知道到底点了哪一个评价项的第几颗星?

默认的当前元素变量名都是item,这样的话,在里层循环里使用item指向的是里层的数组,而无法找到外层循环里的内容。于是我对两层数组当前元素的变量名进行重命名(分别命名为 i 和 j ),这样在里层使用i.*也可以访问到外层数组的数据项。

3、如何动态显示星级?

在wx:for里面嵌套wx:if或者hidden都可以实现效果。当星星下标+1小于或者等于当前星级的时候,就显示为被选中的星星;当星星下标+1大于当前星级的时候,就显示为被选中的星星。当星星被点中时,就将相应项的星级改为被点击的星星的下标+1。

关注微信公众号:爱唱歌的蜗牛先生

发表评论

表情:
评论列表 (有 0 条评论,809人围观)

还没有评论,来说两句吧...

相关阅读

    相关 程序3:五星级评分

    ▍写在前面 在制作一个医院类小程序的前端时,有一个功能是对医院进行评价,通过查找资料并结合自身的知识花了一个下午终于解决了。(由于知识欠缺的原因,中间一个问题阻碍了几个小时,

    相关 程序实现星星评分效果

    思路很简单,小星星都是一张张独立的图片,点击的时候改变图片的路径就可以了。 我是用背景图片做的,先给盒子设置背景图片为灰色的小星星,盒子是根据js文件中的stars数组循环