Angular Grid 行選択
Ignite UI for Angular の行選択では、行内の他のすべての列に先行する行セレクター列があります。行選択ボックスをクリックすると、行の選択や選択解除、複数行にわたるデータの選択が可能になります。
Angular 行選択の例
以下のサンプルは、Grid の行選択 の 3 つのタイプを示します。以下のボタンを使用して、使用可能な各選択モードを有効にします。Snackbar メッセージ ボックスで各ボタンの操作について簡単に説明します。切り替えボタンを使用して、行セレクターのチェックボックスを非表示または表示します。
新しく選択された要素を取得するには、event.newSelection を使用できます。
public handleRowSelection (event: IRowSelectionEventArgs ) {
this .selectedRowsCount = event.newSelection.length;
this .selectedRowIndex = event.newSelection[0 ];
this .snackbarRowCount.open();
this .snackbar.close();
this .logAnEvent(`=> 'rowSelectionChanging' with value: ` + JSON .stringify(event.newSelection));
}
ts
import { NgModule } from "@angular/core" ;
import { FormsModule } from "@angular/forms" ;
import { BrowserModule } from "@angular/platform-browser" ;
import { BrowserAnimationsModule } from "@angular/platform-browser/animations" ;
import { AppComponent } from "./app.component" ;
import { HttpClientModule } from "@angular/common/http" ;
import {
IgxAvatarModule,
IgxBadgeModule,
IgxButtonModule,
IgxSnackbarModule,
IgxGridModule,
IgxIconModule,
IgxInputGroupModule,
IgxSwitchModule
} from "igniteui-angular" ;
import { GridSelectionSampleComponent } from "./grid/grid-sample-selection/grid-selection.component" ;
import { IgxPreventDocumentScrollModule } from "./directives/prevent-scroll.directive" ;
import { FinancialDataService } from "./services/financial.service" ;
@NgModule ({
bootstrap : [AppComponent],
declarations : [
AppComponent,
GridSelectionSampleComponent
],
imports : [
BrowserModule,
BrowserAnimationsModule,
FormsModule,
IgxPreventDocumentScrollModule,
IgxAvatarModule,
IgxBadgeModule,
IgxButtonModule,
IgxGridModule,
IgxSnackbarModule,
IgxIconModule,
IgxInputGroupModule,
IgxSwitchModule,
HttpClientModule
],
providers : [FinancialDataService],
entryComponents : [],
schemas : []
})
export class AppModule {}
ts コピー import { Component, OnInit, ViewChild } from '@angular/core' ;
import { GridSelectionMode, IgxGridComponent, IgxSnackbarComponent, IRowSelectionEventArgs } from 'igniteui-angular' ;
import { Observable } from 'rxjs' ;
import { FinancialDataService } from '../../services/financial.service' ;
@Component ({
providers : [FinancialDataService],
selector : 'grid-sample' ,
styleUrls : ['./grid-selection.component.scss' ],
templateUrl : 'grid-selection.component.html'
})
export class GridSelectionSampleComponent implements OnInit {
@ViewChild ('grid1' , { static : true }) public grid1: IgxGridComponent;
@ViewChild ('snackbarRowCount' , { static : true }) public snackbarRowCount: IgxSnackbarComponent;
@ViewChild ('snackbar' , { static : true }) public snackbar: IgxSnackbarComponent;
public data: Observable<any []>;
public selectionMode: GridSelectionMode = 'multiple' ;
public selectionModes = [];
public hideRowSelectors = false ;
public selectedRows = [1 , 2 , 3 ];
public selectedRowsCount;
public selectedRowIndex;
constructor (private localService: FinancialDataService ) {
this .localService.getData(100000 );
this .data = this .localService.records;
this .selectionModes = [
{ label : 'none' , selected : this .selectionMode === 'none' , togglable : true },
{ label : 'single' , selected : this .selectionMode === 'single' , togglable : true },
{ label : 'multiple' , selected : this .selectionMode === 'multiple' , togglable : true }
];
}
public ngOnInit(): void {
this .snackbar.autoHide = false ;
this .snackbar.open();
this .snackbarRowCount.autoHide = true ;
this .snackbarRowCount.close();
}
public formatNumber (value: number ) {
return value.toFixed(2 );
}
public formatCurrency (value: number ) {
return '$' + value.toFixed(2 );
}
public handleRowSelection (event:IRowSelectionEventArgs ) {
this .selectedRowsCount = event.newSelection.length;
this .selectedRowIndex = event.newSelection[0 ];
this .snackbarRowCount.open();
this .snackbar.close();
}
public selectCellSelectionMode (args ) {
this .selectionMode = this .selectionModes[args.index].label;
this .snackbar.open();
this .snackbarRowCount.close();
this .selectedRowsCount = undefined ;
this .selectedRowIndex = undefined ;
}
}
ts コピー
<div class ="sample-container" >
<div class ="grid-controls" >
<igx-switch [(ngModel )]="hideRowSelectors" > Hide Row Selectors</igx-switch >
<igx-buttongroup [values ]="selectionModes" (selected )="selectCellSelectionMode($event)" > </igx-buttongroup >
</div >
<igx-grid [igxPreventDocumentScroll ]="true" #grid1 [data ]="data | async" [height ]="'530px'" [primaryKey ]="'ID'" width ="100%" [cellSelection ]="'none'"
[rowSelection ]="selectionMode" [selectedRows ]="selectedRows" [hideRowSelectors ]="hideRowSelectors" [allowFiltering ]="true"
(rowSelectionChanging )="handleRowSelection($event)" >
<igx-column [field ]="'Category'" > </igx-column >
<igx-column [field ]="'Type'" [filterable ]='false' > </igx-column >
<igx-column [field ]="'Price'" dataType ="number" [formatter ]="formatCurrency" > </igx-column >
<igx-column [field ]="'Buy'" dataType ="number" [formatter ]="formatCurrency" > </igx-column >
<igx-column [field ]="'Sell'" dataType ="number" [formatter ]="formatCurrency" > </igx-column >
<igx-column [field ]="'Change'" dataType ="number" [headerClasses ]="'headerAlignSyle'" >
<ng-template igxHeader >
<span > Change</span >
</ng-template >
<ng-template igxCell let-val >
<div class ="currency-badge-container" >
<igx-badge *ngIf ="val>0" type ="success" position ="bottom-right" icon ="arrow_upward"
class ="badge-left" > </igx-badge >
<igx-badge *ngIf ="val<0" type ="error" position ="bottom-right" icon ="arrow_downward"
class ="error badge-left" > </igx-badge >
<span class ="cellAlignSyle" [class.up ]="val>0" [class.down ]="val<0" > {{ formatNumber(val) }}</span >
</div >
</ng-template >
</igx-column >
<igx-column [field ]="'Change(%)'" dataType ="number" [formatter ]="formatNumber" >
<ng-template igxHeader >
<span > Change(%)</span >
</ng-template >
<ng-template igxCell let-val >
<span class ="cellAlignSyle" [class.up ]="val>0" [class.down ]="val<0" > {{ formatNumber(val) }}%</span >
</ng-template >
</igx-column >
<igx-column [field ]="'Change On Year(%)'" dataType ="number" [formatter ]="formatNumber" >
<ng-template igxCell let-val >
<div class ="currency-badge-container" >
<igx-badge *ngIf ="val>0" type ="success" position ="bottom-right" icon ="arrow_upward"
class ="badge-left" > </igx-badge >
<igx-badge *ngIf ="val<0" type ="error" position ="bottom-right" icon ="arrow_downward"
class ="error badge-left" > </igx-badge >
<span class ="cellAlignSyle" [class.up ]="val>0" [class.down ]="val<0" > {{ formatNumber(val) }}%</span >
</div >
</ng-template >
</igx-column >
</igx-grid >
</div >
<igx-snackbar #snackbarRowCount actionText ="Got it. Thanks!" (clicked )="snackbarRowCount.close()" >
<div class ="container" >
<igx-icon > notification_important</igx-icon >
<ng-container *ngIf ="selectionMode === 'multiple'" >
<p > Number of selected rows: {{selectedRowsCount}}</p >
</ng-container >
<ng-container *ngIf ="selectionMode === 'single' && this.selectedRowIndex !== undefined" >
<p > Currently selected row index: {{selectedRowIndex}}</p >
</ng-container >
<ng-container *ngIf ="selectionMode === 'single' && this.selectedRowIndex === undefined" >
<p > There is no currently selected row.</p >
</ng-container >
</div >
</igx-snackbar >
<igx-snackbar #snackbar actionText ="Got it. Thanks!" (clicked )="snackbar.close()" >
<div class ="container" >
<igx-icon > notification_important</igx-icon >
<ng-container *ngIf ="selectionMode === 'multiple'" >
<ul >
<li > <b > Now you can select multiple rows within a grid.</b > </li >
<li > Click on row selector field or press SPACE key when some cell is active to toggle row
selection.
</li >
<li > On cell click the row get selected and previous selection state is cleared.</li >
<li > On cell click holding ctrl key, the row get selected and previous selection state is
preserved.
</li >
<li > Shift + click select a range of rows.</li >
</ul >
</ng-container >
<ng-container *ngIf ="selectionMode === 'single'" >
<ul >
<li > <b > Now you can select only one row within a grid.</b > </li >
<li > Click on row selector field or press SPACE key when some cell is active to toggle row
selection.
</li >
<li > On cell click the row get selected and previous selection state is cleared.</li >
</ul >
</ng-container >
<ng-container *ngIf ="selectionMode === 'none'" >
<ul >
<li > <b > Now you are unable to select a row while interacting with grid UI.</b > </li >
<li > If you need to select a row use grid API methods.</li >
</ul >
</ng-container >
</div >
</igx-snackbar >
html コピー .cellAlignSyle {
text-align : right;
float : right;
}
.cellAlignSyle > span {
float : right;
}
.up {
color : green;
}
.down {
color : red;
}
.headerAlignSyle {
text-align : right !important ;
}
.sample-container {
margin : 16px ;
}
.grid-controls {
display : flex;
justify-content : space-between;
flex-flow : column;
align-items : flex-start;
position : relative;
> * {
margin-bottom : 16px ;
}
}
.currency-badge-container {
width : 80px ;
float : right;
}
.badge-left {
float : left;
}
ul {
padding : 0 ;
list-style-type : none;
}
.igx-snackbar {
background : rgba(0 , 0 , 0 , 0.7 );
}
.container {
display : flex;
igx-icon {
margin : 20px ;
}
}
.container p {
margin-top : 20px ;
}
scss コピー
このサンプルが気に入りましたか? 完全な Ignite UI for Angularツールキットにアクセスして、すばやく独自のアプリの作成を開始します。無料でダウンロードできます。
設定
igx-grid
で行選択を設定するには、rowSelection プロパティを設定します。このプロパティは、GridSelectionMode 列挙を受け取ります。GridSelectionMode は、次の 3 つのモードを公開します: none 、single および multiple 。以下で、それぞれについて詳しく説明します。
None 選択
igx-grid
では、デフォルトで行選択が無効になります。それ以外の場合 ([rowSelection]="'none'")。したがって、Grid UI とのインタラクションを通じて行を選択または選択解除することはできません が、選択/選択解除する唯一の方法は、提供された API メソッドを使用することです。
単一選択
単一行の選択は、[rowSelection] = '"single"'
プロパティの設定のみで簡単に設定できるようになりました。これにより、グリッド内の 1 行のみを選択できます 。行のセルにフォーカスするときにセルをクリックするかスペースキーを押すと行を選択できます。もちろん、行セレクターフィールドをクリックして行を選択できます。行が選択または選択解除されると、rowSelectionChanging イベントが生成されます。
<igx-grid [data ]="remote | async" [rowSelection ]="'single'" [autoGenerate ]="true"
(rowSelectionChanging )="handleRowSelection($event)" [allowFiltering ]="true" >
</igx-grid >
html
public handleRowSelection (args ) {
if (args.added.length && args.added[0 ] === 3 ) {
args.cancel = true ;
}
}
typescript
複数選択
igx-grid
で複数行選択を有効にするには、rowSelection
プロパティを multiple
に設定します。これにより、各行および Grid ヘッダーで行セレクター フィールドが有効になります。行セレクターを使用して複数行を選択できます。選択はスクロール、ページング、およびソートとフィルターリング、などのデータ操作で保持されます。行を選択するには、セルをクリックするか、セルにフォーカスがあるときにスペースキーを押します。1 つの行を選択し、Shift キーを押しながら別の行をクリックすると、行の範囲全体が選択されます。この選択モードでは、単一の行をクリックすると、前に選択した行が選択解除されます。Ctrl キーを押しながらクリックすると、行が切り替わり、前の選択が保持されます。
<igx-grid [data ]="remote | async" [primaryKey ]="'ProductID'" [rowSelection ]="'multiple'"
(rowSelectionChanging )="handleRowSelection($event)" [allowFiltering ]="true" [autoGenerate ]="true" >
</igx-grid >
html
<!-- selectionExample.component.ts -->
public handleRowSelection (event: IRowSelectionEventArgs ) {
this .selectedRowsCount = event.newSelection.length;
this .selectedRowIndex = event.newSelection[0 ];
}
ts
注
行選択およびセル選択を正しく実行するには、Grid にリモート仮想化がある場合、primaryKey
を設定します。
Grid でリモート仮想化を使用した場合、ヘッダーのチェックボックスをクリックすると、現在グリッドにあるすべてのレコードが選択/選択解除されます。新しいデータがオンデマンドで Grid にロードされると、新、しく追加された行は選択されない制限があるため、これらの行を選択するには API メソッドを使用して動作を処理する必要があります。
行を選択すると、rowSelectionChanging
イベントがトリガーされます。このイベントは、新しい選択、古い選択、古い選択に対して追加および削除された行に関する情報を提供します。また、イベントはキャンセル可能であるため、選択を防ぐことができます。
行選択が有効になっている場合、行セレクターが表示されますが、表示しない場合は、[hideRowSelectors] = true
に設定できます。
行選択モードのランタイムを切り替えると、優先行選択状態がクリアされます。
API の使用
コードで行を選択
以下は、単一または複数の行を同時に選択できるコード スニペットです (primaryKey
を介して)。さらに、このメソッドの 2 番目のパラメーターは boolean プロパティです。それを使用して、前の行の選択をクリアするかどうかを選択できます。以前の選択はデフォルトで保存されます。
<igx-grid ... [primaryKey ]="'ID'" >
...
</igx-grid >
...
<button (click )="this.grid.selectRows([1,2,5], true)" > Select 1,2 and 5</button > // select rows and clear previous selection state
html
1、2、および 5 の ID を持つデータ エントリに対応する行を Grid の選択に追加します。
行選択の解除
プログラムで行を選択解除する必要がある場合は、deselectRows(rowIds: [])
を使用できます。
<igx-grid ... [primaryKey ]="'ID'" >
...
</igx-grid >
...
<button (click )="this.grid.deselectRows([1,2,5])" > Deselect 1,2 and 5</button >
html
行選択イベント
行選択に変更がある場合、rowSelectionChanging
イベントが発行されます。rowSelectionChanging
は次の引数を公開します:
oldSelection
- 行選択の前の状態を含む行データの配列。
newSelection
- 行選択の新しい状態に一致する行データの列。
added
- 現在選択に追加されている行データの配列。
removed
- 古い選択状態に従って現在削除されている行 データの配列。
event
- 行選択の変更をトリガーする元のイベント。
cancel
- 行選択の変更をトリガーする元のイベント。
リモート データ シナリオでの行選択イベント
リモート データ シナリオでは、グリッドに primaryKey
が設定されている場合、rowSelectionChanging.oldSelection
イベント引数には、現在データ ビューに含まれていない行の完全な行データ オブジェクトが含まれません。この場合、rowSelectionChanging.oldSelection
オブジェクトには、primaryKey
フィールドである 1 つのプロパティのみが含まれます。現在データ ビューにある残りの行については、rowSelectionChanging.oldSelection
に行データ全体が含まれます。
<igx-grid (rowSelectionChanging )="handleRowSelectionChange($event)" >
...
</igx-grid >
html
public handleRowSelectionChange (args ) {
args.cancel = true ;
}
typescript
全行の選択
igx-grid
が提供するもう 1 つの便利な API メソッドが selectAll(onlyFilteredData)
です。このメソッドはデフォルトですべてのデータ行を選択しますが、フィルタリングが適用される場合、フィルター条件に一致する行のみが選択されます。ただし、false パラメーターを指定してメソッドを呼び出すと、selectAll(false)
は、フィルターが適用されているかどうかに関係なく、常にグリッド内のすべてのデータを選択します。
selectAll()
は削除された行を選択しないことに注意してください。
全行の選択解除
igx-grid
は、デフォルトですべてのデータ行の選択を解除する deselectAll(onlyFilteredData)
メソッドを提供しますが、フィルタリングが適用される場合、フィルター条件に一致する行のみを選択解除します。ただし、false パラメーターを指定してメソッドを呼び出すと、deselectAll(false)
は、フィルターが適用されているかどうかに関係なく、常にグリッド内のすべてのデータをクリアします。
選択した行を取得する方法
現在選択されている行を確認する必要がある場合は、selectedRows
ゲッターを使用して行 ID を取得できます。
public getSelectedRows ( ) {
const currentSelection = this .grid.selectedRows;
}
typescript
さらに、selectedRows
に行 ID を割り当てると、グリッドの選択状態を変更できます。
public mySelectedRows = [1 , 2 , 3 ];
typescript
<igx-grid primaryKey ="ProductID" rowSelection ="multiple" [autoGenerate ]="false" [mySelectedRows ]="selectedRows" [data ]="data" >
<igx-column [field ]="'ProductID'" > </igx-column >
<igx-column [field ]="'ProductName'" > </igx-column >
<igx-column [field ]="'UnitsInStock'" > </igx-column >
</igx-grid >
html
行セレクター テンプレート
Grid でヘッダーおよび行セレクターをテンプレート化し、さまざまなシナリオに役立つ機能を提供するコンテキストにアクセスすることもできます。
デフォルトでは、Grid は、行セレクターの親コンテナーまたは行自体のすべての行選択操作を処理 し、テンプレートの状態の可視化のみになります。基本機能のオーバーライドは通常、rowSelectionChanging
イベント を使用して実行する必要があります。基本機能をオーバーライドする click
ハンドラーを使用してカスタムテンプレートを実装する場合、イベントの伝播を停止して、正しい行の状態を保持する必要があります。
行テンプレート
カスタム行セレクター テンプレートを作成するには、igx-grid
内で igxRowSelector
ディレクティブを使用して <ng-template>
を宣言します。テンプレートから、行の状態に関する情報を提供するプロパティを使用して、暗黙的に提供されたコンテキスト変数にアクセスできます。
selected
プロパティは、現在の行が選択されているかどうかを示しますが、index
プロパティを使用して行インデックスにアクセスできます。
<ng-template igxRowSelector let-rowContext >
{{ rowContext.index }}
<igx-checkbox
[checked ]="rowContext.selected"
[readonly ]="true"
> </igx-checkbox >
</ng-template >
html
rowID
プロパティを使用して、igx-grid
行の参照を取得できます。行セレクター要素に click
ハンドラーを実装する場合に便利です。
<ng-template igxRowSelector let-rowContext >
<igx-checkbox (click )="onSelectorClick($event, rowContext.key)" > </igx-checkbox >
</ng-template >
html
上の例では、igx-checkbox
を使用しており、rowContext.selected
をその checked
プロパティにバインドしています。行番号のデモ
で実際にこれをご覧ください。
ヘッダー テンプレート
Grid 内でカスタムヘッダーセレクターテンプレートを作成するには、igxHeadSelector
ディレクティブで <ng-template>
を宣言します。テンプレートから、ヘッダーの状態に関する情報を提供するプロパティを使用して、暗黙的に提供されたコンテキスト変数にアクセスできます。
selectedCount
プロパティは現在選択されている行数を示し、totalCount
は Grid に合計の行数を示します。
<ng-template igxHeadSelector let-headContext >
{{ headContext.selectedCount }} / {{ headContext.totalCount }}
</ng-template >
html
selectedCount
および totalCount
プロパティを使用して、ヘッド セレクターをチェックするか、不確定にする (部分的に選択する) かを決定できます。
<igx-grid [data ]="gridData" primaryKey ="ProductID" rowSelection ="multiple" >
<ng-template igxHeadSelector let-headContext >
<igx-checkbox
[checked ]=" headContext.selectedCount > 0 && headContext.selectedCount === headContext.totalCount"
[indeterminate ]="headContext.selectedCount > 0 && headContext.selectedCount !== headContext.totalCount" >
</igx-checkbox >
</ng-template >
</igx-grid >
html
行の番号付けデモ
このデモでは、カスタム ヘッダーと行セレクターの使用方法を示します。後者は、rowContext.index
を使用して行番号と、rowContext.selected
にバインドされた igx-checkbox
を表示します。
import { NgModule } from "@angular/core" ;
import { FormsModule } from "@angular/forms" ;
import { BrowserModule } from "@angular/platform-browser" ;
import { BrowserAnimationsModule } from "@angular/platform-browser/animations" ;
import { AppComponent } from "./app.component" ;
import {
IgxCheckboxModule,
IgxGridModule
} from "igniteui-angular" ;
import { GridSelectionTemplateNumbersComponent } from "./grid/grid-sample-selection-template-numbers/grid-sample-selection-template-numbers.component" ;
import { IgxPreventDocumentScrollModule } from "./directives/prevent-scroll.directive" ;
@NgModule ({
bootstrap : [AppComponent],
declarations : [
AppComponent,
GridSelectionTemplateNumbersComponent
],
imports : [
BrowserModule,
BrowserAnimationsModule,
FormsModule,
IgxPreventDocumentScrollModule,
IgxCheckboxModule,
IgxGridModule
],
providers : [],
entryComponents : [],
schemas : []
})
export class AppModule {}
ts コピー import { Component } from '@angular/core' ;
import { DATA } from '../../data/customers' ;
@Component ({
selector : 'app-grid-selection-template-numbers' ,
styleUrls : ['./grid-sample-selection-template-numbers.component.scss' ],
templateUrl : 'grid-sample-selection-template-numbers.component.html'
})
export class GridSelectionTemplateNumbersComponent {
public data: any [];
constructor ( ) {
this .data = [...DATA];
}
}
ts コピー <div class ="grid__wrapper" >
<igx-grid [igxPreventDocumentScroll ]="true" #myGrid [data ]="data" primaryKey ="ID" rowSelection ="multiple" cellSelection ="none" [height ]="'510px'" width ="100%" >
<igx-paginator > </igx-paginator >
<igx-column field ='ContactName' width ="20%" > </igx-column >
<igx-column field ='Country' width ="20%" > </igx-column >
<igx-column field ='City' width ="20%" > </igx-column >
<igx-column field ='PostalCode' width ="20%" > </igx-column >
<igx-column field ='CompanyName' width ="20%" > </igx-column >
<ng-template igxHeadSelector >
<div class ="header-selector" >
<img src ="https://www.infragistics.com/angular-demos-lob/assets/images/card/avatars/igLogo.png" class ="header-image" >
</div >
</ng-template >
<ng-template igxRowSelector let-rowContext >
<div class ="row-selector" >
<span class ="row-selector-index" > {{ rowContext.index + 1 }}</span >
<igx-checkbox class ="row-selector-checkbox"
[checked ]="rowContext.selected"
[readonly ]="true"
[disableRipple ]="true" [disableTransitions ]="myGrid.disableTransitions" >
</igx-checkbox >
</div >
</ng-template >
</igx-grid >
</div >
html コピー .grid__wrapper {
margin : 0 auto;
padding : 16px ;
}
.header-selector {
width : 70px ;
height : 60px ;
display : flex;
justify-content : center;
align-items : center;
}
.header-image {
width : 40px ;
height : 40px ;
}
.row-selector {
width : 70px ;
display : flex;
justify-content : space-evenly;
align-items : center;
height : 100% ;
}
.row-selector-index {
width : 35px ;
text-align : center;
}
scss コピー
Excel スタイル行セレクターのデモ
このデモは、カスタム テンプレートを使用して Excel ライクなヘッダーおよび行セレクターを示します。
import { NgModule } from "@angular/core" ;
import { FormsModule } from "@angular/forms" ;
import { BrowserModule } from "@angular/platform-browser" ;
import { BrowserAnimationsModule } from "@angular/platform-browser/animations" ;
import { AppComponent } from "./app.component" ;
import { IgxGridModule } from "igniteui-angular" ;
import { GridSelectionTemplateExcelComponent } from "./grid/grid-sample-selection-template-excel/grid-sample-selection-template-excel.component" ;
import { IgxPreventDocumentScrollModule } from "./directives/prevent-scroll.directive" ;
@NgModule ({
bootstrap : [AppComponent],
declarations : [
AppComponent,
GridSelectionTemplateExcelComponent
],
imports : [
BrowserModule,
BrowserAnimationsModule,
FormsModule,
IgxPreventDocumentScrollModule,
IgxGridModule
],
providers : [],
entryComponents : [],
schemas : []
})
export class AppModule {}
ts コピー import { Component } from '@angular/core' ;
import { DATA } from '../../data/customers' ;
@Component ({
selector : 'app-grid-selection-template-excel' ,
styleUrls : ['./grid-sample-selection-template-excel.component.scss' ],
templateUrl : 'grid-sample-selection-template-excel.component.html'
})
export class GridSelectionTemplateExcelComponent {
public data: any [];
constructor ( ) {
this .data = DATA;
}
}
ts コピー <div class ="grid__wrapper" >
<igx-grid [igxPreventDocumentScroll ]="true" #myGrid [data ]="data" primaryKey ="ID" rowSelection ="multiple" cellSelection ="none" [height ]="'510px'" width ="100%" >
<igx-paginator > </igx-paginator >
<igx-column field ='ContactName' width ="20%" > </igx-column >
<igx-column field ='Country' width ="20%" > </igx-column >
<igx-column field ='City' width ="20%" > </igx-column >
<igx-column field ='PostalCode' width ="20%" > </igx-column >
<igx-column field ='CompanyName' width ="20%" > </igx-column >
<ng-template igxHeadSelector let-headContext >
<div class ="header-selector" [ngClass ]="{
'excel-style-arrow-all': headContext.selectedCount === headContext.totalCount && headContext.totalCount !== 0,
'excel-style-arrow': headContext.selectedCount !== headContext.totalCount
}" >
<i class ="material-icons" > signal_cellular_4_bar</i >
</div >
</ng-template >
<ng-template igxRowSelector let-rowContext >
<div class ="row-selector" >
<span > {{ rowContext.index + 1 }}</span >
</div >
</ng-template >
</igx-grid >
</div >
html コピー @use '../../../variables' as *;
$default-arrow-color : color($color : 'gray' , $variant : 400 );
$default-arrow-hover-color : color($color : 'gray' , $variant : 800 );
$default-arrow-all-color : color($color : 'secondary' , $variant : 100 );
@mixin selector($justify , $align ) {
display : flex;
justify-content : $justify ;
align-items : $align ;
min-width : 50px ;
cursor : pointer;
}
.grid__wrapper {
margin : 0 auto;
padding : 16px ;
}
.header-selector {
@include selector(flex-end, flex-end);
height : 100% ;
padding : 8px ;
}
.row-selector {
@include selector(center, center);
}
::ng-deep {
.igx-grid__cbx-selection {
padding: 0 ;
}
}
.excel-style-arrow {
color : $default-arrow-color ;
&:hover {
color : $default-arrow-hover-color ;
}
}
.excel-style-arrow-all {
color : $default-arrow-all-color ;
}
scss コピー
条件付き選択のデモ
このデモでは、rowSelectionChanging
イベントと、選択できない行のチェックボックスが無効になっているカスタム テンプレートを使用して、一部の行が選択されないようにします。
import { NgModule } from "@angular/core" ;
import { FormsModule } from "@angular/forms" ;
import { BrowserModule } from "@angular/platform-browser" ;
import { BrowserAnimationsModule } from "@angular/platform-browser/animations" ;
import { AppComponent } from "./app.component" ;
import {
IgxGridModule,
IgxCheckboxModule
} from "igniteui-angular" ;
import { GridConditionalRowSelectorsComponent } from "./grid/grid-conditional-row-selectors/grid-conditional-row-selectors-sample.component" ;
import { IgxPreventDocumentScrollModule } from "./directives/prevent-scroll.directive" ;
@NgModule ({
bootstrap : [AppComponent],
declarations : [
AppComponent,
GridConditionalRowSelectorsComponent
],
imports : [
BrowserModule,
BrowserAnimationsModule,
FormsModule,
IgxPreventDocumentScrollModule,
IgxGridModule,
IgxCheckboxModule
],
providers : [],
entryComponents : [],
schemas : []
})
export class AppModule {}
ts コピー import { Component } from '@angular/core' ;
import { IRowSelectionEventArgs } from 'igniteui-angular' ;
import { DATA } from '../../data/customers' ;
@Component ({
selector : 'app-grid-conditional-row-selectors' ,
styleUrls : ['./grid-conditional-row-selectors-sample.component.scss' ],
templateUrl : 'grid-conditional-row-selectors-sample.component.html'
})
export class GridConditionalRowSelectorsComponent {
public data: any [];
public disabledCollection: string [] = [];
constructor ( ) {
this .data = DATA;
this .data.filter(dataItem => dataItem.ID.indexOf('A' ) === -1 ).map((item ) => {
this .disabledCollection.push(item.ID);
});
}
public isSelectionAllowed (newRowID: string ) {
return this .disabledCollection.indexOf(newRowID) === -1 ;
}
public onRowSelect (event: IRowSelectionEventArgs ) {
if (!event.added.length && event.removed.length) {
return ;
}
const originalAddedLength = event.added.length;
event.newSelection = event.newSelection.filter(x => this .isSelectionAllowed(x));
if (event.newSelection.length
&& !event.newSelection.filter(x => event.oldSelection.indexOf(x) === -1 ).length
&& originalAddedLength > 1 ) {
event.newSelection = [];
}
}
}
ts コピー <div class ="grid__wrapper" >
<igx-grid [igxPreventDocumentScroll ]="true" [data ]="data" height ="510px" width ="100%" cellSelection ="none" primaryKey ="ID" rowSelection ="multiple" (rowSelectionChanging )="onRowSelect($event)" >
<igx-paginator > </igx-paginator >
<igx-column field ="ContactName" width ="20%" > </igx-column >
<igx-column field ="Country" width ="20%" > </igx-column >
<igx-column field ="City" width ="20%" > </igx-column >
<igx-column field ="PostalCode" width ="20%" > </igx-column >
<igx-column field ="CompanyName" width ="20%" > </igx-column >
<ng-template igxRowSelector let-rowContext >
<div class ="row-selector" >
<igx-checkbox [readonly ]="true" [checked ]="rowContext.selected"
[disabled ]="!isSelectionAllowed(rowContext.rowID)"
[aria-label ]="rowContext.selected ? 'Deselect row' : 'Select row'" >
</igx-checkbox >
</div >
</ng-template >
</igx-grid >
</div >
html コピー .grid__wrapper {
margin : 0 auto;
padding : 16px ;
}
.row-selector {
display : flex;
align-items : center;
justify-content : center;
min-width : calc(1.25rem + (1.5rem * 2 ));
}
scss コピー
API リファレンス
その他のリソース
コミュニティに参加して新しいアイデアをご提案ください。