Skip to content

Commit

Permalink
DataGrid: Batch Editing (#5122)
Browse files Browse the repository at this point in the history
* WIP : Cell Edit feature (Rapid editing)

* Remove uneeded EditCellValues

* CellEdit | Ongoing

* CellEdit | Code Improvements | OnFocusOut Save | Validation ongoing

* Prefer default behavior for New

* Revert formating

* CellEdit | Improvements

* Cell Edit | Refactor & Improvements

* HandleCellKeyDown | Refactor logic to be easier to read / follow

* Add docs to internal props

* CommandColumn | On Removal set null | Fix issue where Enter key press without a submit button would post the page

* Fix _DataGridRowCommand

* Specific Edit Components | Feature parity with DataGridCellEdit

* Docs & release notes

* Fix issue where Edit.Inline or Edit.Cell showing incorrect # columns on Edit Mode.

* Fix wrong logic for Enter Key Post prevention

* add PreventDefaultOnSubmit to form defautl enter

* Replace Div with Span

* DataGrid | Start Batch Edit

* BatchEdit | Ongoing

* Batch Edit Demo Page | Add Edit Modes

* SaveInternal | Add ValidateAll

* SaveBatch implementation

* Optimize SaveBatchItem to only add an entry if there's changes

* Optimize SaveBatchItem to only add an entry if there's changes

* Batch Edit Demo Page | Add Quantity of Changes

* Introduce Batch Change Event for more flexibility

* Save Changes Translations

* Batch New Rows | Ongoing | Fix pt.json incorrect format

* merge fix

* Customizable Cell & Row styling for batch edit

* Cancel Batch Changes command | Demo customizations

* Rename BatchEditItemState to DataGridBatchEditItemState | Fix edit on a new item

* Fix state comparison

* _DataGridRowMultiSelect fix parameter name

* Docs batch edit example

* Release notes for BatchEdit

* Rename new batch apis to DataGrid prefix

* Docs BatchEdit

* Batch Edit | ButtonRow support | Docs change to batch edit button row example | Remove custom batch styles

* Cancel Changes translations

* DataGridCellStyling | Add Style and Class | Deprecate Column CellClass and CellStyle

* Release notes | Mention new obsolete datagridcolumn parameters

* example formating

* formating

---------

Co-authored-by: Mladen Macanovic <[email protected]>
  • Loading branch information
David-Moreira and stsrki authored Nov 11, 2023
1 parent b7d87be commit 5b8fb90
Show file tree
Hide file tree
Showing 49 changed files with 1,465 additions and 149 deletions.
3 changes: 3 additions & 0 deletions Demos/Blazorise.Demo/Components/SideMenu.razor
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@
Editing
</BarDropdownToggle>
<BarDropdownMenu>
<BarDropdownItem To="tests/datagrid/editing/batch-editing">
Batch Editing
</BarDropdownItem>
<BarDropdownItem To="tests/datagrid/editing/button-row">
Button Row
</BarDropdownItem>
Expand Down
135 changes: 135 additions & 0 deletions Demos/Blazorise.Demo/Pages/Tests/DataGrid/EditingBatchEdit.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
@page "/tests/datagrid/editing/batch-editing"
<Row>
<Column>
<Card Margin="Margin.Is4.OnY">
<CardHeader>
<CardTitle>Datagrid: Batch Editing</CardTitle>
</CardHeader>
<CardBody>
<Paragraph>
Allows you to batch edit by allowing the user to edit multiple rows and then save all the changes at once.
</Paragraph>
<Field>
<FieldLabel>
Edit Mode
</FieldLabel>
<FieldBody>
<Select @bind-SelectedValue="@editMode">
<SelectItem Value="DataGridEditMode.Form">Form</SelectItem>
<SelectItem Value="DataGridEditMode.Inline">Inline</SelectItem>
<SelectItem Value="DataGridEditMode.Popup">Popup</SelectItem>
<SelectItem Value="DataGridEditMode.Cell">Cell</SelectItem>
</Select>
</FieldBody>
</Field>
</CardBody>

<CardBody>
<DataGrid @ref=dataGridRef
TItem="Employee"
Data="inMemoryData"
Responsive
ShowPager
ShowPageSizes
@bind-SelectedRow="@selectedEmployee"
Editable
EditMode="@editMode"
BatchEdit
BatchChange="OnBatchChange"
BatchSaving="OnBatchSaving"
BatchSaved="OnBatchSaved"
BatchEditCellStyling="OnCellBatchEditStyling"
RowBatchEditStyling="(x, y ) => OnRowBatchEditStyling(x,y)"
UseValidation
ValidationsSummaryLabel="The following validation errors have occurred..."
ShowValidationsSummary>
<DataGridColumns>
<DataGridCommandColumn>
<SaveBatchCommandTemplate>
<Button Background=Background.Info Size=Size.Small Clicked="@context.Clicked">Save Changes (@batchQuantity.ToString())</Button>
</SaveBatchCommandTemplate>
<CancelBatchCommandTemplate>
<Button Background=Background.Light Size=Size.Small Clicked="@context.Clicked">Cancel Changes</Button>
</CancelBatchCommandTemplate>
</DataGridCommandColumn>
<DataGridColumn TextAlignment="TextAlignment.Center" TItem="Employee" Field="@nameof( Employee.Id )" Caption="#" Width="60px" />
<DataGridColumn TItem="Employee" Field="@nameof( Employee.FirstName )" Caption="First Name" Editable />
<DataGridColumn TItem="Employee" Field="@nameof( Employee.LastName )" Caption="Last Name" Editable />
<DataGridColumn TItem="Employee" Field="@nameof( Employee.Email )" Caption="Email" Editable />
<DataGridColumn TItem="Employee" Field="@nameof( Employee.City )" Caption="City" Editable>
<CaptionTemplate>
<Icon Name="IconName.City" /> @context.Caption
</CaptionTemplate>
</DataGridColumn>
<DataGridColumn TItem="Employee" Field="@nameof( Employee.Zip )" Caption="Zip">
</DataGridColumn>
<DataGridDateColumn TItem="Employee" Field="@nameof( Employee.DateOfBirth )" DisplayFormat="{0:dd.MM.yyyy}" Caption="Date Of Birth" Editable />
<DataGridNumericColumn TItem="Employee" Field="@nameof( Employee.Childrens )" Caption="Childrens" Editable Filterable="false" />
<DataGridSelectColumn TItem="Employee" Field="@nameof( Employee.Gender )" Caption="Gender" Editable Data="EmployeeData.Genders" ValueField="(x) => ((Gender)x).Code" TextField="(x) => ((Gender)x).Description" />
<DataGridColumn TItem="Employee" Field="@nameof( Employee.Salary )" Caption="Salary" Editable Width="140px" DisplayFormat="{0:C}" DisplayFormatProvider="@System.Globalization.CultureInfo.GetCultureInfo("fr-FR")" TextAlignment="TextAlignment.End">
</DataGridColumn>
<DataGridCheckColumn TItem="Employee" Field="@nameof(Employee.IsActive)" Caption="Active" Editable Filterable="false">
<DisplayTemplate>
<Check TValue="bool" Checked="context.IsActive" Disabled ReadOnly />
</DisplayTemplate>
</DataGridCheckColumn>
</DataGridColumns>
</DataGrid>
</CardBody>
</Card>
</Column>
</Row>

@code {

[Inject] EmployeeData EmployeeData { get; set; }

private int batchQuantity = 0;
private DataGrid<Employee> dataGridRef;
private List<Employee> inMemoryData;
private Employee selectedEmployee;
private DataGridEditMode editMode = DataGridEditMode.Form;

protected override async Task OnInitializedAsync()
{
inMemoryData = ( await EmployeeData.GetDataAsync().ConfigureAwait( false ) ).Take( 25 ).ToList();
await base.OnInitializedAsync();
}

private Task OnBatchChange( DataGridBatchChangeEventArgs<Employee> args )
{
Console.WriteLine( "Batch Change" );
batchQuantity = dataGridRef.BatchChanges.Count;
return Task.CompletedTask;
}

private Task OnBatchSaving( DataGridBatchSavingEventArgs<Employee> args )
{
Console.WriteLine( "Batch Saving" );
return Task.CompletedTask;
}

private Task OnBatchSaved( DataGridBatchSavedEventArgs<Employee> args )
{
Console.WriteLine( "Batch Saved" );
batchQuantity = 0;
return Task.CompletedTask;
}

private void OnRowBatchEditStyling( DataGridBatchEditItem<Employee> batchEditItem, DataGridRowStyling cellStyling )
{
if ( batchEditItem.State == DataGridBatchEditItemState.Delete )
{
cellStyling.Background = Background.Secondary;
}
}

private void OnCellBatchEditStyling( DataGridBatchEditItem<Employee> batchEditItem, DataGridColumn<Employee> column, DataGridCellStyling cellStyling )
{
if ( batchEditItem.State == DataGridBatchEditItemState.Edit && batchEditItem.IsModified( column.Field ) )
{
cellStyling.Background = Background.Light;
cellStyling.TextColor = TextColor.Success;
}
}
}
85 changes: 85 additions & 0 deletions Documentation/Blazorise.Docs/Models/Snippets.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6271,6 +6271,91 @@ private Task OnPredefinedClicked() => dataGrid.ApplySorting(
);
}";

