Close
Angular React Web Components Blazor Web Components
Premium

Web Components Hierarchical Grid セル編集

Web Components Hierarchical Grid の Ignite UI for Web Components セル編集機能は、Web Components Hierarchical Grid コンポーネント内の個々のセルのコンテンツの優れたデータ操作機能を提供し、React CRUD 操作用の強力な API を備えています。これはスプレッドシート、データ テーブル、データ グリッドなどのアプリの基本的な機能であり、ユーザーが特定のセル内のデータを追加、編集、更新できるようにします。 デフォルトでは、Ignite UI for Web Components の Grid がセル編集に使用されます。また、デフォルトのセル編集テンプレートにより、列のデータ型 「Top of Form」 に基づいて異なるエディターが存在します。

さらに、データ更新アクション用の独自のカスタム テンプレートを定義したり、変更をコミット/破棄したりするためのデフォルトの動作をオーバーライドすることもできます。

Web Components Hierarchical Grid セル編集と編集テンプレートの例

セルの編集

UI を介した編集

編集可能なセルがフォーカスされたときに以下のいずれかの方法で特定のセルを編集モードにすることができます。

  • ダブル クリック
  • シングル クリック - 以前選択したセルが編集モードで現在選択したセルが編集可能な場合のみ、シングル クリックで編集モードに入ります。以前選択したセルが編集モードではない場合、編集モードに入らずにシングル クリックでセルを選択します。
  • ENTER キーの押下
  • F2 キーの押下

変更をコミットしない場合も以下の方法で編集モードを終了できます。

  • Escape キーの押下;
  • ソート、フィルターリング、検索、非表示操作の実行時。

変更をコミットしない場合も以下の方法で編集モードを終了できます。

  • ENTER キーの押下
  • F2 キーの押下
  • TAB キーの押下
  • 他のセルをシングル クリック - IgcHierarchicalGrid で他のセルをクリックしたときに変更がサブミットされます。
  • その他の操作 (ページング、サイズ変更、ピン固定、移動など) は、編集モードを終了して変更を送信します。

セルは、垂直/水平方向へのスクロールや IgcHierarchicalGrid 以外をクリックした場合も編集モードのままです。セル編集と行編集両方で有効です。

API を介した編集

プライマリキーが定義されている場合のみ IgcHierarchicalGrid API でもセル値を変更することができます。

public updateCell() {
    this.hierarchicalGrid.updateCell(newValue, rowID, 'Age');
}

セルを更新するその他の方法として CellUpdate メソッドで直接更新する方法があります。

public updateCell() {
    const cell = this.hierarchicalGrid.getCellByColumn(rowIndex, 'ReorderLevel');
    // You can also get cell by rowID if primary key is defined
    // cell = this.hierarchicalGrid.getCellByKey(rowID, 'ReorderLevel');
    cell.update(70);
}

セル編集テンプレート

デフォルトのセル編集テンプレートの詳細については、編集トピックを参照してください。

セルに適用されるカスタム テンプレートを提供する場合は、そのテンプレートをセル自体またはそのヘッダーに渡すことができます。まず、通常どおりに列を作成します。

<igc-column
    field="Age"
    data-type="string"
    editable="true"
    id="column1">
</igc-column>

and pass the templates to this column in the index.ts file:


constructor() {
    var hierarchicalGrid = document.getElementById('hierarchicalGrid') as IgcHierarchicalGridComponent;
    var column1 = document.getElementById('column1') as IgcColumnComponent;

    hierarchicalGrid.data = this.singersData;
    column1.inlineEditorTemplate = this.webGridCellEditCellTemplate;
}

public webGridCellEditCellTemplate = (ctx: IgcCellTemplateContext) => {
    let cellValues: any = [];
    let uniqueValues: any = [];
    for(const i of (this.singersData as any)){
        const field: string = ctx.cell.column.field;
        if(uniqueValues.indexOf(i[field]) === -1 )
        {
            cellValues.push(html`<igc-select-item value=${i[field]}>${(i[field])}</igc-select-item>`);
            uniqueValues.push(i[field]);
        }
    }
    return html`
    <igc-select style="width:100%; height:100%" size="large" @igcChange=${(e: any) => ctx.cell.editValue = e.detail.value}>
            ${cellValues}
    </igc-select>
    `;
}

そして、テンプレートを index.ts ファイルのこの列に渡します。

CRUD 操作

