SelectButtonSelectButton is used to choose single or multiple items from a list using buttons.

SelectButton - 图1

Documentation

Import

  1. import {SelectButtonModule} from 'primeng/selectbutton';
  2.  

Getting Started

SelectButton requires a value to bind and a collection of options. There are two alternatives of how to define the options property; one way is providing a collection of SelectItem instances whereas other way is providing an array of arbitrary objects along with the optionLabel property to specify the field name of the option. SelectItem API is designed to have more control on how the options are displayed such as grouping and disabling however in most cases an arbitrary object collection will suffice. Example below demonstrates both cases.

  1. <p-selectButton [options]="cities1" [(ngModel)]="selectedCity1"></p-selectButton>
  2. <p-selectButton [options]="cities2" [(ngModel)]="selectedCity2" optionLabel="name"></p-selectButton>
  3.  
  1. import {SelectItem} from 'primeng/api';
  2. export class MyModel {
  3. cities1: SelectItem[];
  4. cities2: City[];
  5. selectedCity1: City;
  6. selectedCity2: City;
  7. constructor() {
  8. //SelectItem API with label-value pairs
  9. this.cities1 = [
  10. {label:'Select City', value:null},
  11. {label:'New York', value:{id:1, name: 'New York', code: 'NY'}},
  12. {label:'Rome', value:{id:2, name: 'Rome', code: 'RM'}},
  13. {label:'London', value:{id:3, name: 'London', code: 'LDN'}},
  14. {label:'Istanbul', value:{id:4, name: 'Istanbul', code: 'IST'}}
  15. {label:'Paris', value:{id:5, name: 'Paris', code: 'PRS'}}
  16. ];
  17. //An array of cities
  18. this.cities2 = [
  19. {name: 'New York', code: 'NY'},
  20. {name: 'Rome', code: 'RM'},
  21. {name: 'London', code: 'LDN'},
  22. {name: 'Istanbul', code: 'IST'},
  23. {name: 'Paris', code: 'PRS'}
  24. ];
  25. }
  26. }
  27.  

Multiple

SelectButton allows selecting only one item by default and setting multiple option enables choosing more than one item. In multiple case, model property should be an array instance that is not null or undefined.

  1. <p-selectButton [options]="cities" [(ngModel)]="selectedCities" multiple="multiple" ></p-selectButton>
  2.  
  1. import {SelectItem} from 'primeng/api';
  2. export class MyModel {
  3. cities: SelectItem[];
  4. selectedCities: string[] = [];
  5. constructor() {
  6. this.cities = [];
  7. this.cities.push({label:'New York', value:'New York'});
  8. this.cities.push({label:'Rome', value:'Rome'});
  9. this.cities.push({label:'London', value:'London'});
  10. this.cities.push({label:'Istanbul', value:'Istanbul'});
  11. this.cities.push({label:'Paris', value:'Paris'});
  12. }
  13. }
  14.  

Model Driven Forms

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

  1. <p-selectButton [options]="cities" formControlName="selectedCity"></p-selectButton>
  2.  

Icons

Button options can display icons using the icon property of the SelectItem API.

  1. <p-selectButton [options]="types" [(ngModel)]="selectedType"></p-selectButton>
  2.  
  1. export class SelectButtonDemo {
  2. types: SelectItem[];
  3. selectedType: string;
  4. constructor() {
  5. this.types = [];
  6. this.types.push({title: 'Paypal', value: 'PayPal', icon: 'fa fa-fw fa-cc-paypal'});
  7. this.types.push({title: 'Visa', value: 'Visa', icon: 'fa fa-fw fa-cc-visa'});
  8. this.types.push({title: 'MasterCard', value: 'MasterCard', icon: 'fa fa-fw fa-cc-mastercard'});
  9. }
  10. }
  11.  

Disabled Options

Particular options can be prevented from selection using the disabled property of SelectItem API.

Templating

Items support templating to display custom content inside the buttons using an ng-template that receives the option as the implicit variable. Note that if an arbitrary object is used as the option instead of the SelectItem API, internally they are converted to SelectItems which means in the template the arbitrary object can be access using the value property.

  1. <p-selectButton [options]="countries" [(ngModel)]="selectedCountry" optionLabel="name">
  2. <ng-template let-item>
  3. <img src="assets/showcase/images/demo/flag/{{item.value.flag}}" />
  4. <span>{{item.name}}</span>
  5. </ng-template>
  6. </p-selectButton>
  7.  
  1. export class SelectButtonDemo {
  2. countries: any[];
  3. selectedCountry: any;
  4. constructor() {
  5. this.countries = [
  6. {name: 'USA', flag: 'usa.png'},
  7. {name: 'Germany', flag: 'germany.png'},
  8. {name: 'Japan', flag: 'japan.png'}
  9. ];
  10. }
  11. }
  12.  