public const string DataGridBatchEditExample = @"<Field>
<FieldLabel>
Edit Mode
</FieldLabel>
<FieldBody>
<Select @bind-SelectedValue=""@editMode"">
<SelectItem Value=""DataGridEditMode.Form"">Form</SelectItem>
<SelectItem Value=""DataGridEditMode.Inline"">Inline</SelectItem>
<SelectItem Value=""DataGridEditMode.Popup"">Popup</SelectItem>
<SelectItem Value=""DataGridEditMode.Cell"">Cell (""Rapid Editing"")</SelectItem>
</Select>
</FieldBody>
</Field>
<DataGrid @ref=dataGridRef
TItem=""Employee""
Data=""inMemoryData""
Responsive
ShowPager
ShowPageSizes
@bind-SelectedRow=""@selectedEmployee""
Editable
EditMode=""@editMode""
BatchEdit
BatchChange=""OnBatchChange""
BatchSaving=""OnBatchSaving""
BatchSaved=""OnBatchSaved""
UseValidation
ValidationsSummaryLabel=""The following validation errors have occurred...""
CommandMode=""DataGridCommandMode.ButtonRow""
ShowValidationsSummary>
<DataGridColumns>
<DataGridCommandColumn SaveBatchCommandAllowed=false CancelBatchCommandAllowed=false />
<DataGridColumn TextAlignment=""TextAlignment.Center"" TItem=""Employee"" Field=""@nameof( Employee.Id )"" Caption=""#"" Width=""60px"" />
<DataGridColumn TItem=""Employee"" Field=""@nameof( Employee.FirstName )"" Caption=""First Name"" Editable />
<DataGridColumn TItem=""Employee"" Field=""@nameof( Employee.LastName )"" Caption=""Last Name"" Editable />
<DataGridColumn TItem=""Employee"" Field=""@nameof( Employee.Email )"" Caption=""Email"" Editable />
<DataGridColumn TItem=""Employee"" Field=""@nameof( Employee.Salary )"" Caption=""Salary"" Editable Width=""140px"" DisplayFormat=""{0:C}"" DisplayFormatProvider=""@System.Globalization.CultureInfo.GetCultureInfo(""fr-FR"")"" TextAlignment=""TextAlignment.End"" />
</DataGridColumns>
<ButtonRowTemplate>
<Button Color=""Color.Success"" Clicked=""context.NewCommand.Clicked"">New</Button>
<Button Color=""Color.Primary"" Disabled=""(selectedEmployee is null)"" Clicked=""context.EditCommand.Clicked"">Edit</Button>
<Button Color=""Color.Danger"" Disabled=""(selectedEmployee is null)"" Clicked=""context.DeleteCommand.Clicked"">Delete</Button>
<Button Color=""Color.Link"" Clicked=""context.ClearFilterCommand.Clicked"">Clear Filter</Button>
<Button Color=""Color.Success"" Disabled=""(batchQuantity == 0)"" Clicked=""@(context.SaveBatchCommand.Clicked)"">@context.SaveBatchCommand.LocalizationString</Button>
<Button Color=""Color.Default"" Clicked=""@(context.CancelBatchCommand.Clicked)"">@context.CancelBatchCommand.LocalizationString</Button>
</ButtonRowTemplate>
</DataGrid>
@code {
[Inject] EmployeeData EmployeeData { get; set; }
private int batchQuantity = 0;
private DataGrid<Employee> dataGridRef;
private List<Employee> inMemoryData;
private Employee selectedEmployee;
private DataGridEditMode editMode = DataGridEditMode.Form;
protected override async Task OnInitializedAsync()
{
inMemoryData = ( await EmployeeData.GetDataAsync().ConfigureAwait( false ) ).Take( 25 ).ToList();
await base.OnInitializedAsync();
}
private Task OnBatchChange( DataGridBatchChangeEventArgs<Employee> args )
{
Console.WriteLine( ""Batch Change"" );
batchQuantity = dataGridRef.BatchChanges.Count;
return Task.CompletedTask;
}
private Task OnBatchSaving( DataGridBatchSavingEventArgs<Employee> args )
{
Console.WriteLine( ""Batch Saving"" );
return Task.CompletedTask;
}
private Task OnBatchSaved( DataGridBatchSavedEventArgs<Employee> args )
{
Console.WriteLine( ""Batch Saved"" );
batchQuantity = 0;
return Task.CompletedTask;
}
}";

public const string DataGridButtonRowExample = @"<DataGrid TItem=""Employee""
Data=""@employeeList""
@bind-SelectedRow=""@selectedEmployee""
Expand Down
Loading

0 comments on commit 5b8fb90

Please sign in to comment.