Saving Settings

When using Datagrid save settings. Setting a filter works correctly but if you clear filter, code restores filter. To try goto Blazor DataGrid save/load settings (radzen.com) set filter on City. Try to clear filter. Datagrid refreshes 2x 1st without the filter then back on again.

Fixed and the fix will be release tomorrow.

I found an issue that became more apparent with the save settings.
When using a bool column the filter fires a reload before apply. This causes a race as the settings get reloaded and reset the change in the filter. I think this is where the issue is?

Here is what I've tested:



Filtered column is loaded properly:

UPDATE: I was able to reproduce the problem with DataGrid advanced filtering mode - It will be fixed for our next update Monday.

Page Size seems to be an issue as well. When changing page size settings are not saved. To reproduce. Sort on any column to force save settings, then change page size. After that it gets stuck in loop.

@page "/datagrid-loaddata"
@using System.Linq.Dynamic.Core
@using RadzenBlazorDemos.Data
@using RadzenBlazorDemos.Models.Northwind
@using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage

@inject ProtectedLocalStorage ProtectedLocalStore

@inherits DbContextPage

DataGrid LoadData The LoadData event allows you to perform custom paging, sorting and filtering. <RadzenDataGrid style="height: 335px" @ref="grid" IsLoading=@isLoading Count="@count" Data="@employees" LoadData="@LoadData" AllowSorting="true" AllowFiltering="true" AllowPaging="true" PageSize="4" PagerHorizontalAlign="HorizontalAlign.Center" TItem="Employee" ColumnWidth="200px" PageSizeOptions="@pageSizeOptions" @bind-Settings="@Settings"> Perform custom data-binding 1. Set the Data and Count properties.
            <RadzenDataGrid Count="@@count" Data="@@employees"
        
2. Handle the LoadData event and update the Data and Count backing fields (employees and count in this case).
            
void LoadData(LoadDataArgs args)
{
    var query = dbContext.Employees.AsQueryable();
if (!string.IsNullOrEmpty(args.Filter))
{
    query = query.Where(args.Filter);
}

if (!string.IsNullOrEmpty(args.OrderBy))
{
    query = query.OrderBy(args.OrderBy);
}

count = query.Count();

employees = query.Skip(args.Skip.Value).Take(args.Top.Value).ToList();

}



@code {
RadzenDataGrid grid;
int count;
IEnumerable pageSizeOptions = new int[] {1,2,3,4,5, 10, 20, 30 };

IEnumerable<Employee> employees;
bool isLoading = false;

List<string> titles = new List<string> {"Sales Representative", "Vice President, Sales", "Sales Manager", "Inside Sales Coordinator" };
IEnumerable<string> selectedTitles;

async Task OnSelectedTitlesChange(object value)
{
    if (selectedTitles != null && !selectedTitles.Any())
    {
        selectedTitles = null;  
    }
    
    await grid.FirstPage();
}

async Task Reset()
{
    grid.Reset(true); 
    await grid.FirstPage(true);
}

async Task LoadData(LoadDataArgs args)
{
    isLoading = true;

    await Task.Yield();

    // This demo is using https://dynamic-linq.net
    var query = dbContext.Employees.AsQueryable();

    if (!string.IsNullOrEmpty(args.Filter))
    {
        // Filter via the Where method
        query = query.Where(args.Filter);
    }

    if (!string.IsNullOrEmpty(args.OrderBy))
    {
        // Sort via the OrderBy method
        query = query.OrderBy(args.OrderBy);
    }

    // Important!!! Make sure the Count property of RadzenDataGrid is set.
    count = query.Count();

    // Perform paginv via Skip and Take.
    employees = query.Skip(args.Skip.Value).Take(args.Top.Value).ToList();

    isLoading = false;
}
DataGridSettings _settings; public DataGridSettings Settings { get { return _settings; } set { if (_settings != value) { _settings = value; InvokeAsync(SaveStateAsync); } } }
private async Task LoadStateAsync()
{
    var result = await ProtectedLocalStore.GetAsync<DataGridSettings>("Paged2");
    if (result.Success)
    {
        _settings = result.Value;
    }
}

private async Task SaveStateAsync()
{
    await ProtectedLocalStore.SetAsync("Paged2", _settings);
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
    if (firstRender)
    {
        await LoadStateAsync();
        StateHasChanged();
    }
}
}
1 Like

If the PageSize is in definition it gets in conflict with Settings?