So I have this component, which works perfectly - now - after struggling with infamous issues relating to table data not updating when a row is edited.
This now works for me after using async code (START/END block)
However, when I click on the Edit link, the data form is correctly shown, I can update and save fine, and the new data is committed to the db. But, the table loses all sort/filtering and displays data in a different order. Sometimes the row is on another page! I clearly have got something wrong here.
questions..
- How can i ensure that after editing a row record, the table data presentation remains unchanged apart from of course showing any updates to the row.
2.How might I improve the Where clause on retrieving Licences from context.LicenceEntity? - what foes this code do? i obtained it ffrom a github example but no comment or idea what it does... without it, my component wont work..
protected override void OnInitialized()
{
context = DbFactory.CreateDbContext();
Licences = context.LicenceEntity.Where(l => l.Id != null); // not sure how to get all rows unconditionally ??
}
public async ValueTask DisposeAsync() => await context.DisposeAsync();
Thanks!
Component:
@page "/licenceentities"
@using Microsoft.EntityFrameworkCore
@using Microsoft.AspNetCore.Components.QuickGrid
@using Licenses;
@using Licenses.Data
@inject IDbContextFactory<Licenses.Data.LicensesContext> DbFactory
@inject NavigationManager Navigation_Manager
<PageTitle>Licences</PageTitle>
<h1>Licence List</h1>
<div>
<p>This page demonstrates Data Grid component featuring:</p>
<ul>
<li>Column Sorting</li>
<li>Compound Column Filtering</li>
<li>Pagination [set to 3 items for demo purposes]</li>
</ul>
<p>Additionally, the Add new button demonstrates how easy it is to enact client side rendering, as this is a component that would have required JavaScript and a websocket back to the server.</p>
</div>
<p>
<RadzenButton Click=@( args => Navigation_Manager.NavigateTo("licenceentities/create")) Text="Add new" Icon="add_circle" ButtonStyle="ButtonStyle.Primary" />
</p>
<style type="text/css">
.rz-column-title {
font-weight: bold !important;
}
</style>
<RadzenDataGrid @ref="licenceGrid" AllowFiltering="true" FilterMode="FilterMode.Advanced" LogicalFilterOperator="LogicalFilterOperator.Or" AllowPaging="true" PageSize="3"
AllowSorting="true" AllowMultiColumnSorting="true" @bind-Data=@Licences TItem="LicenceEntity" GridLines=Radzen.DataGridGridLines.Default>
<Columns>
<RadzenDataGridColumn TItem="LicenceEntity" Property="Name" Title="Name" />
<RadzenDataGridColumn TItem="LicenceEntity" Property="Description" Title="Description" />
<RadzenDataGridColumn TItem="LicenceEntity" Property="Date" Title="Date created" FormatString="{0:dd MMM yyyy}"/>
<RadzenDataGridColumn TItem="LicenceEntity" Property="ExpiryDate" Title="Expiry date" FormatString="{0:dd MMM yyyy}" />
<RadzenDataGridColumn TItem="LicenceEntity" Sortable="false" Filterable="false" >
<Template Context="licence">
<a href="@($"licenceentities/edit?id={licence.Id}")">Edit</a> |
<a href="@($"licenceentities/details?id={licence.Id}")">Details</a> |
<a href="@($"licenceentities/delete?id={licence.Id}")">Delete</a>
</Template>
</RadzenDataGridColumn>
</Columns>
<FooterTemplate>
Licences in list: <b>@licenceGrid?.View.Count()</b> of <b>@Licences?.Count()</b>
</FooterTemplate>
</RadzenDataGrid>
@code {
RadzenDataGrid<LicenceEntity> ?licenceGrid;
PaginationState pagination = new PaginationState { ItemsPerPage = 10 };
LicensesContext context = default!;
// START - Ensure data is async loaded from spearate DbContext. This ensures edits are reflected in the table.
IQueryable<LicenceEntity> ?Licences = default;
protected override void OnInitialized()
{
context = DbFactory.CreateDbContext();
Licences = context.LicenceEntity.Where(l => l.Id != null); // not sure how to get all rows unconditionally ??
}
public async ValueTask DisposeAsync() => await context.DisposeAsync();
// END
}