Skip to content

Commit

Permalink
DataGrid: Add SelectedCell API (#5700)
Browse files Browse the repository at this point in the history
* SelectedCell feature

* Docs and release notes

* Docs | DatagridApi | Add new SelectedCell params

* Fix merge

* Formating and comments

---------

Co-authored-by: Mladen Macanovic <[email protected]>
Co-authored-by: Mladen Macanovic <[email protected]>
  • Loading branch information
3 people authored Aug 19, 2024
1 parent 6c3a2ea commit d3e4864
Show file tree
Hide file tree
Showing 17 changed files with 452 additions and 22 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 @@ -229,6 +229,9 @@
<BarDropdownItem To="tests/datagrid/selection/single">
Single
</BarDropdownItem>
<BarDropdownItem To="tests/datagrid/selection/cell">
Cell
</BarDropdownItem>
<BarDropdownItem To="tests/datagrid/selection/styling">
Styling
</BarDropdownItem>
Expand Down
98 changes: 98 additions & 0 deletions Demos/Blazorise.Demo/Pages/Tests/DataGrid/SelectionCellPage.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
@page "/tests/datagrid/selection/cell"
<Row>
<Column>
<Card Margin="Margin.Is4.OnY">
<CardHeader>
<CardTitle>Datagrid: Cell Selection</CardTitle>
</CardHeader>
<CardBody>
<Paragraph>
Set the DataGrid's <code>NavigationMode</code> to <code>Cell</code>
</Paragraph>
<UnorderedList>
<UnorderedListItem>
Bind the Selected cell to a property by binding to the <code>SelectedCell</code> parameter.
</UnorderedListItem>
</UnorderedList>
</CardBody>
<CardBody>
<DataGrid TItem="Employee"
Data="inMemoryData"
Responsive
ShowPager
ShowPageSizes
NavigationMode="DataGridNavigationMode.Cell"
@bind-Selectedcell="@selectedCell">
<DataGridColumns>
<DataGridColumn TextAlignment="TextAlignment.Center" TItem="Employee" Field="@nameof( Employee.Id )" Caption="#" Width="60px" />
<DataGridColumn TItem="Employee" Field="@nameof( Employee.FirstName )" Caption="First Name">
</DataGridColumn>
<DataGridColumn TItem="Employee" Field="@nameof( Employee.LastName )" Caption="Last Name" />
<DataGridColumn TItem="Employee" Field="@nameof( Employee.Email )" Caption="Email" />
<DataGridColumn TItem="Employee" Field="@nameof( Employee.City )" Caption="City">
<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>

<Row>
<Column>
<Card>
<CardHeader>
<CardTitle>Selected Cell</CardTitle>
</CardHeader>
<CardBody>
<Fields>
<Field>
<FieldLabel>Row Index</FieldLabel>
<FieldBody>
<TextEdit ReadOnly Text="@selectedCell?.RowIndex.ToString()"></TextEdit>
</FieldBody>
</Field>
<Field>
<FieldLabel>Field</FieldLabel>
<FieldBody>
<TextEdit ReadOnly Text="@selectedCell?.ColumnInfo?.Field"></TextEdit>
</FieldBody>
</Field>
<Field>
<FieldLabel>Value</FieldLabel>
<TextEdit ReadOnly Text="@selectedCell?.Column?.FormatDisplayValue(selectedCell?.Item)"></TextEdit>
</Field>
</Fields>
</CardBody>
</Card>
</Column>
</Row>

@code {

[Inject] EmployeeData EmployeeData { get; set; }

private IEnumerable<Employee> inMemoryData;
private DataGridCellInfo<Employee> selectedCell;

protected override async Task OnInitializedAsync()
{
inMemoryData = ( await EmployeeData.GetDataAsync().ConfigureAwait( false ) ).Take( 25 );
await base.OnInitializedAsync();
}
}
1 change: 1 addition & 0 deletions Documentation/Blazorise.Docs/Layouts/DocsLayout.razor
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@
<BarDropdownMenu>
<BarDropdownItem To="docs/extensions/datagrid/selection/single">Single</BarDropdownItem>
<BarDropdownItem To="docs/extensions/datagrid/selection/multiple">Multiple</BarDropdownItem>
<BarDropdownItem To="docs/extensions/datagrid/selection/cell">Cell</BarDropdownItem>
<BarDropdownItem To="docs/extensions/datagrid/selection/custom-row-colors">Custom Row Colors</BarDropdownItem>
</BarDropdownMenu>
</BarDropdown>
Expand Down
61 changes: 61 additions & 0 deletions Documentation/Blazorise.Docs/Models/Snippets.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7103,6 +7103,67 @@ private Task OnBatchSaved( DataGridBatchSavedEventArgs<Employee> args )
private List<Employee> employeeList;
private Employee selectedEmployee;
protected override async Task OnInitializedAsync()
{
employeeList = await EmployeeData.GetDataAsync();
await base.OnInitializedAsync();
}
}";

public const string DataGridCellSelectionExample = @"<DataGrid TItem=""Employee""
Data=""@employeeList""
NavigationMode=""DataGridNavigationMode.Cell""
@bind-Selectedcell=""@selectedCell""
Responsive>
<DataGridMultiSelectColumn Width=""30px""></DataGridMultiSelectColumn>
<DataGridCommandColumn />
<DataGridColumn Field=""@nameof(Employee.Id)"" Caption=""#"" Sortable=""false"" />
<DataGridColumn Field=""@nameof(Employee.FirstName)"" Caption=""First Name"" Editable />
<DataGridColumn Field=""@nameof(Employee.LastName)"" Caption=""Last Name"" Editable />
<DataGridColumn Field=""@nameof(Employee.Email)"" Caption=""Email"" Editable />
<DataGridColumn Field=""@nameof(Employee.Salary)"" Caption=""Salary"" DisplayFormat=""{0:C}"" DisplayFormatProvider=""@System.Globalization.CultureInfo.GetCultureInfo(""fr-FR"")"" Editable>
<EditTemplate>
<NumericEdit TValue=""decimal"" Value=""@((decimal)context.CellValue)"" ValueChanged=""@( v => context.CellValue = v)"" />
</EditTemplate>
</DataGridColumn>
</DataGrid>
<Row>
<Column>
<Card>
<CardHeader>
<CardTitle>Selected Cell</CardTitle>
</CardHeader>
<CardBody>
<Fields>
<Field>
<FieldLabel>Row Index</FieldLabel>
<FieldBody>
<TextEdit ReadOnly Text=""@selectedCell?.RowIndex.ToString()""></TextEdit>
</FieldBody>
</Field>
<Field>
<FieldLabel>Field</FieldLabel>
<FieldBody>
<TextEdit ReadOnly Text=""@selectedCell?.ColumnInfo?.Field""></TextEdit>
</FieldBody>
</Field>
<Field>
<FieldLabel>Value</FieldLabel>
<TextEdit ReadOnly Text=""@selectedCell?.Column?.FormatDisplayValue(selectedCell?.Item)""></TextEdit>
</Field>
</Fields>
</CardBody>
</Card>
</Column>
</Row>
@code {
[Inject]
public EmployeeData EmployeeData { get; set; }
private List<Employee> employeeList;
private DataGridCellInfo<Employee> selectedCell;
protected override async Task OnInitializedAsync()
{
employeeList = await EmployeeData.GetDataAsync();
Expand Down
Loading

0 comments on commit d3e4864

Please sign in to comment.