WeChat Mini Programs Implement Star Rating

WeChat Mini Programs Implement Star Rating

This article shares the specific code for WeChat applet to implement star rating for your reference. The specific content is as follows

First method:

WXML code:

<view>
    Star rating</view>
 
<view>
 
    <block class="brightStars">
        <image data-index='0' data-no="{{item}}" wx:for="{{starsBox}}" bindtap="changePic" wx:key="index" src="{{flag[0]>=item? '../img/starFull.png' : '../img/star.png'}}"></image>
    </block>
    <view class="texts">{{startext[0]}}</view>
</view>

JS code:

Page({
    data: {
        flag: [0, 0, 0],
        startext: ['', '', '', ],
        starsBox: [1, 2, 3, 4, 5]
    },
 
    changePic: function (e) {
        var index = e.currentTarget.dataset.index;
        console.log(index,'-');
        var num = e.currentTarget.dataset.no;
        console.log(num);
        var a = 'flag[' + index + ']';
        console.log(a);
        var b = 'startext[' + index + ']';
        console.log(b);
        var that = this;
        if (num == 1) {
            that.setData({
                [a]: 1,
                [b]: 'Very dissatisfied'
            });
        } else if (num == 2) {
            that.setData({
                [a]: 2,
                [b]: 'unsatisfied'
            });
        } else if (num == 3) {
            that.setData({
                [a]: 3,
                [b]: 'General'
            });
        } else if (num == 4) {
            that.setData({
                [a]: 4,
                [b]: 'Satisfied'
            });
        } else if (num == 5) {
            that.setData({
                [a]: 5,
                [b]: 'Very satisfied'
            });
        }
    },
 
})

WXSS Code:

.container{
    display: flex;
    flex-direction: row;
    padding: 0;
    flex-wrap: nowrap;
    
}
 
image{
    width: 50px;
    height: 50px;
}

Second method:

WXML Content:

<view>Star Rating</view>
 
<block wx:for="{{starYesNum}}" wx:key="index">
  <image bindtap="selectStar" id='{{index+1}}' data-in='selectStarYes' src='../../static/img/starY.png'></image>
</block>
<block wx:for="{{starNoNum}}" wx:key="index">
  <image bindtap='selectStar' id='{{index+1}}' data-in='selectStarNo' src='../../static/img/starN.png'></image>
</block>
<view>{{starYesNum}} star</view>

The first block tag contains a box with a bright star. The second block tag contains a box without a bright star.

Star rating is achieved by changing the number of bright stars and the number of dark stars

JS code:

Page({
  data: {
    starYesNum:0, //Number of lit stars starNoNum:5, //Number of unlit stars},
 
  selectStar:function(e){
    console.log(e.target.id);
    console.log(e.currentTarget.dataset.in);
 
    //Judge whether the clicked star is a bright star or a dark star, and set it if(e.currentTarget.dataset.in == 'selectStarNo'){
      this.setData({
        starYesNum: Number(e.target.id) + Number(this.data.starYesNum), 
        starNoNum:5-Number(e.target.id) - Number(this.data.starYesNum)
      })
    }else{
      this.setData({
        starYesNum:Number(e.target.id ),
        starNoNum:Number(5-e.target.id)
      })
    }
  },
})

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • WeChat Mini Program implements star rating and display
  • WeChat applet realizes star rating effect
  • Implementation of WeChat applet dynamic rating display/five-pointed star display/half star display/custom length display function
  • WeChat applet implements the function of displaying rating results
  • The store rating component in the WeChat applet and the rating display component implemented with svg in vue
  • An example of implementing a five-star rating system for WeChat Mini Programs
  • WeChat applet five-star rating effect implementation code
  • WeChat applet five-star rating (including half-star rating) example code
  • WeChat applet uses template tag to implement five-star rating function
  • WeChat Mini Program implements star rating and display

<<:  Problems and experiences encountered in web development

>>:  CSS web page responsive layout to automatically adapt to PC/Pad/Phone devices

Recommend

How to open MySQL binlog log

binlog is a binary log file, which records all my...

JavaScript implementation of magnifying glass details

Table of contents 1. Rendering 2. Implementation ...

Vue simulates the shopping cart settlement function

This article example shares the specific code of ...

Notes on configuring multiple proxies using vue projects

In the development process of Vue project, for th...

Vue shopping cart case study

Table of contents 1. Shopping cart example 2. Cod...

Analysis of MySQL cumulative aggregation principle and usage examples

This article uses examples to illustrate the prin...

Detailed explanation of the principle and function of Vue list rendering key

Table of contents The principle and function of l...

JavaScript Basics Objects

Table of contents 1. Object 1.1 What is an object...

Implementation and optimization of MySql subquery IN

Table of contents Why is IN slow? Which is faster...

Comprehensive summary of MYSQL tables

Table of contents 1. Create a table 1.1. Basic sy...

JavaScript to implement a simple shopping form

This article shares the specific code of JavaScri...

XHTML Getting Started Tutorial: Commonly Used XHTML Tags

<br />Just like an article, our web pages sh...

Reasons why MySQL 8.0 statistics are inaccurate

Preface Whether it is Oracle or MySQL, the new fe...

How to make a tar file of wsl through Docker

I've been playing with the remote development...