The Ignite UI for Blazor State Persistence in Blazor Grid allows developers to easily save and restore the grid state. When the IgbGridState is applied on the Blazor IgbGrid, it exposes the GetStateAsStringAsync and ApplyStateFromStringAsync methods that developers can use to achieve state persistence in any scenario.
Supported Features
IgbGridState supports saving and restoring the state of the following features:
Sorting
Filtering
Advanced Filtering
Paging
CellSelection
RowSelection
ColumnSelection
RowPinning
Expansion
GroupBy
Columns
Multi column headers
Columns order
Column properties defined by the IColumnState interface.
Usage
The GetStateAsStringAsync returns a serialized JSON string, so developers can just take it and save it on any data storage (database, cloud, browser localStorage, etc).
The developer may choose to get only the state for a certain feature/features, by passing in an array with feature names as an argument. Empty array will result to using the default state options.
<IgbGrid><IgbGridState @ref="gridState"></IgbGridState></IgbGrid>@code {// get all features` state in a serialized JSON stringstring stateString = gridState.GetStateAsStringAsync(newstring[0]);
// get the sorting and filtering expressionsstring sortingFilteringStates = gridState.GetStateAsStringAsync(newstring[] { "sorting", "filtering" });
}razor
ApplyStateFromStringAsync - The method accepts a serialized JSON string as argument and will restore the state of each feature found in the JSON string or specified features as second argument.
gridState.ApplyStateFromStringAsync(gridStateString, new string[0]);
gridState.ApplyStateFromStringAsync(sortingFilteringStates, new string[0])
razor
The Options object implements the IgbGridStateOptions interface, i.e. for every key, which is the name of a certain feature, there is the boolean value indicating if this feature state will be tracked. GetStateAsStringAsync methods will not put the state of these features in the returned value and ApplyStateFromStringAsync methods will not restore state for them.
The simple to use single-point API's allows to achieve a full state persistence functionality in just a few lines of code. Copy paste the code from below - it will save the grid state in the browser LocalStorage object every time the user leaves the current page. Whenever the user returns to main page, the grid state will be restored. No more need to configure those complex advanced filtering and sorting expressions every time to get the data you want - do it once and have the code from below do the rest for your users:
using System;
using System.Net.Http;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Text;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using IgniteUI.Blazor.Controls; // for registering Ignite UI modulesnamespaceInfragistics.Samples
{
publicclassProgram
{
publicstaticasync Task Main(string[] args)
{
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("app");
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
// registering Ignite UI modules
builder.Services.AddIgniteUIBlazor(
typeof(IgbGridModule),
typeof(IgbIconModule),
typeof(IgbCheckboxModule)
);
await builder.Build().RunAsync();
}
}
}cs
using System;
using System.Collections.Generic;
publicclassCustomersDataItem
{
publicstring ID { get; set; }
publicstring Company { get; set; }
publicstring ContactName { get; set; }
publicstring ContactTitle { get; set; }
publicstring Address { get; set; }
publicstring City { get; set; }
publicstring Region { get; set; }
publicdouble PostalCode { get; set; }
publicstring Country { get; set; }
publicstring Phone { get; set; }
publicstring Fax { get; set; }
}
publicclassCustomersData
: List<CustomersDataItem>
{
publicCustomersData()
{
this.Add(new CustomersDataItem() { ID = @"ALFKI", Company = @"Alfreds Futterkiste", ContactName = @"Maria Anders", ContactTitle = @"Sales Representative", Address = @"Obere Str. 57", City = @"Berlin", Region = @"East", PostalCode = 12209, Country = @"Germany", Phone = @"030-0074321", Fax = @"030-0076545" });
this.Add(new CustomersDataItem() { ID = @"ANATR", Company = @"Ana Trujillo Emparedados y helados", ContactName = @"Ana Trujillo", ContactTitle = @"Owner", Address = @"Avda. de la Constitución 2222", City = @"México D.F.", Region = @"South", PostalCode = 5021, Country = @"Mexico", Phone = @"(5) 555-4729", Fax = @"(5) 555-3745" });
this.Add(new CustomersDataItem() { ID = @"ANTON", Company = @"Antonio Moreno Taquería", ContactName = @"Antonio Moreno", ContactTitle = @"Owner", Address = @"Mataderos 2312", City = @"México D.F.", Region = @"South", PostalCode = 5023, Country = @"Mexico", Phone = @"(5) 555-3932", Fax = @"(5) 555-3745" });
this.Add(new CustomersDataItem() { ID = @"AROUT", Company = @"Around the Horn", ContactName = @"Thomas Hardy", ContactTitle = @"Sales Representative", Address = @"120 Hanover Sq.", City = @"London", Region = @"East", PostalCode = 22000, Country = @"UK", Phone = @"(171) 555-7788", Fax = @"(171) 555-6750" });
this.Add(new CustomersDataItem() { ID = @"BERGS", Company = @"Berglunds snabbköp", ContactName = @"Christina Berglund", ContactTitle = @"Order Administrator", Address = @"Berguvsvägen 8", City = @"Luleå", Region = @"South", PostalCode = 17000, Country = @"Sweden", Phone = @"0921-12 34 65", Fax = @"0921-12 34 67" });
this.Add(new CustomersDataItem() { ID = @"BLAUS", Company = @"Blauer See Delikatessen", ContactName = @"Hanna Moos", ContactTitle = @"Sales Representative", Address = @"Forsterstr. 57", City = @"Mannheim", Region = @"East", PostalCode = 68306, Country = @"Germany", Phone = @"0621-08460", Fax = @"0621-08924" });
this.Add(new CustomersDataItem() { ID = @"BLONP", Company = @"Blondesddsl père et fils", ContactName = @"Frédérique Citeaux", ContactTitle = @"Marketing Manager", Address = @"24, place Kléber", City = @"Strasbourg", Region = @"East", PostalCode = 67000, Country = @"France", Phone = @"88.60.15.31", Fax = @"88.60.15.32" });
this.Add(new CustomersDataItem() { ID = @"BOLID", Company = @"Bólido Comidas preparadas", ContactName = @"Martín Sommer", ContactTitle = @"Owner", Address = @"C/ Araquil, 67", City = @"Madrid", Region = @"East", PostalCode = 28023, Country = @"Spain", Phone = @"(91) 555 22 82", Fax = @"(91) 555 91 99" });
this.Add(new CustomersDataItem() { ID = @"BONAP", Company = @"Bon app'", ContactName = @"Laurence Lebihan", ContactTitle = @"Owner", Address = @"12, rue des Bouchers", City = @"Marseille", Region = @"West", PostalCode = 13008, Country = @"France", Phone = @"91.24.45.40", Fax = @"91.24.45.41" });
this.Add(new CustomersDataItem() { ID = @"BOTTM", Company = @"Bottom-Dollar Markets", ContactName = @"Elizabeth Lincoln", ContactTitle = @"Accounting Manager", Address = @"23 Tsawassen Blvd.", City = @"Tsawassen", Region = @"BC", PostalCode = 28000, Country = @"Canada", Phone = @"(604) 555-4729", Fax = @"(604) 555-3745" });
this.Add(new CustomersDataItem() { ID = @"BSBEV", Company = @"B's Beverages", ContactName = @"Victoria Ashworth", ContactTitle = @"Sales Representative", Address = @"Fauntleroy Circus", City = @"London", Region = @"South", PostalCode = 10000, Country = @"UK", Phone = @"(171) 555-1212", Fax = @"(5) 555-3745" });
this.Add(new CustomersDataItem() { ID = @"CACTU", Company = @"Cactus Comidas para llevar", ContactName = @"Patricio Simpson", ContactTitle = @"Sales Agent", Address = @"Cerrito 333", City = @"Buenos Aires", Region = @"East", PostalCode = 1010, Country = @"Argentina", Phone = @"(1) 135-5555", Fax = @"(1) 135-4892" });
this.Add(new CustomersDataItem() { ID = @"CENTC", Company = @"Centro comercial Moctezuma", ContactName = @"Francisco Chang", ContactTitle = @"Marketing Manager", Address = @"Sierras de Granada 9993", City = @"México D.F.", Region = @"South", PostalCode = 5022, Country = @"Mexico", Phone = @"(5) 555-3392", Fax = @"(5) 555-7293" });
this.Add(new CustomersDataItem() { ID = @"CHOPS", Company = @"Chop-suey Chinese", ContactName = @"Yang Wang", ContactTitle = @"Owner", Address = @"Hauptstr. 29", City = @"Bern", Region = @"East", PostalCode = 3012, Country = @"Switzerland", Phone = @"0452-076545", Fax = @"(5) 555-3745" });
this.Add(new CustomersDataItem() { ID = @"COMMI", Company = @"Comércio Mineiro", ContactName = @"Pedro Afonso", ContactTitle = @"Sales Associate", Address = @"Av. dos Lusíadas, 23", City = @"Sao Paulo", Region = @"SP", PostalCode = 34000, Country = @"Brazil", Phone = @"(11) 555-7647", Fax = @"(5) 555-3745" });
this.Add(new CustomersDataItem() { ID = @"CONSH", Company = @"Consolidated Holdings", ContactName = @"Elizabeth Brown", ContactTitle = @"Sales Representative", Address = @"Berkeley Gardens 12 Brewery", City = @"London", Region = @"South", PostalCode = 27000, Country = @"UK", Phone = @"(171) 555-2282", Fax = @"(171) 555-9199" });
this.Add(new CustomersDataItem() { ID = @"DRACD", Company = @"Drachenblut Delikatessen", ContactName = @"Sven Ottlieb", ContactTitle = @"Order Administrator", Address = @"Walserweg 21", City = @"Aachen", Region = @"South", PostalCode = 52066, Country = @"Germany", Phone = @"0241-039123", Fax = @"0241-059428" });
this.Add(new CustomersDataItem() { ID = @"DUMON", Company = @"Du monde entier", ContactName = @"Janine Labrune", ContactTitle = @"Owner", Address = @"67, rue des Cinquante Otages", City = @"Nantes", Region = @"East", PostalCode = 44000, Country = @"France", Phone = @"40.67.88.88", Fax = @"40.67.89.89" });
this.Add(new CustomersDataItem() { ID = @"EASTC", Company = @"Eastern Connection", ContactName = @"Ann Devon", ContactTitle = @"Sales Agent", Address = @"35 King George", City = @"London", Region = @"East", PostalCode = 41000, Country = @"UK", Phone = @"(171) 555-0297", Fax = @"(171) 555-3373" });
this.Add(new CustomersDataItem() { ID = @"ERNSH", Company = @"Ernst Handel", ContactName = @"Roland Mendel", ContactTitle = @"Sales Manager", Address = @"Kirchgasse 6", City = @"Graz", Region = @"South", PostalCode = 8010, Country = @"Austria", Phone = @"7675-3425", Fax = @"7675-3426" });
this.Add(new CustomersDataItem() { ID = @"FAMIA", Company = @"Familia Arquibaldo", ContactName = @"Aria Cruz", ContactTitle = @"Marketing Assistant", Address = @"Rua Orós, 92", City = @"Sao Paulo", Region = @"SP", PostalCode = 27000, Country = @"Brazil", Phone = @"(11) 555-9857", Fax = @"(5) 555-3745" });
this.Add(new CustomersDataItem() { ID = @"FISSA", Company = @"FISSA Fabrica Inter. Salchichas S.A.", ContactName = @"Diego Roel", ContactTitle = @"Accounting Manager", Address = @"C/ Moralzarzal, 86", City = @"Madrid", Region = @"East", PostalCode = 28034, Country = @"Spain", Phone = @"(91) 555 94 44", Fax = @"(91) 555 55 93" });
this.Add(new CustomersDataItem() { ID = @"FOLIG", Company = @"Folies gourmandes", ContactName = @"Martine Rancé", ContactTitle = @"Assistant Sales Agent", Address = @"184, chaussée de Tournai", City = @"Lille", Region = @"South", PostalCode = 59000, Country = @"France", Phone = @"20.16.10.16", Fax = @"20.16.10.17" });
this.Add(new CustomersDataItem() { ID = @"FOLKO", Company = @"Folk och fä HB", ContactName = @"Maria Larsson", ContactTitle = @"Owner", Address = @"Åkergatan 24", City = @"Bräcke", Region = @"East", PostalCode = 36000, Country = @"Sweden", Phone = @"0695-34 67 21", Fax = @"0695 33-4455" });
this.Add(new CustomersDataItem() { ID = @"FRANK", Company = @"Frankenversand", ContactName = @"Peter Franken", ContactTitle = @"Marketing Manager", Address = @"Berliner Platz 43", City = @"München", Region = @"East", PostalCode = 80805, Country = @"Germany", Phone = @"089-0877310", Fax = @"089-0877451" });
this.Add(new CustomersDataItem() { ID = @"FRANR", Company = @"France restauration", ContactName = @"Carine Schmitt", ContactTitle = @"Marketing Manager", Address = @"54, rue Royale", City = @"Nantes", Region = @"South", PostalCode = 44000, Country = @"France", Phone = @"40.32.21.21", Fax = @"40.32.21.20" });
this.Add(new CustomersDataItem() { ID = @"FRANS", Company = @"Franchi S.p.A.", ContactName = @"Paolo Accorti", ContactTitle = @"Sales Representative", Address = @"Via Monte Bianco 34", City = @"Torino", Region = @"East", PostalCode = 10100, Country = @"Italy", Phone = @"011-4988260", Fax = @"011-4988261" });
}
}cs
/*
CSS styles are loaded from the shared CSS file located at:
https://static.infragistics.com/xplatform/css/samples/
*/.horizontal {
gap: 10px;
flex-basis: fit-content;
flex-wrap: wrap;
}
.sampleContainer {
padding: 0.5rem
}css
Limitations
GetStateAsString method uses JSON.stringify() method to convert the original objects to a JSON string. JSON.stringify() does not support Functions, thats why the IgbGridState component will ignore the columns Formatter, Filters, Summaries, SortStrategy, CellClasses, CellStyles, HeaderTemplate and BodyTemplate properties.