Created
February 21, 2025 02:24
-
-
Save swaters86/6e5609dccc17fb80a065f7f07eb86ab7 to your computer and use it in GitHub Desktop.
code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@inject IDbContextFactory<MyDbContext> DbContextFactory | |
@implements IAsyncDisposable | |
<RadzenDataGrid Data="@filteredItems" Count="@totalRecords" | |
LoadData="@LoadData" | |
PageSize="1000" | |
AllowPaging="true" | |
AllowFiltering="true" | |
AllowSorting="true" | |
Virtualize="true"> | |
<Columns> | |
<RadzenDataGridColumn Field="@(data => data.Name)" Title="Name" /> | |
<RadzenDataGridColumn Field="@(data => data.Date)" Title="Date" /> | |
</Columns> | |
</RadzenDataGrid> | |
@code { | |
private List<MyItem> allItems = new(); // Stores real-time updates | |
private List<MyItem> filteredItems = new(); // Stores the displayed 1000 filtered records | |
private int totalRecords; | |
private HubConnection hubConnection; | |
private string currentFilter = string.Empty; | |
protected override async Task OnInitializedAsync() | |
{ | |
// Initialize SignalR connection | |
hubConnection = new HubConnectionBuilder() | |
.WithUrl("https://your-signalr-url") | |
.Build(); | |
hubConnection.On<MyItem>("ReceiveNewItem", HandleNewSignalRItem); | |
await hubConnection.StartAsync(); | |
} | |
private void HandleNewSignalRItem(MyItem newItem) | |
{ | |
allItems.Insert(0, newItem); // Insert at the top | |
if (allItems.Count > 1000) // Maintain max 1000 records | |
{ | |
allItems.RemoveAt(allItems.Count - 1); | |
} | |
ApplyFilter(); | |
InvokeAsync(StateHasChanged); | |
} | |
private void ApplyFilter() | |
{ | |
if (string.IsNullOrEmpty(currentFilter)) | |
{ | |
filteredItems = allItems.Take(1000).ToList(); | |
} | |
else | |
{ | |
filteredItems = allItems | |
.Where(x => x.Name.Contains(currentFilter, StringComparison.OrdinalIgnoreCase)) | |
.Take(1000) | |
.ToList(); | |
} | |
totalRecords = filteredItems.Count; | |
} | |
private async Task LoadData(LoadDataArgs args) | |
{ | |
currentFilter = args.Filter ?? string.Empty; | |
ApplyFilter(); | |
await InvokeAsync(StateHasChanged); | |
} | |
public async ValueTask DisposeAsync() | |
{ | |
if (hubConnection != null) | |
{ | |
await hubConnection.DisposeAsync(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment