Angular Grid の選択
Ignite UI for Angular Grid を使用して、さまざまなイベント、豊富な API、または単一選択などのシンプルなマウス操作でデータを簡単に選択できます。
Angular Grid 選択の例
以下のサンプルは、Grid のセル選択 動作の 3 つのタイプを示します。以下のボタンを使用して、使用可能な各選択モードを有効にします。Snackbar メッセージ ボックスで各ボタンの操作について簡単に説明します。
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 { GridCellSelectionComponent } from "./grid/grid-cellSelection-sample/grid-cellSelection.component" ;
import {
IgxGridModule,
IgxSwitchModule,
IgxIconModule,
IgxButtonGroupModule,
IgxAvatarModule,
IgxSnackbarModule
} from "igniteui-angular" ;
import { IgxPreventDocumentScrollModule } from "./directives/prevent-scroll.directive" ;
@NgModule ({
bootstrap : [AppComponent],
declarations : [
AppComponent,
GridCellSelectionComponent
],
imports : [
BrowserModule,
BrowserAnimationsModule,
FormsModule,
IgxPreventDocumentScrollModule,
IgxGridModule,
IgxSnackbarModule
],
providers : [],
entryComponents : [],
schemas : []
})
export class AppModule {}
ts コピー import { Component, OnInit, ViewChild } from '@angular/core' ;
import { GridSelectionMode, IgxGridComponent, IgxSnackbarComponent } from 'igniteui-angular' ;
import { DATA } from '../../data/nwindData' ;
@Component ({
selector : 'app-grid-cell-selection' ,
styleUrls : ['./grid-cellSelection.component.scss' ],
templateUrl : 'grid-cellSelection.component.html'
})
export class GridCellSelectionComponent implements OnInit {
@ViewChild ('grid' , { static : true }) public grid: IgxGridComponent;
@ViewChild (IgxSnackbarComponent, { static : true }) public snackbar: IgxSnackbarComponent;
public data: any [];
public selection = true ;
public selectionMode: GridSelectionMode = 'multiple' ;
public selectionModes = [];
constructor ( ) { }
public ngOnInit(): void {
this .data = DATA;
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 }
];
this .snackbar.autoHide = false ;
this .snackbar.open();
}
public selectCellSelectionMode (args ) {
this .selectionMode = this .selectionModes[args.index].label;
this .snackbar.open();
}
}
ts コピー <div class ="grid__wrapper" >
<span class ="button-group-wrapper" >
<igx-buttongroup [values ]="selectionModes" (selected )="selectCellSelectionMode($event)" > </igx-buttongroup >
</span >
<igx-grid [igxPreventDocumentScroll ]="true" #grid [data ]="data" [height ]="'600px'" [width ]="'100%'" [cellSelection ]="selectionMode" >
<igx-column [field ]="'ProductID'" > </igx-column >
<igx-column [field ]="'ProductName'" > </igx-column >
<igx-column [field ]="'UnitsInStock'" [dataType ]="'number'" > </igx-column >
<igx-column [field ]="'UnitPrice'" [dataType ]="'number'" > </igx-column >
<igx-column [field ]="'Discontinued'" [dataType ]="'boolean'" >
</igx-column >
<igx-column [field ]="'OrderDate'" [dataType ]="'date'" > </igx-column >
</igx-grid >
</div >
<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 > This is the default selection behavior.</b > </li >
<li > Click on a cell and while pressing the mouse key perform drag action.
</li >
<li > Select a cell and press Shift + Arrow down key, this will start range selection as well.</li >
</ul >
</ng-container >
<ng-container *ngIf ="selectionMode === 'single'" >
<ul >
<li > <b > Now you can select only one cell within the grid.</b > </li >
<li > On single cell click the previous selection state will be cleared.</li >
<li > The mouse drag will not work and instead of selecting a cell, this will make default text selection.</li >
</ul >
</ng-container >
<ng-container *ngIf ="selectionMode === 'none'" >
<ul >
<li > <b > Now you are unable to select a cell while interacting with grid UI.</b > </li >
<li > If you need to select a cell, you can use the grid API methods.</li >
</ul >
</ng-container >
</div >
</igx-snackbar >
html コピー .grid__wrapper {
display : flex;
justify-content : space-between;
margin : 16px ;
flex-flow : column;
align-items : flex-start;
position : relative;
}
.button-group-wrapper {
margin-bottom : 16px ;
}
.igx-snackbar {
background : rgba(0 , 0 , 0 , 0.7 );
}
.container {
display : flex;
igx-icon {
margin : 20px ;
}
}
scss コピー
このサンプルが気に入りましたか? 完全な Ignite UI for Angularツールキットにアクセスして、すばやく独自のアプリの作成を開始します。無料でダウンロードできます。
Angular Grid 選択のオプション
Ignite UI for Angular Grid コンポーネントは、行選択 、セル選択 、列選択 の 3 つの選択モードを提供します。デフォルトでは、Grid で複数セル選択 モードのみが有効になっています。選択モードを変更/有効化するには、rowSelection
、cellSelection
、または selectable
プロパティを使用できます。
Angular 行選択
rowSelection
プロパティを使用すると、次のオプションを指定できます。
none - Grid の行選択が無効になります。
single - Grid 内の 1 行のみの選択が利用可能になります。
multiple - 複数行の選択は、ctrl + click 、space キー を押して行セレクター
を使用することにより、複数行の選択が可能になります。
詳細については、行選択トピック を参照してください。
Angular セル選択
以下のオプションは、プロパティ cellSelection
で指定できます。
none - Grid のセル選択が無効になります。
single - Grid 内の 1 セルのみ選択が利用可能になります。
multiple - Grid の選択のデフォルト状態です。Multi - セルの選択は、マウスの左ボタンを連続してクリックした後、マウスをセル上にドラッグすることで利用できます。
詳細については、セル選択トピック を参照してください。
Angular 列選択
selectable
プロパティ を使用して、列 ごとに以下のオプションを指定できます。
False - 対応する列選択は Grid に対して無効になります。
True - 対応する列選択は Grid に対して有効になります。
以下の 3 つのバリエーションがあります。
単一選択 - 列セルをマウス クリック します。
複数列の選択 - Ctrl キーを押しながら列セルをマウス クリック します。
列の範囲選択 - Shift キーを押しながら + マウス クリック 、その間のすべての列が選択されます。
詳細については、列選択トピック を参照してください。
グリッドのコンテキスト メニュー
contextMenu
イベントは、カスタム コンテキスト メニューを追加して、IgxGrid での作業をアシストします。グリッドの本体を右クリック すると、イベントはトリガーされたセルを放出します。コンテキスト メニュー は、放出されたセルで動作します。
複数セルの選択 がある場合、選択したセルが複数セルの選択領域にあるかどうかをチェックするロジックを配置します。その場合、選択したセルの値も出力します。
基本的に、メイン関数は次のようになります。
public rightClick (eventArgs: any ) {
eventArgs.event.preventDefault();
this .multiCellArgs = {};
if (this .multiCellSelection) {
const node = eventArgs.cell.selectionNode;
const isCellWithinRange = this .grid1.getSelectedRanges().some(range => {
if (node.column >= range.columnStart &&
node.column <= range.columnEnd &&
node.row >= range.rowStart &&
node.row <= range.rowEnd) {
return true ;
}
return false ;
})
if (isCellWithinRange) {
this .multiCellArgs = { data : this .multiCellSelection.data };
}
}
this .contextmenuX = eventArgs.event.clientX;
this .contextmenuY = eventArgs.event.clientY;
this .clickedCell = eventArgs.cell;
this .contextmenu = true ;
}
typescript
以下はコンテキストメニューの機能です。
選択したセルの値のコピー。
選択したセルの dataRow のコピー。
選択したセルが複数セルの選択範囲 内にある場合、選択したすべてのデータをコピーします
public copySelectedCellData (event ) {
const selectedData = { [this .cell.column.field]: this .cell.value };
this .copyData(JSON .stringify({ [this .cell.column.field]: this .cell.value }));
this .onCellValueCopy.emit({ data : selectedData });
}
public copyRowData (event ) {
const selectedData = this .cell.row.data ;
this .copyData(JSON .stringify(this .cell.row.data));
this .onCellValueCopy.emit({ data : selectedData });
}
public copySelectedCells (event ) {
const selectedData = this .selectedCells.data;
this .copyData(JSON .stringify(selectedData));
this .onCellValueCopy.emit({ data : selectedData });
}
typescript
IgxGrid はコピーされたデータを取得し、コンテナー要素に貼り付けます。
グリッドとコンテキスト メニューを組み合わせるために使用するテンプレート:
<div class ="wrapper" >
<div class ="grid__wrapper" (window:click )="disableContextMenu()" >
<igx-grid #grid1 [data ]="data" [autoGenerate ]="false" height ="500px" width ="100%"
(contextMenu )="rightClick($event)" (rangeSelected )="getCells($event)"
(selected )="cellSelection($event)" >
</igx-grid >
<div *ngIf ="contextmenu==true" >
<contextmenu [x ]="contextmenuX" [y ]="contextmenuY" [cell ]="clickedCell" [selectedCells ]="multiCellArgs" (onCellValueCopy )="copy($event)" >
</contextmenu >
</div >
</div >
<div class ="selected-data-area" >
<div >
<pre > {{copiedData}}</pre >
</div >
</div >
</div >
html
複数のセルを選択し、マウスの右
ボタンを押します。コンテキストメニューが表示され、セルデータのコピー
を選択すると、選択したデータが右側の空のボックスに表示されます。
結果:
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 { GridContextmenuSampleComponent } from "./grid/grid-contextmenu-sample/grid-contextmenu-sample.component" ;
import { ContextmenuComponent } from "./grid/grid-contextmenu-sample/contextmenu/contextmenu.component" ;
import { IgxPreventDocumentScrollModule } from "./directives/prevent-scroll.directive" ;
@NgModule ({
bootstrap : [AppComponent],
declarations : [
AppComponent,
GridContextmenuSampleComponent,
ContextmenuComponent
],
imports : [
BrowserModule,
BrowserAnimationsModule,
FormsModule,
IgxPreventDocumentScrollModule,
IgxGridModule
],
providers : [],
entryComponents : [],
schemas : []
})
export class AppModule {}
ts コピー import { Component, OnInit, ViewChild} from '@angular/core' ;
import { DefaultSortingStrategy, IgxGridComponent, SortingDirection} from 'igniteui-angular' ;
import { DATA } from '../../data/nwindData' ;
@Component ({
selector : 'app-grid-contextmenu-sample' ,
styleUrls : ['./grid-contextmenu-sample.component.scss' ],
templateUrl : './grid-contextmenu-sample.component.html'
})
export class GridContextmenuSampleComponent implements OnInit {
@ViewChild ('grid1' , { read : IgxGridComponent, static : true })
public grid1: IgxGridComponent;
public data: any [];
public contextmenu = false ;
public contextmenuX = 0 ;
public contextmenuY = 0 ;
public clickedCell = null ;
public copiedData;
public multiCellSelection: { data : any []} = { data : []};
public multiCellArgs;
constructor ( ) {
}
public ngOnInit(): void {
this .data = DATA;
this .grid1.sortingExpressions = [
{
dir : SortingDirection.Asc, fieldName : 'ProductName' ,
ignoreCase : true , strategy : DefaultSortingStrategy.instance()
}
];
}
public formatDate (val: Date ) {
return new Intl .DateTimeFormat('en-US' ).format(val);
}
public rightClick (eventArgs: any ) {
eventArgs.event.preventDefault();
this .multiCellArgs = {};
if (this .multiCellSelection) {
const node = eventArgs.cell.selectionNode;
const isCellWithinRange = this .grid1.getSelectedRanges().some((range ) => {
if (node.column >= range.columnStart &&
node.column <= range.columnEnd &&
node.row >= range.rowStart &&
node.row <= range.rowEnd) {
return true ;
}
return false ;
});
if (isCellWithinRange) {
this .multiCellArgs = { data : this .multiCellSelection.data };
}
}
this .contextmenuX = eventArgs.event.clientX;
this .contextmenuY = eventArgs.event.clientY;
this .clickedCell = eventArgs.cell;
this .contextmenu = true ;
}
public disableContextMenu ( ) {
if (this .contextmenu) {
this .multiCellSelection = undefined ;
this .multiCellArgs = undefined ;
this .contextmenu = false ;
}
}
public getCells (event ) {
this .multiCellSelection = {
data : this .grid1.getSelectedData()
};
}
public copy (event ) {
this .copiedData = JSON .stringify(event.data, null , 2 );
if (this .multiCellSelection) {
this .multiCellSelection = undefined ;
this .multiCellArgs = undefined ;
this .grid1.clearCellSelection();
}
}
public cellSelection (event ) {
this .contextmenu = false ;
}
}
ts コピー <div class ="wrapper" >
<div class ="grid__wrapper" (window:click )="disableContextMenu()" >
<igx-grid [igxPreventDocumentScroll ]="true" #grid1 [data ]="data" [autoGenerate ]="false" height ="500px" width ="100%"
(contextMenu )="rightClick($event)" (rangeSelected )="getCells($event)"
(selected )="cellSelection($event)" >
<igx-column field ="ProductID" header ="Product ID" [headerClasses ]="'prodId'" >
</igx-column >
<igx-column field ="ProductName" header ="Product Name" [dataType ]="'string'" [sortable ]="true" >
</igx-column >
<igx-column field ="UnitsInStock" header ="UnitsInStock" dataType ="number" [sortable ]="true" >
<ng-template igxCell let-cell ="cell" let-val let-row >
${{val}}
</ng-template >
</igx-column >
<igx-column field ="OrderDate" [dataType ]="'date'" [formatter ]="formatDate" [sortable ]="true" >
</igx-column >
<igx-column field ="Discontinued" header ="Discontinued" [dataType ]="'boolean'" >
</igx-column >
</igx-grid >
<div *ngIf ="contextmenu" >
<app-contextmenu [x ]="contextmenuX" [y ]="contextmenuY" [cell ]="clickedCell" [selectedCells ]="multiCellArgs" (cellValueCopy )="copy($event)" >
</app-contextmenu >
</div >
</div >
<div class ="selected-data-area" >
<div >
<pre > {{copiedData}}</pre >
</div >
</div >
</div >
html コピー
.grid__wrapper {
width : 50% ;
}
.selected-data-area {
overflow-y : auto;
max-height : 500px ;
width : 50% ;
box-shadow : 0 1px 3px 0 rgba(0 , 0 , 0 , 0.2 ), 0 1px 1px 0 rgba(0 , 0 , 0 , 0.14 ), 0 2px 1px -1px rgba(0 , 0 , 0 , 0.12 );
margin-left : 10px ;
}
.wrapper {
margin : 10px ;
display : flex;
justify-content : space-evenly;
}
scss コピー
既知の問題と制限
IE11 で選択を有効にした Grid コンポーネントを使用するには、Angular アプリケーションの polyfill.ts に配列ポリフィルを明示的にインポートする必要があります。IE11 は、バージョン 13.0.0 でサポートされなくなりました。
import 'core-js/es7/array' ;
typescript
グリッドに primaryKey
が設定されておらず、リモート データ シナリオが有効になっている場合 (ページング、ソート、フィルタリング、スクロール時に、グリッドに表示されるデータを取得するためのリモート サーバーへのリクエストがトリガーされる場合)、データ要求が完了すると、行は次の状態を失います:
Row Selection
Row Expand/collapse
Row Editing
Row Pinning
API リファレンス
その他のリソース
コミュニティに参加して新しいアイデアをご提案ください。