RatingRating components is a star based selection input.

Rating - 图1

Documentation

Import

  1. import {RatingModule} from 'primeng/rating';
  2.  

Getting Started

Two-way value binding is defined using ngModel.

  1. <p-rating [(ngModel)]="val"></p-rating>
  2.  
  1. export class ModelComponent {
  2. val: number;
  3. }
  4.  

Defining a default value would be enough to display the initial number of selected stars.

  1. export class ModelComponent {
  2. val: number = 3;
  3. }
  4.  

Model Driven Forms

Rating can be used in a model driven form as well.

  1. <p-rating formControlName="score"></p-rating>
  2.  

Properties

NameTypeDefaultDescription
starsnumber5Number of stars.
cancelbooleantrueWhen specified a cancel icon is displayed to allow removing the value.
disabledbooleanfalseWhen present, it specifies that the element should be disabled.
readonlybooleanfalseWhen present, changing the value is not possible.
iconOnClassstringpi pi-starStyle class of the on icon.
iconOffClassstringpi pi-starStyle class of the off icon.
iconCancelClassstringpi pi-banStyle class of the cancel icon.
iconOnStyleobjectnullInline style of the on icon.
iconOffStyleobjectnullInline style of the off icon.
iconCancelStyleobjectnullInline style of the cancel icon.

Events

NameParametersDescription
onRateevent.originalEvent: browser event event.value: selected value Callback to invoke on rate change.
onCancelevent: browser eventCallback to invoke when value is removed.

Styling

Following is the list of structural style classes, for theming classes visit theming page.

NameElement
ui-ratingContainer element
ui-rating-starStar element
ui-rating-star-onSelected star element.
ui-rating-cancelCancel icon.

Dependencies

None.

Source

View on GitHub

  1. <h3 class="first">Basic {{val1}}</h3>
  2. <p-rating [(ngModel)]="val1"></p-rating>
  3. <h3>Callback {{val2}}</h3>
  4. <p-rating [(ngModel)]="val2" (onRate)="handleRate($event)" (onCancel)="handleCancel($event)"></p-rating> <span *ngIf="msg" style="margin-left:10px">{{msg}}</span>
  5. <h3>No Cancel {{val3}}</h3>
  6. <p-rating [(ngModel)]="val3" [cancel]="false"></p-rating>
  7. <h3>ReadOnly</h3>
  8. <p-rating [ngModel]="5" readonly="true" stars="10" [cancel]="false"></p-rating>
  9. <h3>Disabled</h3>
  10. <p-rating [ngModel]="val4" disabled="true" stars="10"></p-rating>
  11. <h3>Custom Icons</h3>
  12. <p-rating [ngModel]="val5" iconOnClass="pi pi-circle-on" iconOffClass="pi pi-circle-off" iconCancelClass="pi pi-times">;</p-rating>
  13.  
  1. export class RatingDemo {
  2. val1: number;
  3. val2: number = 5;
  4. val3: number;
  5. val4: number = 5;
  6. val5: number;
  7. msg: string;
  8. handleRate(event) {
  9. this.msg = "You have rated " + event.value;
  10. }
  11. handleCancel(event) {
  12. this.msg = "Rating Cancelled";
  13. }
  14. }
  15.