• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# <rating> Development
2
3
4The **<rating>** component provides a rating bar used for reviews and ratings. For details, see [rating](../reference/arkui-js/js-components-basic-rating.md).
5
6
7## Creating a <rating> Component
8
9Create a **<rating>** component in the .hml file under **pages/index**.
10
11
12```
13<!-- xxx.hml -->
14<div class="container">
15  <rating></rating>
16</div>
17```
18
19
20```
21/* xxx.css */
22.container {
23  width: 100%;
24  height: 100%;
25  display: flex;
26  justify-content: center;
27  align-items: center;
28  background-color: #F1F3F5;
29}
30rating {
31  width: 80%;
32  height: 150px;
33}
34```
35
36![en-us_image_0000001231843116](figures/en-us_image_0000001231843116.gif)
37
38
39## Setting the Rating Level
40
41Use the **&lt;rating&gt;** component to set the number of stars in a rating bar and the current rating using the **numstars** and **rating** attributes, respectively.
42
43
44```
45<!-- xxx.hml -->
46<div class="container">
47  <ratingnumstars="6" rating="5">
48  </rating>
49</div>
50```
51
52
53```
54/* xxx.css */
55.container {
56  width: 100%;
57  height: 100%;
58  display: flex;
59  justify-content: center;
60  align-items: center;
61  background-color: #F1F3F5;
62}
63rating {
64  width: 80%;
65  height: 150px;
66}
67```
68
69![en-us_image_0000001232003012](figures/en-us_image_0000001232003012.gif)
70
71
72## Setting the Rating Style
73
74Use the **&lt;rating&gt;** 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.
75
76
77```
78<!-- xxx.hml -->
79<div class="container">
80  <div style="width: 500px;height: 500px;align-items: center;justify-content: center;flex-direction: column;;">
81    <rating numstars="5" rating="1" class="myrating" style="width: {{ratewidth}}; height:{{rateheight}};
82    star-background: {{backstar}}; star-secondary: {{secstar}};star-foreground: {{forestar}};rtl-flip: true;">
83    </rating>
84  </div>
85</div>
86```
87
88
89```
90/* xxx.css */
91.container {
92  width: 100%;
93  height: 100%;
94  flex-direction: column;
95  align-items: center;
96  justify-content: center;
97  background-color: #F1F3F5;
98}
99```
100
101
102```
103/* index.js */
104export default {
105  data: {
106    backstar: 'common/love.png',
107    secstar: 'common/love.png',
108    forestar: 'common/love1.png',
109    ratewidth: '400px',
110    rateheight: '150px'
111  },
112  onInit(){
113  }
114}
115```
116
117![en-us_image_0000001275803173](figures/en-us_image_0000001275803173.gif)
118
119> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE:**
120> - You must set **star-background**, **star-secondary**, and **star-foreground**. Otherwise, the grey rating star applies, indicating that the image source is incorrectly set.
121>
122> - The **star-background**, **star-secondary**, and **star-foreground** attributes support only PNG and JPG images in the local path.
123
124
125## Binding Events
126
127Add the **change** event to the &lt;rating&gt; component to print the current rating.
128
129
130```
131<!-- xxx.hml -->
132<div class="container">
133  <rating numstars="5" rating="0" onchange="showrating"></rating>
134</div>
135```
136
137
138```
139.container {
140  width: 100%;
141  height: 100%;
142  display: flex;
143  justify-content: center;
144  align-items: center;
145  background-color: #F1F3F5;
146}
147rating {
148  width: 80%;
149  height: 150px;
150}
151```
152
153
154```
155import prompt from '@system.prompt';
156export default {
157  showrating(e) {
158    prompt.showToast({
159      message:'Current Rating' + e.rating
160    })
161  }
162}
163```
164
165![en-us_image_0000001276003525](figures/en-us_image_0000001276003525.gif)
166
167
168## Example Scenario
169
170Change the switch status to toggle between the star background images and drag the slider to adjust the rating values.
171
172
173```
174<!-- xxx.hml -->
175<div style="width: 100%;height:100%;flex-direction: column;align-items: center;background-color: #F1F3F5;">
176  <div style="width: 500px;height: 500px;align-items: center;justify-content: center;flex-direction: column;;">
177    <rating numstars="{{stars}}" rating="{{rate}}" stepsize="{{step}}" onchange="showrating" class="myrating"
178    style="width: {{ratewidth}};height:{{rateheight}};star-background: {{backstar}};star-secondary: {{secstar}};
179    star-foreground: {{forestar}};rtl-flip: true;"></rating>
180  </div>
181  <div style="flex-direction: column;width: 80%;align-items: center;">
182    <div style="width: 100%;height: 100px;align-items: center;justify-content: space-around;">
183      <text>Replacing a custom image</text>
184      <switch checked="false" showtext="true" onchange="setstar"></switch>
185    </div>
186    <div style="width: 100%;height:120px;margin-top: 50px;margin-bottom: 50px;flex-direction: column;align-items: center;
187    justify-content: space-around;">
188      <text>numstars   {{stars}}</text>
189      <slider id="sli1" min="-1" max="10" value="5" step="1" onchange="setnumstars"></slider>
190    </div>
191    <div style="width: 100%;height:120px;flex-direction: column;align-items: center;justify-content: space-around;">
192      <text>rating   {{rate}}</text>
193      <slider id="sli2" min="-1" max="10" value="0" step="1" onchange="setrating"></slider>
194    </div>
195  </div>
196</div>
197```
198
199
200```
201/* xxx.css */
202.myrating:active {
203  width: 500px;
204  height: 100px;
205}
206switch{
207  font-size: 40px;
208}
209```
210
211
212```
213/* index.js */
214import prompt from '@system.prompt';
215export default {
216  data: {
217    backstar: '',
218    secstar: '',
219    forestar: '',
220    stars: 5,
221    ratewidth: '300px',
222    rateheight: '60px',
223    step: 0.5,
224    rate: 0
225  },
226  onInit(){
227  },
228  setstar(e) {
229    if (e.checked == true) {
230      this.backstar = 'common/love.png'
231      this.secstar = 'common/love.png'
232      this.forestar = 'common/love1.png'
233    } else {
234      this.backstar = ''
235      this.secstar = ''
236      this.forestar = ''
237    }
238  },
239  setnumstars(e) {
240    this.stars = e.progress
241    this.ratewidth = 60 * parseInt(this.stars) + 'px'
242  },
243  setstep(e) {
244    this.step = e.progress
245  },
246  setrating(e){
247    this.rate = e.progress
248  },
249  showrating(e) {
250    prompt.showToast({
251      message:'Current Rating' + e.rating
252    })
253  }
254}
255```
256
257![en-us_image_0000001275923005](figures/en-us_image_0000001275923005.gif)
258