文檔首頁>>telerik中文文檔>>按鈕集合
按鈕集合
ButtonGroup使您能夠通過迭代一組配置對象來呈現(xiàn)多個Button組件。
下面的示例演示如何呈現(xiàn)按鈕集合。它利用每個按鈕的selectedChange事件,該事件在按鈕狀態(tài)更改時執(zhí)行自定義邏輯。
app.component.ts
import { Component, ViewEncapsulation } from '@angular/core'; import { clockIcon, checkCircleIcon, xCircleIcon, minusCircleIcon } from '@progress/kendo-svg-icons'; export interface IButton { text: string; icon: string; color: string; selected?: boolean; } @Component({ selector: 'my-app', template: ` <div class="teamMate"> <img [src]="img" alt="Nancy Leverling" /> <span class="k-icon k-i-circle status" [ngStyle]="{ color: status }" ></span> <span class="mate-info"> <h2>Nancy Leverling</h2> <p>Team Lead</p> </span> </div> <kendo-buttongroup width="100%" selection="single"> <button *ngFor="let button of buttons" [svgIcon]="button.svgIcon" kendoButton [toggleable]="true" [selected]="button.selected" (selectedChange)="selectedChange($event, button)" > <span> <span class="k-icon {{ button.icon }}"></span> {{ button.text }} </span> </button> </kendo-buttongroup> `, encapsulation: ViewEncapsulation.None, styles: [ ` .status { margin-left: -22px; padding-top: 1px; font-size: 25px; } .teamMate h2 { font-size: 1.3em; font-weight: normal; padding-top: 17px; margin: 0; } .teamMate p { margin: 0; font-size: 0.8em; } .teamMate img { display: inline-block; vertical-align: top; width: 50px; height: 50px; margin: 10px; border: 1px solid #ccc; border-radius: 50%; } .mate-info { display: inline-block; vertical-align: top; } ` ] }) export class AppComponent { public status = '#10b507'; public buttons = [ { text: 'Away', svgIcon: clockIcon, color: '#f0c505' }, { text: 'Available', svgIcon: checkCircleIcon, color: '#10b507', selected: true }, { text: 'Offline', svgIcon: xCircleIcon, color: '#707070' }, { text: 'Do not disturb', svgIcon: minusCircleIcon, color: '#e30000' } ]; public img = 'https://demos.telerik.com/kendo-ui/content/web/panelbar/nancy.jpg'; public selectedChange(e: boolean, btn: IButton): void { this.status = btn.color; } }
app.module.ts
import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; import { ButtonsModule } from '@progress/kendo-angular-buttons'; import { AppComponent } from './app.component'; @NgModule({ imports: [BrowserModule, BrowserAnimationsModule, ButtonsModule], declarations: [AppComponent], bootstrap: [AppComponent] }) export class AppModule { }
main.ts
import './polyfills'; import { enableProdMode } from '@angular/core'; import { AppModule } from './app.module'; import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; enableProdMode(); const platform = platformBrowserDynamic(); platform.bootstrapModule(AppModule, { preserveWhitespaces: true });
存儲存中的按鈕
當(dāng)您在ButtonGroup中實現(xiàn)按鈕集合時,可以通過將選擇存儲在自定義集合中來跟蹤所選按鈕。
要啟用此行為:
- 設(shè)置“ButtonGroup”的選擇模式為“multiple”。
- 處理每個按鈕的selectedChange事件以更新集合。
app.component.ts
import { Component, ViewChild, ViewContainerRef } from '@angular/core'; import { NotificationService } from '@progress/kendo-angular-notification'; export interface IDay { text: string; index: number; } @Component({ selector: 'my-app', template: ` <div class="example-config">Selected Days: {{ selectedDays | json }}</div> <div class="row"> <div class="col-sm-12 col-md-4 example-col"> <p>Days</p> <kendo-buttongroup selection="multiple"> <button *ngFor="let button of buttons" kendoButton [toggleable]="true" (selectedChange)="selectedChange($event, button)" > {{ button.text }} </button> </kendo-buttongroup> </div> </div> <p>Notification container</p> <div class="append-container" #container style=" height: 150px; border: 1px solid black;" ></div> ` }) export class AppComponent { @ViewChild('container', { read: ViewContainerRef }) public container: ViewContainerRef; public buttons = [ { text: 'Monday', index: 0 }, { text: 'Tuesday', index: 1 }, { text: 'Wednesday', index: 2 }, { text: 'Thursday', index: 3 }, { text: 'Friday', index: 4 }, { text: 'Saturday', index: 5 }, { text: 'Sunday', index: 6 } ]; public selectedDays = []; constructor(private notificationService: NotificationService) { } public selectedChange(isTrue: boolean, day: IDay): void { if (isTrue) { this.selectedDays.push(day.index); } else { const index = this.selectedDays.indexOf(day.index); this.selectedDays.splice(index, 1); } this.selectedDays.sort((a, b) => a - b); this.show(isTrue, day); } public show(isChecked: boolean, day: IDay): void { if (isChecked) { this.showNotification(isChecked, day); } else { this.showNotification(isChecked, day); } } // Show notification public showNotification(checked: boolean, day: IDay): void { this.notificationService.show({ content: `${day.text} was ${checked ? 'selected' : 'deselected'}`, appendTo: this.container, position: { horizontal: 'left', vertical: 'top' }, animation: { type: 'fade', duration: 300 }, type: { style: `${checked ? 'success' : 'info'}`, icon: true }, hideAfter: 5000 }); } }
app.module.ts
import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; import { ButtonsModule } from '@progress/kendo-angular-buttons'; import { NotificationModule } from '@progress/kendo-angular-notification'; import { AppComponent } from './app.component'; @NgModule({ imports: [BrowserModule, BrowserAnimationsModule, ButtonsModule, NotificationModule], declarations: [AppComponent], bootstrap: [AppComponent] }) export class AppModule { }
main.ts
import './polyfills'; import { enableProdMode } from '@angular/core'; import { AppModule } from './app.module'; import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; enableProdMode(); const platform = platformBrowserDynamic(); platform.bootstrapModule(AppModule, { preserveWhitespaces: true });