We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All

rating Development

The <rating> component provides a rating bar, commonly used for reviews and ratings. For details, see rating

Creating a <rating> Component

Create a <rating> component in the .hml file under pages/index.

Collapse
Word wrap
Dark theme
Copy code
  1. <!-- xxx.hml -->
  2. <div class="container">
  3. <rating></rating>
  4. </div>
Collapse
Word wrap
Dark theme
Copy code
  1. /* xxx.css */
  2. .container {
  3. width: 100%;
  4. height: 100%;
  5. display: flex;
  6. justify-content: center;
  7. align-items: center;
  8. background-color: #F1F3F5;
  9. }
  10. .rating {
  11. width: 80%;
  12. height: 150px;
  13. }

Setting the Rating Level

Use the <rating> component to set the number of stars in a rating bar and the current rating using the numstars and rating attributes, respectively.

Collapse
Word wrap
Dark theme
Copy code
  1. <!-- xxx.hml -->
  2. <div class="container">
  3. <rating numstars="6" rating="5">
  4. </rating>
  5. </div>
Collapse
Word wrap
Dark theme
Copy code
  1. /* xxx.css */
  2. .container {
  3. width: 100%;
  4. height: 100%;
  5. display: flex;
  6. justify-content: center;
  7. align-items: center;
  8. background-color: #F1F3F5;
  9. }
  10. .rating {
  11. width: 80%;
  12. height: 150px;
  13. }

Setting the Rating Style

Use the <rating> component to set the background images when a rating star is unselected, selected, and partially selected using the star-background, star-foreground, and star-secondary attributes, respectively.

Collapse
Word wrap
Dark theme
Copy code
  1. <!-- xxx.hml -->
  2. <div class="container">
  3. <div style="width: 500px;height: 500px;align-items: center;justify-content: center;flex-direction: column;">
  4. <rating numstars="5" rating="1" class="myrating" style="width: {{ratewidth}}; height:{{rateheight}};
  5. star-background: {{backstar}}; star-secondary: {{secstar}};star-foreground: {{forestar}};rtl-flip: true;">
  6. </rating>
  7. </div>
  8. </div>
Collapse
Word wrap
Dark theme
Copy code
  1. /* xxx.css */
  2. .container {
  3. width: 100%;
  4. height: 100%;
  5. flex-direction: column;
  6. align-items: center;
  7. justify-content: center;
  8. background-color: #F1F3F5;
  9. }
Collapse
Word wrap
Dark theme
Copy code
  1. // index.js
  2. export default {
  3. data: {
  4. backstar: 'common/love.png',
  5. secstar: 'common/love.png',
  6. forestar: 'common/love1.png',
  7. ratewidth: '400px',
  8. rateheight: '150px'
  9. },
  10. onInit(){
  11. }
  12. }

NOTE
  • You must set star-background, star-secondary, and star-foreground. If they are not set, gray rating stars will be displayed, indicating an incorrect image source configuration

  • These three attributes support only PNG and JPG images from local file paths.

Binding Events

Add the change event to the <rating> component to display the current rating.

Collapse
Word wrap
Dark theme
Copy code
  1. <!-- xxx.hml -->
  2. <div class="container">
  3. <rating numstars="5" rating="0" onchange="showrating"></rating>
  4. </div>
Collapse
Word wrap
Dark theme
Copy code
  1. /* xxx.css */
  2. .container {
  3. width: 100%;
  4. height: 100%;
  5. display: flex;
  6. justify-content: center;
  7. align-items: center;
  8. background-color: #F1F3F5;
  9. }
  10. .rating {
  11. width: 80%;
  12. height: 150px;
  13. }
Collapse
Word wrap
Dark theme
Copy code
  1. // xxx.js
  2. export default {
  3. showrating(e) {
  4. this.getUIContext().getPromptAction().showToast({
  5. message:'Rating' + e.rating
  6. })
  7. }
  8. }

Example

Change the switch status to toggle between the star background images and drag the slider to adjust the rating values.

Collapse
Word wrap
Dark theme
Copy code
  1. <!-- xxx.hml -->
  2. <div style="width: 100%;height:100%;flex-direction: column;align-items: center;background-color: #F1F3F5;">
  3. <div style="width: 500px;height: 500px;align-items: center;justify-content: center;flex-direction: column;">
  4. <rating numstars="{{stars}}" rating="{{rate}}" stepsize="{{step}}" onchange="showrating" class="myrating"
  5. style="width: {{ratewidth}};height:{{rateheight}};star-background: {{backstar}};star-secondary: {{secstar}};
  6. star-foreground: {{forestar}};rtl-flip: true;"></rating>
  7. </div>
  8. <div style="flex-direction: column;width: 80%;align-items: center;">
  9. <div style="width: 100%;height: 100px;align-items: center;justify-content: space-around;">
  10. <text&>Use custom image</text&>
  11. <switch checked="false" showtext="true" onchange="setstar"></switch>
  12. </div>
  13. <div style="width: 100%;height:120px;margin-top: 50px;margin-bottom: 50px;flex-direction: column;align-items: center;
  14. justify-content: space-around;">
  15. <text>numstars {{stars}}</text>
  16. <slider id="sli1" min="0" max="10" value="5" step="1" onchange="setnumstars"></slider>
  17. </div>
  18. <div style="width: 100%;height:120px;flex-direction: column;align-items: center;justify-content: space-around;">
  19. <text>rating {{rate}}</text>
  20. <slider id="sli2" min="0" max="10" value="{{rate}}" step="0.5" onchange="setrating"></slider>
  21. </div>
  22. </div>
  23. </div>
Collapse
Word wrap
Dark theme
Copy code
  1. /* xxx.css */
  2. .myrating:active {
  3. width: 500px;
  4. height: 100px;
  5. }
  6. .switch{
  7. font-size: 40px;
  8. }
Collapse
Word wrap
Dark theme
Copy code
  1. // xxx.js
  2. export default {
  3. data: {
  4. backstar: '',
  5. secstar: '',
  6. forestar: '',
  7. stars: 5,
  8. ratewidth: '300px',
  9. rateheight: '60px',
  10. step: 0.5,
  11. rate: 0
  12. },
  13. onInit(){
  14. },
  15. setstar(e) {
  16. if (e.checked == true) {
  17. this.backstar = 'common/love.png'
  18. this.secstar = 'common/love.png'
  19. this.forestar = 'common/love1.png'
  20. } else {
  21. this.backstar = ''
  22. this.secstar = ''
  23. this.forestar = ''
  24. }
  25. },
  26. setnumstars(e) {
  27. this.stars = e.progress
  28. this.ratewidth = 60 * parseInt(this.stars) + 'px'
  29. },
  30. setrating(e){
  31. this.rate = e.progress
  32. },
  33. showrating(e) {
  34. this.rate = e.rating
  35. this.getUIContext().getPromptAction().showToast({
  36. message:'Rating' + e.rating
  37. })
  38. }
  39. }

Search in Guides
Enter a keyword.