Properties

NameTypeDefaultDescription
optionsarraynullAn array of selectitems to display as the available options.
optionLabelstringnullName of the label field of an option when an arbitrary objects instead of SelectItems are used as options.
multiplebooleanfalseWhen specified, allows selecting multiple values.
tabindexnumber0Index of the element in tabbing order.
stylestringnullInline style of the component.
styleClassstringnullStyle class of the component.
disabledbooleanfalseWhen present, it specifies that the element should be disabled.
dataKeystringnullA property to uniquely identify a value in options.

Events

NameParametersDescription
onChangeevent.originalEvent: browser event event.value: single value or an array of values that are selected Callback to invoke when value changes.
onOptionClickevent.originalEvent: browser event event.option: SelectItem instance of the clicked button event.index: Index of the clicked button Callback to invoke when a button is clicked.

Dependencies

None.

Source

View on GitHub

  1. <h3 class="first">Single</h3>
  2. <p-selectButton [options]="types" [(ngModel)]="selectedType"></p-selectButton>
  3. <p>Selected Type: <span style="font-weight: bold">{{selectedType}}</span></p>
  4. <h3>Multiple</h3>
  5. <p-selectButton [options]="types" [(ngModel)]="selectedTypes" multiple="multiple"></p-selectButton>
  6. <p>Selected Types: <span style="font-weight: bold" *ngFor="let type of selectedTypes">{{type}} </span></p>
  7. <h3>Icon Only</h3>
  8. <p-selectButton [options]="modes" [(ngModel)]="selectedModes" multiple="multiple"></p-selectButton>
  9. <p>Selected Modes: <span style="font-weight: bold" *ngFor="let mode of selectedModes">{{mode}} </span></p>
  10. <h3>Custom Template</h3>
  11. <p-selectButton [options]="countries" [(ngModel)]="selectedCountry" optionLabel="name">
  12. <ng-template let-item>
  13. <div style="padding: .5em 1em">
  14. <img style="vertical-align: middle; margin-right: .5em" src="assets/showcase/images/demo/flag/{{item.value.flag}}" height="20px"/>
  15. <span>{{item.name}}</span>
  16. </div>
  17. </ng-template>
  18. </p-selectButton>
  19. <p>Selected Country: <span style="font-weight: bold">{{selectedCountry?.name}}</span></p>
  20. <hr>
  21. <button type="button" (click)="clear()" pButton icon="pi pi-times" label="Clear"></button>
  22.  
  1. export class SelectButtonDemo {
  2. types: SelectItem[];
  3. selectedType: string;
  4. selectedTypes: string[] = ['PayPal','MasterCard'];
  5. selectedModes: string[];
  6. modes: SelectItem[];
  7. countries: any[];
  8. selectedCountry: any;
  9. constructor() {
  10. this.types = [
  11. {label: 'Paypal', value: 'PayPal', icon: 'fa fa-fw fa-cc-paypal'},
  12. {label: 'Visa', value: 'Visa', icon: 'fa fa-fw fa-cc-visa'},
  13. {label: 'MasterCard', value: 'MasterCard', icon: 'fa fa-fw fa-cc-mastercard'}
  14. ];
  15. this.modes = [
  16. {value: 'Bold', title: 'Bold', icon: 'fa fa-fw fa-bold'},
  17. {value: 'Italic', title: 'Italic', icon: 'fa fa-fw fa-italic'},
  18. {value: 'Underline', title: 'Underline', icon: 'fa fa-fw fa-underline'}
  19. ];
  20. this.countries = [
  21. {name: 'USA', flag: 'usa.png'},
  22. {name: 'Germany', flag: 'germany.png'},
  23. {name: 'Japan', flag: 'japan.png'}
  24. ];
  25. }
  26. clear() {
  27. this.selectedType = null;
  28. this.selectedTypes = [];
  29. this.selectedModes = [];
  30. this.selectedCountry = null;
  31. }
  32. }
  33.