Hi team,
I have a razor page containing a data grid (using advanced filtering option) where users can click an "edit" button in each row which takes the user to an "edit record" razor page via a NavigationManager.NavigateTo statement.
After the record edit is saved, the user is returned to the data grid page. Currently, if the grid is filtered using a "contains" filter, then the data grid retains the filter correctly. However, if the grid is filtered using a "starts with" filter, then the data grid filter is not retained. Is this expected behavior?
I've pasted the three bits of code that I am using currently to retain the filter in the protected session value. Are you able to please off me advice on how I can go about adding "page" retention to this code? ie. if I click the "edit record" button when I am on the 3rd page of a filtered data grid, then when I return to the data grid page after editing the record, I should return to the 3rd page of the filtered data grid.
Cheers Jeff
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
await GetSessionValues();
StateHasChanged();
}
await base.OnAfterRenderAsync(firstRender);
}
private async Task GetSessionValues()
{
//Get Filter Value session variable
var resultFV = await storage.GetAsync("filterValues");
sessionquery = resultFV.Success ? resultFV.Value : "";
if (!string.IsNullOrEmpty(resultFV.Value))
{
var elems = JsonSerializer.Deserialize<IEnumerable<Tuple<string, string>>>(resultFV.Value);
foreach (var elem in elems)
{
var column = this.RecordsGrid.ColumnsCollection.FirstOrDefault(x => x.Property.Equals(elem.Item1));
if (column != null)
{
column.FilterValue = elem.Item2;
}
}
this.RecordsGrid.Reload();
}
}
async Task LoadData(LoadDataArgs args)
{
//Define our Records query
var query = StreetsContext.Records.AsQueryable();
//if our "Filter" grid argument has a value then apply it to the query
if (!string.IsNullOrEmpty(args.Filter))
{
query = query.Where(args.Filter);
}
//if our "OrderBy" grid argument has a value then apply it to the query
if (!string.IsNullOrEmpty(args.OrderBy))
{
query = query.OrderBy(args.OrderBy);
}
else //if there is not value then OrderBy RecordName by default.
{
query = query.OrderBy(s => s.RecordName);
}
//XML serialized filter values
var filterValues = args.Filters.Where(x => x.FilterValue != null).Select(x => new { Property = x.Property, FilterValue = x.FilterValue }).AsEnumerable().Select(c => new Tuple<string, string>(c.Property, c.FilterValue.ToString())).ToList();
if (filterValues.Any())
await storage.SetAsync("filterValues", JsonSerializer.Serialize(filterValues));
count = query.Count();
records = query.Skip(args.Skip.Value).Take(args.Top.Value).ToList();
}