CRUD 操作を実行した場合、filteringsortinggrouping などのパイプが再適用されるため、ビューが自動的に更新されることに注意してください。

IgcHierarchicalGrid は基本的な CRUD 操作のための簡易な API を提供します。

新しいレコードの追加

IgcHierarchicalGrid コンポーネントは、提供したデータをデータ ソースに追加する AddRow メソッドを公開します。

public addRow() {
    // Adding a new record
    // Assuming we have a `getNewRecord` method returning the new row data
    const record = this.getNewRecord();
    this.hierarchicalGrid.addRow(record);
}

データを Hierarchical Grid で更新

Hierarchical Grid のデータ更新は、グリッドで PrimaryKey が定義されている場合のみ UpdateRowUpdateCell メソッドで行うことができます。セルと行の値またはそのいずれかを各 update メソッドで直接更新できます。

// Updating the whole row
this.hierarchicalGrid.updateRow(newData, this.selectedCell.cellID.rowID);

// Just a particular cell through the Grid API
this.hierarchicalGrid.updateCell(newData, this.selectedCell.cellID.rowID, this.selectedCell.column.field);

// Directly using the cell `update` method
this.selectedCell.update(newData);

// Directly using the row `update` method
const row = this.hierarchicalGrid.getRowByKey(rowID);
row.update(newData);

Hierarchical Grid からデータを削除

DeleteRow メソッドは、PrimaryKey が定義されている場合に指定した行のみを削除することに注意してください。

// Delete row through Grid API
this.hierarchicalGrid.deleteRow(this.selectedCell.cellID.rowID);
// Delete row through row object
const row = this.hierarchicalGrid.getRowByIndex(rowIndex);
row.delete();

IgcHierarchicalGrid に関係なく、ボタンのクリックなどのユーザー インタラクションに関連付けできます。

編集イベントでのセル検証

IgcHierarchicalGrid の編集イベントを使用して、ユーザーが IgcHierarchicalGrid を操作する方法を変更できます。

この例では、CellEdit イベントにバインドすることにより、入力されたデータに基づいてセルを検証します。セルの新しい値が事前定義された基準を満たしていない場合、イベントをキャンセルすることでデータソースに到達しないようにします。

最初に必要なことは、グリッドのイベントにバインドすることです。

<igc-hierarchical-grid CellEditScript="HandleCellEdit" />
constructor() {
    var hGrid = document.getElementById('hGrid') as IgcHierarchicalGridComponent;
    this.webHierarchicalGridCellEdit = this.webHierarchicalGridCellEdit.bind(this);
    hGrid.addEventListener("cellEdit", this.webHierarchicalGridCellEdit);
}

CellEdit は、セルのがコミットされる直前に発生します。CellEdit の定義では、アクションを実行する前に特定の列を確認する必要があります。

If the value entered in a cell under the Units On Order column is larger than the available amount (the value under Units in Stock), the editing will be cancelled and the user will be alerted to the cancellation.

public webHierarchicalGridCellEdit(event: CustomEvent<IgcGridEditEventArgs>): void {
    const today = new Date();
    const column = event.detail.column;
    if (column.field === 'Debut') {
        if (event.detail.newValue > today.getFullYear()) {
            event.detail.cancel = true;
            alert('The debut date must be in the past!');
        }
    } else if (column.field === 'LaunchDate') {
        if (event.detail.newValue > today) {
            event.detail.cancel = true;
            alert('The launch date must be in the past!');
        }
    }
}

ここでは、2 つの列を検証しています。ユーザーが従業員の Age (年齢、18 歳未満) または Hire Date (雇用日、将来の日付) に無効な値を設定しようとすると、編集がキャンセルされ (値は送信されません)、エラー メッセージ付きのトースターが表示されます。

スタイル設定

事前定義されたテーマに加えて、利用可能な CSS プロパティを設定することでグリッドをさらにカスタマイズできます。 一部の色を変更したい場合は、最初にグリッドのクラスを設定する必要があります。

<igc-hierarchical-grid class="hierarchicalGrid"></igc-hierarchical-grid>

Then set the related CSS properties for that class:

.hierarchicalGrid {
    --ig-grid-edit-mode-color: #FFA500;
    --ig-grid-cell-active-border-color: #FFA500;
    --ig-grid-cell-editing-background: #add8e6;
}

スタイル設定の例

API リファレンス

IgcHierarchicalGrid
Column
DatePicker

パレットの定義