Blazor Tree Grid セル編集
Blazor Tree Grid の Ignite UI for Blazor セル編集機能は、Blazor Tree Grid コンポーネント内の個々のセルのコンテンツの優れたデータ操作機能を提供し、React CRUD 操作用の強力な API を備えています。これはスプレッドシート、データ テーブル、データ グリッドなどのアプリの基本的な機能であり、ユーザーが特定のセル内のデータを追加、編集、更新できるようにします。 デフォルトでは、Ignite UI for Blazor の Grid がセル編集に使用されます。また、デフォルトのセル編集テンプレートにより、列のデータ型 「Top of Form」 に基づいて異なるエディターが存在します。
さらに、データ更新アクション用の独自のカスタム テンプレートを定義したり、変更をコミット/破棄したりするためのデフォルトの動作をオーバーライドすることもできます。
Blazor Tree Grid セル編集と編集テンプレートの例
using System;
using System.Collections.Generic;
public class EmployeesNestedTreeDataItem
{
public double Age { get; set; }
public string HireDate { get; set; }
public double ID { get; set; }
public string Name { get; set; }
public string Phone { get; set; }
public bool OnPTO { get; set; }
public double ParentID { get; set; }
public string Title { get; set; }
}
public class EmployeesNestedTreeData
: List<EmployeesNestedTreeDataItem>
{
public EmployeesNestedTreeData()
{
this.Add(new EmployeesNestedTreeDataItem()
{
Age = 55,
HireDate = @"2008-03-20",
ID = 1,
Name = @"Johnathan Winchester",
Phone = @"0251-031259",
OnPTO = false,
ParentID = -1,
Title = @"Development Manager"
});
this.Add(new EmployeesNestedTreeDataItem()
{
Age = 42,
HireDate = @"2014-01-22",
ID = 4,
Name = @"Ana Sanders",
Phone = @"(21) 555-0091",
OnPTO = true,
ParentID = -1,
Title = @"CEO"
});
this.Add(new EmployeesNestedTreeDataItem()
{
Age = 49,
HireDate = @"2014-01-22",
ID = 18,
Name = @"Victoria Lincoln",
Phone = @"(071) 23 67 22 20",
OnPTO = true,
ParentID = -1,
Title = @"Accounting Manager"
});
this.Add(new EmployeesNestedTreeDataItem()
{
Age = 61,
HireDate = @"2010-01-01",
ID = 10,
Name = @"Yang Wang",
Phone = @"(21) 555-0091",
OnPTO = false,
ParentID = -1,
Title = @"Localization Manager"
});
this.Add(new EmployeesNestedTreeDataItem()
{
Age = 43,
HireDate = @"2011-06-03",
ID = 3,
Name = @"Michael Burke",
Phone = @"0452-076545",
OnPTO = true,
ParentID = 1,
Title = @"Senior Software Developer"
});
this.Add(new EmployeesNestedTreeDataItem()
{
Age = 29,
HireDate = @"2009-06-19",
ID = 2,
Name = @"Thomas Anderson",
Phone = @"(14) 555-8122",
OnPTO = false,
ParentID = 1,
Title = @"Senior Software Developer"
});
this.Add(new EmployeesNestedTreeDataItem()
{
Age = 31,
HireDate = @"2014-08-18",
ID = 11,
Name = @"Monica Reyes",
Phone = @"7675-3425",
OnPTO = false,
ParentID = 1,
Title = @"Software Development Team Lead"
});
this.Add(new EmployeesNestedTreeDataItem()
{
Age = 35,
HireDate = @"2015-09-17",
ID = 6,
Name = @"Roland Mendel",
Phone = @"(505) 555-5939",
OnPTO = false,
ParentID = 11,
Title = @"Senior Software Developer"
});
this.Add(new EmployeesNestedTreeDataItem()
{
Age = 44,
HireDate = @"2009-10-11",
ID = 12,
Name = @"Sven Cooper",
Phone = @"0695-34 67 21",
OnPTO = true,
ParentID = 11,
Title = @"Senior Software Developer"
});
this.Add(new EmployeesNestedTreeDataItem()
{
Age = 44,
HireDate = @"2014-04-04",
ID = 14,
Name = @"Laurence Johnson",
Phone = @"981-443655",
OnPTO = false,
ParentID = 4,
Title = @"Director"
});
this.Add(new EmployeesNestedTreeDataItem()
{
Age = 25,
HireDate = @"2017-11-09",
ID = 5,
Name = @"Elizabeth Richards",
Phone = @"(2) 283-2951",
OnPTO = true,
ParentID = 4,
Title = @"Vice President"
});
this.Add(new EmployeesNestedTreeDataItem()
{
Age = 39,
HireDate = @"2010-03-22",
ID = 13,
Name = @"Trevor Ashworth",
Phone = @"981-443655",
OnPTO = true,
ParentID = 5,
Title = @"Director"
});
this.Add(new EmployeesNestedTreeDataItem()
{
Age = 44,
HireDate = @"2014-04-04",
ID = 17,
Name = @"Antonio Moreno",
Phone = @"(505) 555-5939",
OnPTO = false,
ParentID = 18,
Title = @"Senior Accountant"
});
this.Add(new EmployeesNestedTreeDataItem()
{
Age = 50,
HireDate = @"2007-11-18",
ID = 7,
Name = @"Pedro Rodriguez",
Phone = @"035-640230",
OnPTO = false,
ParentID = 10,
Title = @"Senior Localization Developer"
});
this.Add(new EmployeesNestedTreeDataItem()
{
Age = 27,
HireDate = @"2016-02-19",
ID = 8,
Name = @"Casey Harper",
Phone = @"0342-023176",
OnPTO = true,
ParentID = 10,
Title = @"Senior Localization"
});
this.Add(new EmployeesNestedTreeDataItem()
{
Age = 25,
HireDate = @"2017-11-09",
ID = 15,
Name = @"Patricia Simpson",
Phone = @"069-0245984",
OnPTO = false,
ParentID = 7,
Title = @"Localization Intern"
});
this.Add(new EmployeesNestedTreeDataItem()
{
Age = 39,
HireDate = @"2010-03-22",
ID = 9,
Name = @"Francisco Chang",
Phone = @"(91) 745 6200",
OnPTO = false,
ParentID = 7,
Title = @"Localization Intern"
});
this.Add(new EmployeesNestedTreeDataItem()
{
Age = 25,
HireDate = @"2018-03-18",
ID = 16,
Name = @"Peter Lewis",
Phone = @"069-0245984",
OnPTO = true,
ParentID = 7,
Title = @"Localization Intern"
});
}
}
csusing System;
using System.Net.Http;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Text;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using IgniteUI.Blazor.Controls; // for registering Ignite UI modules
namespace Infragistics.Samples
{
public class Program
{
public static async Task Main(string[] args)
{
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("app");
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
// registering Ignite UI modules
builder.Services.AddIgniteUIBlazor(
typeof(IgbInputModule),
typeof(IgbPaginatorModule),
typeof(IgbTreeGridModule)
);
await builder.Build().RunAsync();
}
}
}
cs
@using IgniteUI.Blazor.Controls
<div class="container vertical ig-typography">
<div class="container vertical fill">
<IgbTreeGrid
AutoGenerate="false"
Name="treeGrid"
@ref="treeGrid"
Id="treeGrid"
Data="EmployeesNestedTreeData"
PrimaryKey="ID"
AllowFiltering="true"
ForeignKey="ParentID">
<IgbPaginator
PerPage="10">
</IgbPaginator>
<IgbColumn
Field="Name"
DataType="GridColumnDataType.String"
Editable="true"
HasSummary="true">
</IgbColumn>
<IgbColumn
Field="Title"
DataType="GridColumnDataType.String"
Editable="true"
HasSummary="true">
</IgbColumn>
<IgbColumn
Field="Age"
DataType="GridColumnDataType.Number"
Editable="true"
HasSummary="true">
</IgbColumn>
<IgbColumn
Field="HireDate"
DataType="GridColumnDataType.Date"
Editable="true"
HasSummary="true">
</IgbColumn>
<IgbColumn
Field="OnPTO"
DataType="GridColumnDataType.Boolean"
Editable="true"
HasSummary="true"
Width="130px">
</IgbColumn>
</IgbTreeGrid>
</div>
</div>
@code {
protected override async Task OnAfterRenderAsync(bool firstRender)
{
var treeGrid = this.treeGrid;
}
private IgbTreeGrid treeGrid;
private EmployeesNestedTreeData _employeesNestedTreeData = null;
public EmployeesNestedTreeData EmployeesNestedTreeData
{
get
{
if (_employeesNestedTreeData == null)
{
_employeesNestedTreeData = new EmployeesNestedTreeData();
}
return _employeesNestedTreeData;
}
}
}
razor/*
CSS styles are loaded from the shared CSS file located at:
https://static.infragistics.com/xplatform/css/samples/
*/
css
このサンプルが気に入りましたか? 完全な Ignite UI for Blazorツールキットにアクセスして、すばやく独自のアプリの作成を開始します。無料でダウンロードできます。
セルの編集
UI を介した編集
編集可能なセルがフォーカスされたときに以下のいずれかの方法で特定のセルを編集モードにすることができます。
- ダブル クリック
- シングル クリック - 以前選択したセルが編集モードで現在選択したセルが編集可能な場合のみ、シングル クリックで編集モードに入ります。以前選択したセルが編集モードではない場合、編集モードに入らずにシングル クリックでセルを選択します。
- Enter キーの押下
- F2 キーの押下
変更をコミットしない場合も以下の方法で編集モードを終了できます。
- Escape キーの押下;
- ソート、フィルターリング、検索、非表示操作の実行時。
変更をコミットしない場合も以下の方法で編集モードを終了できます。
- Enter キーの押下
- F2 キーの押下
- Tab キーの押下
- 他のセルをシングル クリック -
IgbTreeGrid
で他のセルをクリックしたときに変更がサブミットされます。 - その他の操作 (ページング、サイズ変更、ピン固定、移動など) は、編集モードを終了して変更を送信します。
セルは、垂直/水平方向へのスクロールや IgbTreeGrid 以外をクリックした場合も編集モードのままです。セル編集と行編集両方で有効です。
API を介した編集
プライマリキーが定義されている場合のみ IgbTreeGrid
API でもセル値を変更することができます。
@code {
this.treeGrid.UpdateCell(newValue, rowID, 'ReorderLevel');
}
razor
セルを更新するその他の方法として Cell
の Update
メソッドで直接更新する方法があります。
@code {
private UpdateCell() {
IgbCell cell = this.treeGrid.GetCellByColumn(rowIndex, "Age");
cell.Update(9999);
}
}
razor
セル編集テンプレート
デフォルトのセル編集テンプレートの詳細については、編集トピックを参照してください。
セルに適用されるカスタム テンプレートを提供する場合は、そのテンプレートをセル自体またはそのヘッダーに渡すことができます。まず、通常どおりに列を作成します。
<IgbColumn
Field="Category"
DataType="GridColumnDataType.String"
InlineEditorTemplateScript="WebGridCellEditCellTemplate"
Editable="true"
Name="column1"
@ref="column1">
</IgbColumn>
razor
そしてテンプレートを渡します:
*** In JavaScript ***
igRegisterScript("WebGridCellEditCellTemplate", (ctx) => {
let cellValues = [];
let uniqueValues = [];
for(const i of ctx.cell.grid.data){
const field = 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`<div>
<igc-select position-strategy="fixed" @igcChange=${ e => ctx.cell.editValue = e.detail.value}>
${cellValues}
</igc-select>
</div>`;
}, false);
razor
上記のサンプルは、こちらで参照できます。
using System;
using System.Net.Http;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Text;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using IgniteUI.Blazor.Controls; // for registering Ignite UI modules
namespace Infragistics.Samples
{
public class Program
{
public static async Task Main(string[] args)
{
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("app");
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
// registering Ignite UI modules
builder.Services.AddIgniteUIBlazor(
typeof(IgbInputModule),
typeof(IgbTreeGridModule),
typeof(IgbSelectModule)
);
await builder.Build().RunAsync();
}
}
}
csusing System;
using System.Collections.Generic;
public class RoleplayTreeGridDataItem
{
public double ID { get; set; }
public double ParentID { get; set; }
public string Name { get; set; }
public string Age { get; set; }
public string Alignment { get; set; }
public string Race { get; set; }
public string Class { get; set; }
}
public class RoleplayTreeGridData
: List<RoleplayTreeGridDataItem>
{
public RoleplayTreeGridData()
{
this.Add(new RoleplayTreeGridDataItem()
{
ID = 1,
ParentID = 8,
Name = @"Stredo",
Age = @"244",
Alignment = @"💜 Lawful evil",
Race = @"👩 Human",
Class = @"🎻 Bard"
});
this.Add(new RoleplayTreeGridDataItem()
{
ID = 2,
ParentID = 7,
Name = @"Haluun",
Age = @"40",
Alignment = @"🤍 Unaligned",
Race = @"🧒🏻 Hafling",
Class = @"🙏🏻 Monk"
});
this.Add(new RoleplayTreeGridDataItem()
{
ID = 3,
ParentID = 9,
Name = @"Ivellios",
Age = @"244",
Alignment = @"🧡 Chaotic good",
Race = @"👩 Human",
Class = @"⚔️ Paladin"
});
this.Add(new RoleplayTreeGridDataItem()
{
ID = 4,
ParentID = -1,
Name = @"Tes",
Age = @"35",
Alignment = @"💜 Lawful evil",
Race = @"🎭 Changeling",
Class = @"🧙♂️ Wizard"
});
this.Add(new RoleplayTreeGridDataItem()
{
ID = 5,
ParentID = 3,
Name = @"Kalla",
Age = @"47",
Alignment = @"🤎 Neutral evil",
Race = @"🤖 Warforged",
Class = @"🦹♂️ Sorcerer"
});
this.Add(new RoleplayTreeGridDataItem()
{
ID = 6,
ParentID = 2,
Name = @"Halimath Dundragon",
Age = @"149",
Alignment = @"🤍 Unaligned",
Race = @"🐲 Dragonborn",
Class = @"⚔️ Paladin"
});
this.Add(new RoleplayTreeGridDataItem()
{
ID = 7,
ParentID = 5,
Name = @"Iriphawa",
Age = @"39",
Alignment = @"💛 Lawful neutral",
Race = @"🧝🏻♂️ Half-Elf",
Class = @"🏹 Ranger"
});
this.Add(new RoleplayTreeGridDataItem()
{
ID = 8,
ParentID = 6,
Name = @"Quaf",
Age = @"25",
Alignment = @"💚 Neutral",
Race = @"👩 Human",
Class = @"🥊 Fighter"
});
this.Add(new RoleplayTreeGridDataItem()
{
ID = 9,
ParentID = 10,
Name = @"Rat Scratch",
Age = @"15",
Alignment = @"🤎 Neutral evil",
Race = @"🐡 Locathah",
Class = @"🍁 Druid"
});
this.Add(new RoleplayTreeGridDataItem()
{
ID = 10,
ParentID = 4,
Name = @"Slicer",
Age = @"57",
Alignment = @"💜 Lawful evil",
Race = @"🐡 Locathah",
Class = @"💪 Barbarian"
});
this.Add(new RoleplayTreeGridDataItem()
{
ID = 11,
ParentID = 7,
Name = @"Nereones Ahlorsath",
Age = @"95",
Alignment = @"💛 Lawful neutral",
Race = @"👩 Human",
Class = @"🥊 Fighter"
});
this.Add(new RoleplayTreeGridDataItem()
{
ID = 12,
ParentID = 9,
Name = @"Nalvarti Stonecutter",
Age = @"118",
Alignment = @"❤️ Neutral good",
Race = @"🧝♀️ Elf",
Class = @"❤️ Cleric"
});
this.Add(new RoleplayTreeGridDataItem()
{
ID = 13,
ParentID = 1,
Name = @"Errk",
Age = @"22",
Alignment = @"🤎 Neutral evil",
Race = @"🧝🏻♂️ Half-Elf",
Class = @"🎻 Bard"
});
this.Add(new RoleplayTreeGridDataItem()
{
ID = 14,
ParentID = 5,
Name = @"Seven Thundercloud",
Age = @"43",
Alignment = @"💖 Lawful good",
Race = @"🐡 Locathah",
Class = @"⚔️ Paladin"
});
this.Add(new RoleplayTreeGridDataItem()
{
ID = 15,
ParentID = 10,
Name = @"Navarra Chergoba",
Age = @"16",
Alignment = @"💜 Lawful evil",
Race = @"🐯 Tabaxi",
Class = @"❤️ Cleric"
});
this.Add(new RoleplayTreeGridDataItem()
{
ID = 16,
ParentID = 4,
Name = @"Sail Snap",
Age = @"56",
Alignment = @"💖 Lawful good",
Race = @"🌳 Arboren",
Class = @"💪 Barbarian"
});
this.Add(new RoleplayTreeGridDataItem()
{
ID = 17,
ParentID = 8,
Name = @"Urreek",
Age = @"17",
Alignment = @"💜 Lawful evil",
Race = @"🧝🏻♂️ Half-Elf",
Class = @"🐉 Warlock"
});
this.Add(new RoleplayTreeGridDataItem()
{
ID = 18,
ParentID = 6,
Name = @"Morkral Firetamer",
Age = @"24",
Alignment = @"🤎 Neutral evil",
Race = @"🐲 Dragonborn",
Class = @"🙏🏻 Monk"
});
this.Add(new RoleplayTreeGridDataItem()
{
ID = 19,
ParentID = 2,
Name = @"Vithka",
Age = @"53",
Alignment = @"💜 Lawful evil",
Race = @"🐡 Locathah",
Class = @"⚔️ Paladin"
});
this.Add(new RoleplayTreeGridDataItem()
{
ID = 20,
ParentID = 7,
Name = @"Sandrue Avhoste",
Age = @"19",
Alignment = @"💙 Chaotic Neutral",
Race = @"🐲 Dragonborn",
Class = @"🗡️ Rogue"
});
this.Add(new RoleplayTreeGridDataItem()
{
ID = 21,
ParentID = 8,
Name = @"Hapah Moq",
Age = @"34",
Alignment = @"💜 Lawful evil",
Race = @"🎅🏽 Dwarf",
Class = @"🎻 Bard"
});
this.Add(new RoleplayTreeGridDataItem()
{
ID = 22,
ParentID = 5,
Name = @"Kothar",
Age = @"55",
Alignment = @"🤍 Unaligned",
Race = @"🧝🏻♂️ Half-Elf",
Class = @"🐉 Warlock"
});
this.Add(new RoleplayTreeGridDataItem()
{
ID = 23,
ParentID = 1,
Name = @"Senen",
Age = @"40",
Alignment = @"💜 Lawful evil",
Race = @"🧒🏻 Hafling",
Class = @"🥊 Fighter"
});
}
}
cs
@using IgniteUI.Blazor.Controls
@inject IJSRuntime JS
<div class="container vertical ig-typography">
<div class="container vertical fill">
<IgbTreeGrid
AutoGenerate="false"
Name="treeGrid1"
@ref="treeGrid1"
Id="treeGrid1"
Data="RoleplayTreeGridData"
PrimaryKey="ID"
ForeignKey="ParentID">
<IgbColumn
Field="Name"
Header="Character Name"
DataType="GridColumnDataType.String">
</IgbColumn>
<IgbColumn
Field="Race"
Header="Race"
DataType="GridColumnDataType.String"
Editable="true"
InlineEditorTemplateScript="WebTreeGridCellEditCellTemplate"
Name="column1"
@ref="column1">
</IgbColumn>
<IgbColumn
Field="Class"
Header="Class"
InlineEditorTemplateScript="WebTreeGridCellEditCellTemplate"
DataType="GridColumnDataType.String"
Editable="true"
Name="column2"
@ref="column2">
</IgbColumn>
<IgbColumn
Field="Age"
Header="Age"
DataType="GridColumnDataType.String"
Editable="true">
</IgbColumn>
<IgbColumn
Field="Alignment"
Header="Alignment"
InlineEditorTemplateScript="WebTreeGridCellEditCellTemplate"
DataType="GridColumnDataType.String"
Editable="true"
Name="column3"
@ref="column3">
</IgbColumn>
</IgbTreeGrid>
</div>
</div>
@code {
protected override async Task OnAfterRenderAsync(bool firstRender)
{
var treeGrid1 = this.treeGrid1;
var column1 = this.column1;
var column2 = this.column2;
var column3 = this.column3;
}
private IgbTreeGrid treeGrid1;
private IgbColumn column1;
private IgbColumn column2;
private IgbColumn column3;
private RoleplayTreeGridData _roleplayTreeGridData = null;
public RoleplayTreeGridData RoleplayTreeGridData
{
get
{
if (_roleplayTreeGridData == null)
{
_roleplayTreeGridData = new RoleplayTreeGridData();
}
return _roleplayTreeGridData;
}
}
}
razor
igRegisterScript("WebTreeGridCellEditCellTemplate", (ctx) => {
var html = window.igTemplating.html;
let cellValues = [];
let uniqueValues = [];
for (const i of ctx.cell.grid.data) {
const field = ctx.cell.column.field;
if (uniqueValues.indexOf(i[field]) === -1) {
if (ctx.cell.value == i[field]) {
cellValues.push(html`<igc-select-item selected value=${i[field]}>${(i[field])}</igc-select-item>`);
} else 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%; --ig-size: var(--ig-size-large);" @igcChange=${e => ctx.cell.editValue = e.detail.value}>
${cellValues}
</igc-select>
`
}, false);
js/*
CSS styles are loaded from the shared CSS file located at:
https://static.infragistics.com/xplatform/css/samples/
*/
css
CRUD 操作
CRUD 操作を実行した場合、filtering、sorting、grouping などのパイプが再適用されるため、ビューが自動的に更新されることに注意してください。
IgbTreeGrid
は基本的な CRUD 操作のための簡易な API を提供します。
新しいレコードの追加
IgbTreeGrid
コンポーネントは、提供したデータをデータ ソースに追加する AddRow
メソッドを公開します。
//Assuming we have a `GetNewRecord` method returning the new row data.
const record = this.GetNewRecord();
this.TreeGridRef.AddRow(record);
razor
データを Tree Grid で更新
Tree Grid のデータ更新は、グリッドで PrimaryKey が定義されている場合のみ UpdateRow
と UpdateCell
メソッドで行うことができます。セルと行の値またはそのいずれかを各 update メソッドで直接更新できます。
@code {
// Updating the whole row
this.treeGrid.UpdateRow(newData, this.selectedCell.cellID.rowID);
// Just a particular cell through the Tree Grid API
this.treeGrid.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
IgbRowType row = this.treeGrid.GetRowByKey(rowID);
row.Update(newData);
}
razor
Tree Grid からデータを削除
DeleteRow
メソッドは、PrimaryKey
が定義されている場合に指定した行のみを削除することに注意してください。
@code {
// Delete row through Tree Grid API
this.treeGrid.DeleteRow(this.selectedCell.cellID.rowID);
// Delete row through row object
IgbRowType row = this.treeGrid.GetRowByIndex(rowIndex);
row.Del();
}
razor
編集イベントでのセル検証
IgbTreeGrid
の編集イベントを使用して、ユーザーが IgbTreeGrid
を操作する方法を変更できます。
この例では、CellEdit
イベントにバインドすることにより、入力されたデータに基づいてセルを検証します。セルの新しい値が事前定義された基準を満たしていない場合、イベントをキャンセルすることでデータソースに到達しないようにします。
最初に必要なことは、グリッドのイベントにバインドすることです。
<IgbTreeGrid CellEditScript="HandleCellEdit" />
razor
CellEdit
は、セルの値がコミットされる直前に発生します。CellEdit の定義では、アクションを実行する前に特定の列を確認する必要があります。
*** In JavaScript ***
igRegisterScript("HandleCellEdit", (ev) => {
const column = event.detail.column;
if (column.field === 'Age') {
if (event.detail.newValue < 18) {
event.detail.cancel = true;
alert('Employees must be at least 18 years old!');
}
} else if (column.field === 'HireDate') {
if (event.detail.newValue > new Date().getTime()) {
event.detail.cancel = true;
alert('The employee hire date must be in the past!');
}
}
}, false);
razor
Age (年齢) 列の下のセルに入力された値が 18 未満である場合、または HireDate (雇用日) 列の値が将来の場合、編集はキャンセルされ、ユーザーにキャンセルについての警告が表示されます。
以下は、上記の検証が IgbTreeGrid
に適用された結果のデモです。
using System;
using System.Collections.Generic;
public class EmployeesNestedTreeDataItem
{
public double Age { get; set; }
public string HireDate { get; set; }
public double ID { get; set; }
public string Name { get; set; }
public string Phone { get; set; }
public bool OnPTO { get; set; }
public double ParentID { get; set; }
public string Title { get; set; }
}
public class EmployeesNestedTreeData
: List<EmployeesNestedTreeDataItem>
{
public EmployeesNestedTreeData()
{
this.Add(new EmployeesNestedTreeDataItem()
{
Age = 55,
HireDate = @"2008-03-20",
ID = 1,
Name = @"Johnathan Winchester",
Phone = @"0251-031259",
OnPTO = false,
ParentID = -1,
Title = @"Development Manager"
});
this.Add(new EmployeesNestedTreeDataItem()
{
Age = 42,
HireDate = @"2014-01-22",
ID = 4,
Name = @"Ana Sanders",
Phone = @"(21) 555-0091",
OnPTO = true,
ParentID = -1,
Title = @"CEO"
});
this.Add(new EmployeesNestedTreeDataItem()
{
Age = 49,
HireDate = @"2014-01-22",
ID = 18,
Name = @"Victoria Lincoln",
Phone = @"(071) 23 67 22 20",
OnPTO = true,
ParentID = -1,
Title = @"Accounting Manager"
});
this.Add(new EmployeesNestedTreeDataItem()
{
Age = 61,
HireDate = @"2010-01-01",
ID = 10,
Name = @"Yang Wang",
Phone = @"(21) 555-0091",
OnPTO = false,
ParentID = -1,
Title = @"Localization Manager"
});
this.Add(new EmployeesNestedTreeDataItem()
{
Age = 43,
HireDate = @"2011-06-03",
ID = 3,
Name = @"Michael Burke",
Phone = @"0452-076545",
OnPTO = true,
ParentID = 1,
Title = @"Senior Software Developer"
});
this.Add(new EmployeesNestedTreeDataItem()
{
Age = 29,
HireDate = @"2009-06-19",
ID = 2,
Name = @"Thomas Anderson",
Phone = @"(14) 555-8122",
OnPTO = false,
ParentID = 1,
Title = @"Senior Software Developer"
});
this.Add(new EmployeesNestedTreeDataItem()
{
Age = 31,
HireDate = @"2014-08-18",
ID = 11,
Name = @"Monica Reyes",
Phone = @"7675-3425",
OnPTO = false,
ParentID = 1,
Title = @"Software Development Team Lead"
});
this.Add(new EmployeesNestedTreeDataItem()
{
Age = 35,
HireDate = @"2015-09-17",
ID = 6,
Name = @"Roland Mendel",
Phone = @"(505) 555-5939",
OnPTO = false,
ParentID = 11,
Title = @"Senior Software Developer"
});
this.Add(new EmployeesNestedTreeDataItem()
{
Age = 44,
HireDate = @"2009-10-11",
ID = 12,
Name = @"Sven Cooper",
Phone = @"0695-34 67 21",
OnPTO = true,
ParentID = 11,
Title = @"Senior Software Developer"
});
this.Add(new EmployeesNestedTreeDataItem()
{
Age = 44,
HireDate = @"2014-04-04",
ID = 14,
Name = @"Laurence Johnson",
Phone = @"981-443655",
OnPTO = false,
ParentID = 4,
Title = @"Director"
});
this.Add(new EmployeesNestedTreeDataItem()
{
Age = 25,
HireDate = @"2017-11-09",
ID = 5,
Name = @"Elizabeth Richards",
Phone = @"(2) 283-2951",
OnPTO = true,
ParentID = 4,
Title = @"Vice President"
});
this.Add(new EmployeesNestedTreeDataItem()
{
Age = 39,
HireDate = @"2010-03-22",
ID = 13,
Name = @"Trevor Ashworth",
Phone = @"981-443655",
OnPTO = true,
ParentID = 5,
Title = @"Director"
});
this.Add(new EmployeesNestedTreeDataItem()
{
Age = 44,
HireDate = @"2014-04-04",
ID = 17,
Name = @"Antonio Moreno",
Phone = @"(505) 555-5939",
OnPTO = false,
ParentID = 18,
Title = @"Senior Accountant"
});
this.Add(new EmployeesNestedTreeDataItem()
{
Age = 50,
HireDate = @"2007-11-18",
ID = 7,
Name = @"Pedro Rodriguez",
Phone = @"035-640230",
OnPTO = false,
ParentID = 10,
Title = @"Senior Localization Developer"
});
this.Add(new EmployeesNestedTreeDataItem()
{
Age = 27,
HireDate = @"2016-02-19",
ID = 8,
Name = @"Casey Harper",
Phone = @"0342-023176",
OnPTO = true,
ParentID = 10,
Title = @"Senior Localization"
});
this.Add(new EmployeesNestedTreeDataItem()
{
Age = 25,
HireDate = @"2017-11-09",
ID = 15,
Name = @"Patricia Simpson",
Phone = @"069-0245984",
OnPTO = false,
ParentID = 7,
Title = @"Localization Intern"
});
this.Add(new EmployeesNestedTreeDataItem()
{
Age = 39,
HireDate = @"2010-03-22",
ID = 9,
Name = @"Francisco Chang",
Phone = @"(91) 745 6200",
OnPTO = false,
ParentID = 7,
Title = @"Localization Intern"
});
this.Add(new EmployeesNestedTreeDataItem()
{
Age = 25,
HireDate = @"2018-03-18",
ID = 16,
Name = @"Peter Lewis",
Phone = @"069-0245984",
OnPTO = true,
ParentID = 7,
Title = @"Localization Intern"
});
}
}
csusing System;
using System.Net.Http;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Text;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using IgniteUI.Blazor.Controls; // for registering Ignite UI modules
namespace Infragistics.Samples
{
public class Program
{
public static async Task Main(string[] args)
{
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("app");
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
// registering Ignite UI modules
builder.Services.AddIgniteUIBlazor(
typeof(IgbInputModule),
typeof(IgbTreeGridModule)
);
await builder.Build().RunAsync();
}
}
}
cs
@using IgniteUI.Blazor.Controls
@inject IJSRuntime JS
<div class="container vertical ig-typography">
<div class="container vertical fill">
<IgbTreeGrid
AutoGenerate="false"
Name="treeGrid"
@ref="treeGrid"
Id="treeGrid"
Data="EmployeesNestedTreeData"
PrimaryKey="ID"
CellEditScript="WebTreeGridCellEdit"
ForeignKey="ParentID">
<IgbColumn
Field="Name"
DataType="GridColumnDataType.String">
</IgbColumn>
<IgbColumn
Field="Title"
DataType="GridColumnDataType.String">
</IgbColumn>
<IgbColumn
Field="Age"
DataType="GridColumnDataType.Number"
Editable="true">
</IgbColumn>
<IgbColumn
Field="HireDate"
DataType="GridColumnDataType.Date"
Editable="true">
</IgbColumn>
</IgbTreeGrid>
</div>
</div>
@code {
protected override async Task OnAfterRenderAsync(bool firstRender)
{
var treeGrid = this.treeGrid;
}
private IgbTreeGrid treeGrid;
private EmployeesNestedTreeData _employeesNestedTreeData = null;
public EmployeesNestedTreeData EmployeesNestedTreeData
{
get
{
if (_employeesNestedTreeData == null)
{
_employeesNestedTreeData = new EmployeesNestedTreeData();
}
return _employeesNestedTreeData;
}
}
}
razor
igRegisterScript("WebTreeGridCellEdit", (event) => {
const column = event.detail.column;
if (column.field === 'Age') {
if (event.detail.newValue < 18) {
event.detail.cancel = true;
alert('Employees must be at least 18 years old!');
}
} else if (column.field === 'HireDate') {
if (event.detail.newValue > new Date().getTime()) {
event.detail.cancel = true;
alert('The employee hire date must be in the past!');
}
}
}, false);
js/*
CSS styles are loaded from the shared CSS file located at:
https://static.infragistics.com/xplatform/css/samples/
*/
css
スタイル設定
事前定義されたテーマに加えて、利用可能な CSS プロパティを設定することでグリッドをさらにカスタマイズできます。 一部の色を変更したい場合は、最初にグリッドのクラスを設定する必要があります。
<IgbTreeGrid Class="treeGrid"></IgbTreeGrid>
razor
次に、そのクラスに関連する CSS プロパティを設定します。
.treeGrid {
--ig-grid-edit-mode-color: orange;
--ig-grid-cell-editing-background: lightblue;
}
css
スタイル設定の例
using System;
using System.Collections.Generic;
public class EmployeesNestedTreeDataItem
{
public double Age { get; set; }
public string HireDate { get; set; }
public double ID { get; set; }
public string Name { get; set; }
public string Phone { get; set; }
public bool OnPTO { get; set; }
public double ParentID { get; set; }
public string Title { get; set; }
}
public class EmployeesNestedTreeData
: List<EmployeesNestedTreeDataItem>
{
public EmployeesNestedTreeData()
{
this.Add(new EmployeesNestedTreeDataItem()
{
Age = 55,
HireDate = @"2008-03-20",
ID = 1,
Name = @"Johnathan Winchester",
Phone = @"0251-031259",
OnPTO = false,
ParentID = -1,
Title = @"Development Manager"
});
this.Add(new EmployeesNestedTreeDataItem()
{
Age = 42,
HireDate = @"2014-01-22",
ID = 4,
Name = @"Ana Sanders",
Phone = @"(21) 555-0091",
OnPTO = true,
ParentID = -1,
Title = @"CEO"
});
this.Add(new EmployeesNestedTreeDataItem()
{
Age = 49,
HireDate = @"2014-01-22",
ID = 18,
Name = @"Victoria Lincoln",
Phone = @"(071) 23 67 22 20",
OnPTO = true,
ParentID = -1,
Title = @"Accounting Manager"
});
this.Add(new EmployeesNestedTreeDataItem()
{
Age = 61,
HireDate = @"2010-01-01",
ID = 10,
Name = @"Yang Wang",
Phone = @"(21) 555-0091",
OnPTO = false,
ParentID = -1,
Title = @"Localization Manager"
});
this.Add(new EmployeesNestedTreeDataItem()
{
Age = 43,
HireDate = @"2011-06-03",
ID = 3,
Name = @"Michael Burke",
Phone = @"0452-076545",
OnPTO = true,
ParentID = 1,
Title = @"Senior Software Developer"
});
this.Add(new EmployeesNestedTreeDataItem()
{
Age = 29,
HireDate = @"2009-06-19",
ID = 2,
Name = @"Thomas Anderson",
Phone = @"(14) 555-8122",
OnPTO = false,
ParentID = 1,
Title = @"Senior Software Developer"
});
this.Add(new EmployeesNestedTreeDataItem()
{
Age = 31,
HireDate = @"2014-08-18",
ID = 11,
Name = @"Monica Reyes",
Phone = @"7675-3425",
OnPTO = false,
ParentID = 1,
Title = @"Software Development Team Lead"
});
this.Add(new EmployeesNestedTreeDataItem()
{
Age = 35,
HireDate = @"2015-09-17",
ID = 6,
Name = @"Roland Mendel",
Phone = @"(505) 555-5939",
OnPTO = false,
ParentID = 11,
Title = @"Senior Software Developer"
});
this.Add(new EmployeesNestedTreeDataItem()
{
Age = 44,
HireDate = @"2009-10-11",
ID = 12,
Name = @"Sven Cooper",
Phone = @"0695-34 67 21",
OnPTO = true,
ParentID = 11,
Title = @"Senior Software Developer"
});
this.Add(new EmployeesNestedTreeDataItem()
{
Age = 44,
HireDate = @"2014-04-04",
ID = 14,
Name = @"Laurence Johnson",
Phone = @"981-443655",
OnPTO = false,
ParentID = 4,
Title = @"Director"
});
this.Add(new EmployeesNestedTreeDataItem()
{
Age = 25,
HireDate = @"2017-11-09",
ID = 5,
Name = @"Elizabeth Richards",
Phone = @"(2) 283-2951",
OnPTO = true,
ParentID = 4,
Title = @"Vice President"
});
this.Add(new EmployeesNestedTreeDataItem()
{
Age = 39,
HireDate = @"2010-03-22",
ID = 13,
Name = @"Trevor Ashworth",
Phone = @"981-443655",
OnPTO = true,
ParentID = 5,
Title = @"Director"
});
this.Add(new EmployeesNestedTreeDataItem()
{
Age = 44,
HireDate = @"2014-04-04",
ID = 17,
Name = @"Antonio Moreno",
Phone = @"(505) 555-5939",
OnPTO = false,
ParentID = 18,
Title = @"Senior Accountant"
});
this.Add(new EmployeesNestedTreeDataItem()
{
Age = 50,
HireDate = @"2007-11-18",
ID = 7,
Name = @"Pedro Rodriguez",
Phone = @"035-640230",
OnPTO = false,
ParentID = 10,
Title = @"Senior Localization Developer"
});
this.Add(new EmployeesNestedTreeDataItem()
{
Age = 27,
HireDate = @"2016-02-19",
ID = 8,
Name = @"Casey Harper",
Phone = @"0342-023176",
OnPTO = true,
ParentID = 10,
Title = @"Senior Localization"
});
this.Add(new EmployeesNestedTreeDataItem()
{
Age = 25,
HireDate = @"2017-11-09",
ID = 15,
Name = @"Patricia Simpson",
Phone = @"069-0245984",
OnPTO = false,
ParentID = 7,
Title = @"Localization Intern"
});
this.Add(new EmployeesNestedTreeDataItem()
{
Age = 39,
HireDate = @"2010-03-22",
ID = 9,
Name = @"Francisco Chang",
Phone = @"(91) 745 6200",
OnPTO = false,
ParentID = 7,
Title = @"Localization Intern"
});
this.Add(new EmployeesNestedTreeDataItem()
{
Age = 25,
HireDate = @"2018-03-18",
ID = 16,
Name = @"Peter Lewis",
Phone = @"069-0245984",
OnPTO = true,
ParentID = 7,
Title = @"Localization Intern"
});
}
}
csusing System;
using System.Net.Http;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Text;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using IgniteUI.Blazor.Controls; // for registering Ignite UI modules
namespace Infragistics.Samples
{
public class Program
{
public static async Task Main(string[] args)
{
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("app");
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
// registering Ignite UI modules
builder.Services.AddIgniteUIBlazor(
typeof(IgbInputModule),
typeof(IgbPaginatorModule),
typeof(IgbTreeGridModule)
);
await builder.Build().RunAsync();
}
}
}
cs
@using IgniteUI.Blazor.Controls
<style>
/*
CSS styles are loaded from the shared CSS file located at:
https://static.infragistics.com/xplatform/css/samples/
*/
#treeGrid {
--cell-editing-background: #4567bb;
--cell-active-border-color: #4567bb;
--cell-edited-value-color: #fff;
}
</style>
<div class="container vertical ig-typography">
<div class="container vertical fill">
<IgbTreeGrid
AutoGenerate="false"
Name="treeGrid"
@ref="treeGrid"
Id="treeGrid"
Data="EmployeesNestedTreeData"
PrimaryKey="ID"
AllowFiltering="true"
ForeignKey="ParentID">
<IgbPaginator
PerPage="10">
</IgbPaginator>
<IgbColumn
Field="Name"
DataType="GridColumnDataType.String"
Editable="true">
</IgbColumn>
<IgbColumn
Field="Title"
DataType="GridColumnDataType.String"
Editable="true">
</IgbColumn>
<IgbColumn
Field="Age"
DataType="GridColumnDataType.Number"
Editable="true">
</IgbColumn>
<IgbColumn
Field="HireDate"
DataType="GridColumnDataType.Date"
Editable="true">
</IgbColumn>
<IgbColumn
Field="OnPTO"
DataType="GridColumnDataType.Boolean"
Editable="true"
Width="130px">
</IgbColumn>
</IgbTreeGrid>
</div>
</div>
@code {
protected override async Task OnAfterRenderAsync(bool firstRender)
{
var treeGrid = this.treeGrid;
}
private IgbTreeGrid treeGrid;
private EmployeesNestedTreeData _employeesNestedTreeData = null;
public EmployeesNestedTreeData EmployeesNestedTreeData
{
get
{
if (_employeesNestedTreeData == null)
{
_employeesNestedTreeData = new EmployeesNestedTreeData();
}
return _employeesNestedTreeData;
}
}
}
razor/*
CSS styles are loaded from the shared CSS file located at:
https://static.infragistics.com/xplatform/css/samples/
*/
#treeGrid {
--cell-editing-background: #4567bb;
--cell-active-border-color: #4567bb;
--cell-edited-value-color: #fff;
}
css