Web Components Tree Grid 検索フィルター
Web Components Tree Grid の Ignite UI for Web Components 検索フィルター機能を使用すると、データのコレクション内の値を検索するプロセスが可能になります。この機能のセットアップが簡単になり、検索入力ボックス、ボタン、キーボード ナビゲーション、その他の便利な機能を使用して実装できるため、ユーザー エクスペリエンスがさらに向上します。ブラウザーにはネイティブなコンテンツ検索機能がありますが、ほとんどの場合で IgcTreeGrid は表示範囲外の行列を仮想化します。そのため、ネイティブ ブラウザー検索は DOM の一部でないため仮想化セルでデータを検索できません。IgcTreeGrid では、Web Components Material テーブル ベースのグリッドの拡張により、検索 API を使用した仮想コンテンツの検索が可能です。
Web Components 検索の例
次の例は、すべての列と行を検索できる検索入力ボックスと、各列の特定のフィルタリング オプションを備えた IgcTreeGrid を表しています。
Web Components 検索の使用
Tree Grid のセットアップ
グリッドを作成してからデータをバインドします。コンポーネントにカスタム スタイルも追加しました。
<igc-tree-grid id="treeGrid" auto-generate="false" primary-key="ID" foreign-key="ParentID" allow-filtering="true" height="100%" width="100%">
<igc-column field="Name" data-type="string" sortable="true"></igc-column>
<igc-column field="ID" data-type="number" sortable="true"></igc-column>
<igc-column field="Title" data-type="string" sortable="true"></igc-column>
<igc-column field="Age" data-type="number" sortable="true"></igc-column>
<igc-column field="HireDate" data-type="date" sortable="true"></igc-column>
</igc-tree-grid>
private treeGrid: IgcTreeGridComponent;
constructor() {
this.treeGrid = document.getElementById('treeGrid') as IgcTreeGridComponent;
this.treeGrid.data = new EmployeesFlatData();
}
では、IgcTreeGrid の検索 API の準備をしましょう。検索したテキストの保存、また大文字小文字の区別や完全一致 (またはそのいずれか) に使用するプロパティを作成できます。
private treeGrid: IgcTreeGridComponent;
private searchBox: IgcInputComponent;
private icon: IgcIconComponent;
private nextIconButton: IgcIconButtonComponent;
private prevIconButton: IgcIconButtonComponent;
private caseSensitiveChip: IgcChipComponent;
private exactMatchChip: IgcChipComponent;
Web Components 検索ボックス入力
検索入力を作成します。input 要素を取得することで、その現在の値を取得できます。これにより、IgcTreeGrid の FindNext メソッドと FindPrev メソッドを使用して、SearchText が出現するすべての箇所をハイライト表示し、(呼び出したメソッドに応じて) 次 / 前の箇所にスクロールできるようになります。
FindNext と FindPrev メソッドの両方に 3 つの引数があります。
Text: string (検索テキスト)- (オプション)
CaseSensitive: boolean (検索で完全一致するかどうか、デフォルト値は false)。 - (オプション)
ExactMatch: boolean (検索で完全一致するかどうか、デフォルト値は false)。
完全一致で検索した場合、検索 API は SearchText と完全一致 (大文字小文字の区別を含む) するセル値のみ結果としてハイライト表示します。たとえば、文字列 ‘software’ と ‘Software’ は大文字小文字を区別しない場合は完全一致となります。
上記のメソッドは number 値を返します (IgcTreeGrid で指定した文字列が含まれる回数)。
<igc-input id="searchBox" name="searchBox">
</igc-input>
constructor() {
this.searchBox = document.getElementById('searchBox') as IgcInputComponent;
this.caseSensitiveChip = document.getElementById('caseSensitiveChip') as IgcChipComponent;
this.exactMatchChip = document.getElementById('exactMatchChip') as IgcChipComponent;
}
public nextSearch() {
this.treeGrid.findNext(this.searchBox.value, this.caseSensitiveChip.selected, this.exactMatchChip.selected);
}
検索ボタンの追加
ボタンの各クリック イベント ハンドラー内で FindNext と FindPrev メソッドを呼び出して検索や検索結果をナビゲーションするためのボタンを作成します。
<igc-icon-button id="prevIconBtn" variant="flat" name="prev" collection="material" ></igc-icon-button>
<igc-icon-button id="nextIconBtn" variant="flat" name="next" collection="material"></igc-icon-button>
constructor() {
this.nextIconButton = document.getElementById('nextIconBtn') as IgcIconButtonComponent;
this.prevIconButton = document.getElementById('prevIconBtn') as IgcIconButtonComponent;
this.nextIconButton.addEventListener("click", this.nextSearch);
this.prevIconButton.addEventListener("click", this.prevSearch);
}
public prevSearch() {
this.treeGrid.findPrev(this.searchBox.value, this.caseSensitiveChip.selected, this.exactMatchChip.selected);
}
public nextSearch() {
this.treeGrid.findNext(this.searchBox.value, this.caseSensitiveChip.selected, this.exactMatchChip.selected);
}
キーボード検索の追加
ユーザーは矢印キーと ENTER キーで結果を移動できます。PreventDefault メソッドのデフォルト キャレットの移動を防止する検索入力の keydown イベントを処理し、ユーザーが押したキーに基づいて FindNext/FindPrev メソッドを呼び出します。
<igc-input id="searchBox" name="searchBox">
</igc-input>
constructor() {
this.searchBox = document.getElementById('searchBox') as IgcInputComponent;
this.searchBox.addEventListener("keydown", (evt) => { this.onSearchKeydown(evt); });
this.searchBox.addEventListener("igcInput", (evt) => {
this.treeGrid.findNext(evt.detail, this.caseSensitiveChip.selected, this.exactMatchChip.selected);
});
}
public onSearchKeydown(evt: KeyboardEvent) {
if (evt.key === 'Enter' || evt.key === 'ArrowDown') {
evt.preventDefault();
this.treeGrid.findNext(this.searchBox.value, this.caseSensitiveChip.selected, this.exactMatchChip.selected);
} else if (evt.key === 'ArrowUp') {
evt.preventDefault();
this.treeGrid.findPrev(this.searchBox.value, this.caseSensitiveChip.selected, this.exactMatchChip.selected);
}
}
大文字と小文字の区別と完全一致
Now let’s allow the user to choose whether the search should be case sensitive and/or by an exact match. For this purpose we can use simple selectable Chips and bind to the igcSelect event to determine when the user interacts with them.
<igc-chip selectable="true" id="caseSensitiveChip">Case Sensitive</igc-chip>
<igc-chip selectable="true" id="exactMatchChip">Exact Match</igc-chip>
constructor() {
this.caseSensitiveChip = document.getElementById('caseSensitiveChip') as IgcChipComponent;
this.exactMatchChip = document.getElementById('exactMatchChip') as IgcChipComponent;
this.caseSensitiveChip.addEventListener("igcSelect", (evt) => {
this.treeGrid.findNext(this.searchBox.value, evt.detail, this.exactMatchChip.selected);
});
this.exactMatchChip.addEventListener("igcSelect", (evt) => {
this.treeGrid.findNext(this.searchBox.value, this.caseSensitiveChip.selected, evt.detail);
});
}
保持
IgcTreeGrid のフィルターやソート、レコードの追加や削除をする場合を想定します。そのような処理の後、現在の検索が自動的に更新されて SearchText に一致するテキストが保持されます。更に検索がページングで動作し、IgcTreeGrid の PerPage プロパティの変更時もハイライト表示が保持されます。
アイコンの追加
その他のコンポーネントを使用するためにユーザー インターフェイスを作成し、検索バー全体のデザインを向上します。検索入力の左側に検索または削除アイコン、検索オプションのチップ、右側にはマテリアル デザイン アイコンと Ripple スタイルのボタンを組み合わせたナビゲーションを表示できます。
import { defineComponents, IgcInputComponent, IgcChipComponent, IgcIconComponent, IgcIconButtonComponent, registerIconFromText } from "igniteui-webcomponents";
defineComponents(IgcInputComponent, IgcChipComponent, IgcIconComponent, IgcIconButtonComponent);
テンプレートを新しいコンポーネントで更新します。
Input 内のすべてのコンポーネントをラップします。左側で検索と 削除/クリア アイコンを切り替えます (検索入力が空かどうかに基づきます)。中央に入力を配置します。更に削除アイコンがクリックされたときに SearchText を更新し、IgcTreeGrid.findNext の ClearSearch メソッドを呼び出してハイライト表示をクリアします。
<igc-input id="searchBox" name="searchBox">
<igc-icon id="icon" slot="prefix" name="search" collection="material"></igc-icon>
<div slot="suffix">
<igc-chip selectable="true" id="caseSensitiveChip">Case Sensitive</igc-chip>
<igc-chip selectable="true" id="exactMatchChip">Exact Match</igc-chip>
</div>
<div slot="suffix">
<igc-icon-button id="prevIconBtn" variant="flat" name="prev" collection="material" ></igc-icon-button>
<igc-icon-button id="nextIconBtn" variant="flat" name="next" collection="material"></igc-icon-button>
</div>
</igc-input>
constructor() {
const prevIconText = "<svg width='24' height='24' viewBox='0 0 24 24'><path d='M15.41 7.41 14 6l-6 6 6 6 1.41-1.41L10.83 12z'></path></svg>";
const nextIconText = "<svg width='24' height='24' viewBox='0 0 24 24'><path d='M10 6 8.59 7.41 13.17 12l-4.58 4.59L10 18l6-6z'></path></svg>";
const searchIconText = "<svg width='24' height='24' viewBox='0 0 24 24'><path d='M15.5 14h-.79l-.28-.27C15.41 12.59 16 11.11 16 9.5 16 5.91 13.09 3 9.5 3S3 5.91 3 9.5 5.91 16 9.5 16c1.61 0 3.09-.59 4.23-1.57l.27.28v.79l5 4.99L20.49 19l-4.99-5zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z' /></svg>";
const clearIconText = "<svg width='24' height='24' viewBox='0 0 24 24' title='Clear'><path d='M19 6.41 17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z'></path></svg>";
registerIconFromText('prev', prevIconText, 'material');
registerIconFromText('next', nextIconText, 'material');
registerIconFromText('search', searchIconText, 'material');
registerIconFromText('clear', clearIconText, 'material');
this.icon = document.getElementById('icon') as IgcIconComponent;
this.searchBox = document.getElementById('searchBox') as IgcInputComponent;
this.searchBox.addEventListener('igcInput', (evt) => {
this.icon.name = evt.detail ? 'clear' : 'search';
});
this.icon.addEventListener('click', this.clearSearch);
}
public clearSearch() {
this.searchBox.value = '';
this.icon.name = 'search';
this.treeGrid.clearSearch();
}
Input 内のすべてのコンポーネントをラップします。左側で検索と 削除/クリア アイコンを切り替えます (検索入力が空かどうかに基づきます)。中央に入力を配置します。更に削除アイコンがクリックされたときに SearchText を更新し、IgcTreeGrid の ClearSearch メソッドを呼び出してハイライト表示をクリアします。
- 検索結果の表示
<igc-input id="searchBox" name="searchBox">
<igc-icon id="icon" slot="prefix" name="search" collection="material"></igc-icon>
<div slot="suffix">
<igc-chip selectable="true" id="caseSensitiveChip">Case Sensitive</igc-chip>
<igc-chip selectable="true" id="exactMatchChip">Exact Match</igc-chip>
</div>
<div slot="suffix">
<igc-icon-button id="prevIconBtn" variant="flat" name="prev" collection="material" ></igc-icon-button>
<igc-icon-button id="nextIconBtn" variant="flat" name="next" collection="material"></igc-icon-button>
</div>
</igc-input>
constructor() {
this.caseSensitiveChip = document.getElementById('caseSensitiveChip') as IgcChipComponent;
this.exactMatchChip = document.getElementById('exactMatchChip') as IgcChipComponent;
this.caseSensitiveChip.addEventListener('igcSelect', (evt) => {
this.treeGrid.findNext(this.searchBox.value, evt.detail, this.exactMatchChip.selected);
});
this.exactMatchChip.addEventListener('igcSelect', (evt) => {
this.treeGrid.findNext(this.searchBox.value, this.caseSensitiveChip.selected, evt.detail);
});
}
- 以下は
CaseSensitiveとExactMatchプロパティを切り替えるチップを表示する方法です。チェックボックスの代わりにスタイリッシュなチップを 2 つ表示します。チップをクリックすると、どちらのチップがクリックされたかによって各ハンドラーを呼び出します。
<igx-suffix *ngIf="searchText.length > 0">
<div class="resultsText" *ngIf="@@igObjectRef.lastSearchInfo">
<span *ngIf="@@igObjectRef.lastSearchInfo.matchInfoCache.length > 0">
{{ @@igObjectRef.lastSearchInfo.activeMatchIndex + 1 }} of {{ @@igObjectRef.lastSearchInfo.matchInfoCache.length }} results
</span>
<span *ngIf="@@igObjectRef.lastSearchInfo.matchInfoCache.length == 0">
No results
</span>
</div>
</igx-suffix>
<igc-suffix >
<div class="resultsText">
<span id="results">
</span>
</div>
</igc-suffix>
public prevSearch() {
this.treeGrid.findPrev(this.searchBox.value, this.caseSensitiveChip.selected, this.exactMatchChip.selected);
}
public nextSearch() {
this.treeGrid.findNext(this.searchBox.value, this.caseSensitiveChip.selected, this.exactMatchChip.selected);
}
既知の問題と制限
| 制限 | 説明 |
|---|---|
| テンプレートを使用したセル内の検索 | 検索機能のハイライト表示が、デフォルトのセルテンプレートに対してのみ機能する問題。カスタム セル テンプレートを含む列がある場合、ハイライト表示が機能しないため、列フォーマッタなどの代替アプローチを使用するか、Searchable (検索可能な) プロパティを false に設定します。 |
| リモート仮想化 | リモート仮想化の使用時に検索が正しく動作しません。 |
| セル テキストが切れる問題 | セル内のテキストが長すぎるために検索テキストが省略記号によって切れている場合も、セルまでスクロールして一致カウントに含まれますが、ハイライト表示はされません。 |
API リファレンス
IgcTreeGrid
Column
その他のリソース
コミュニティに参加して新しいアイデアをご提案ください。