ButtonButton is an extension to standard input element with icons and theming.

Button - 图1

Documentation

Import

  1. import {ButtonModule} from 'primeng/button';
  2.  

Getting Started

Button is either applies as a component using p-button element or a directive using pButton attribute. Directive enhances an existing button whereas p-button is an element on its own.

  1. <button pButton type="button" label="Click" ></button>
  2. <p-button label="Click" ></p-button>
  3.  

Events

Events are defined using standard notation in pButton and with on* prefix at p-button.

  1. <button pButton type="button" label="Click" (click)="handleClick($event)"></button>
  2. <p-button label="Click" (onClick)="handleClick($event)"></p-button>
  3.  
  1. export class Model {
  2. handleClick() {
  3. //execute action
  4. }
  5. }
  6.  

Icons

Icon on a button is specified with icon attribute and position is customized using iconPos attribute. Default icon position is left. To display only an icon, leave label as undefined.

  1. <button pButton type="button" icon="pi pi-check" iconPos="left"></button>
  2. <p-button label="Click" icon="pi pi-check" iconPos="left"></p-button>
  3.  

Severity

Different color options are available to define severity levels.

  • .ui-button-secondary
  • .ui-button-success
  • .ui-button-info
  • .ui-button-warning
  • .ui-button-danger
  1. <button pButton type="button" class="ui-button-info"></button>
  2. <p-button label="Click" styleClass="ui-button-info"></p-button>
  3.  

Raised and Rounded Buttons

A button can be raised by having "ui-button-raised" style class and similarly borders can be made rounded using "ui-button-rounded" class.

  1. <button pButton type="button" class="ui-button-raised ui-button-rounded"></button>
  2.  

Properties of pButton

NameTypeDefaultDescription
labelstringnullText of the button.
iconstringnullName of the icon.
iconPosstringleftPosition of the icon, valid values are "left" and "right".
cornerStyleClassstringui-corner-allDefines the cornering of the button, valid replacements are top, left, right and button such as ui-corner-top.

Properties of p-button

NameTypeDefaultDescription
typestringnullType of the button.
labelstringnullText of the button.
iconstringnullName of the icon.
iconPosstringleftPosition of the icon, valid values are "left" and "right".
disabledbooleanfalseWhen present, it specifies that the component should be disabled.
stylestringnullInline style of the element.
styleClassstringnullStyle class of the element.
onClickeventnullCallback to execute when button is clicked.
onFocuseventnullCallback to execute when button is focused.
onBlureventnullCallback to execute when button loses focus.

Styling

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

NameElement
ui-buttonButton element
ui-button-iconIcon element
ui-button-textLabel element of the button

Dependencies

None.

Source

View on GitHub

  1. <h3 class="first">Button Component</h3>
  2. <p-button label="Click"></p-button>
  3. <p-button icon="pi pi-check" label="Click"></p-button>
  4. <p-button icon="pi pi-check" iconPos="right" label="Click"></p-button>
  5. <p-button icon="pi pi-check"></p-button>
  6. <p-button icon="pi pi-check" [disabled]="true" label="Disabled"></p-button>
  7. <h3>Button Directive</h3>
  8. <button pButton type="button"label="Click"></button>
  9. <button pButton type="button" icon="pi pi-check" label="Click"></button>
  10. <button pButton type="button" icon="pi pi-check" iconPos="right" label="Click"></button>
  11. <button pButton type="button" icon="pi pi-check"></button>
  12. <button pButton type="button" icon="pi pi-check" [disabled]="true" label="Disabled"></button>
  13. <h3>Severity Buttons</h3>
  14. <button pButton type="button" label="Primary"></button>
  15. <button pButton type="button" label="Secondary" class="ui-button-secondary"></button>
  16. <button pButton type="button" label="Success" class="ui-button-success"></button>
  17. <button pButton type="button" label="Info" class="ui-button-info"></button>
  18. <button pButton type="button" label="Warning" class="ui-button-warning"></button>
  19. <button pButton type="button" label="Danger" class="ui-button-danger"></button>
  20. <h3>Raised Buttons</h3>
  21. <button pButton type="button" label="Primary" class="ui-button-raised"></button>
  22. <button pButton type="button" label="Secondary" class="ui-button-raised ui-button-secondary"></button>
  23. <button pButton type="button" label="Success" class="ui-button-raised ui-button-success"></button>
  24. <button pButton type="button" label="Info" class="ui-button-raised ui-button-info"></button>
  25. <button pButton type="button" label="Warning" class="ui-button-raised ui-button-warning"></button>
  26. <button pButton type="button" label="Danger" class="ui-button-raised ui-button-danger"></button>
  27. <h3>Rounded Buttons</h3>
  28. <button pButton type="button" label="Primary" class="ui-button-rounded"></button>
  29. <button pButton type="button" label="Secondary" class="ui-button-rounded ui-button-secondary"></button>
  30. <button pButton type="button" label="Success" class="ui-button-rounded ui-button-success"></button>
  31. <button pButton type="button" label="Info" class="ui-button-rounded ui-button-info"></button>
  32. <button pButton type="button" label="Warning" class="ui-button-rounded ui-button-warning"></button>
  33. <button pButton type="button" label="Danger" class="ui-button-rounded ui-button-danger"></button>
  34.  
  1. export class ButtonDemo {
  2. clicks: number = 0;
  3. count() {
  4. this.clicks++;
  5. }
  6. }
  7.