Skip to content

Latest commit

 

History

History
128 lines (93 loc) · 5.01 KB

Data_annotations.md

File metadata and controls

128 lines (93 loc) · 5.01 KB

GridBlazor for ASP.NET Core MVC

Data annotations

Index

You can customize grid and column settings using data annotations. In other words, you can mark properties of your model class as grid columns, specify column options, call AutoGenerateColumns method, and GridBlazor will automatically create columns as you describe in your annotations.

There are some attributes for this:

  • GridTableAttribute: applies to model classes and specify options for the grid (paging options...)
  • GridColumnAttribute: applies to model public properties and configure a property as a column with a set of properties
  • GridHiddenColumn: applies to model public properties and configures a property as a hidden column
  • NotMappedColumnAttribute: applies to model public properties and configures a property as NOT a column. If a property has this attribute, GridBlazor will not add that column to the column collection

For example a model class with the following data annotations:

    [GridTable(PagingEnabled = true, PageSize = 20)]
    public class Foo
    {
        [GridColumn(Position = 0, Title = "Name title", SortEnabled = true, FilterEnabled = true)]
        public string Name { get; set; }

        [GridColumn(Position = 2, Title = "Active Foo?")]
        public bool Enabled { get; set; }

        [GridColumn(Position = 1, Title = "Date", Format = "{0:dd/MM/yyyy}")]
        public DateTime FooDate { get; set; }

        [NotMappedColumn]
        [GridColumn(Position = 3)]
        public byte[]() Data { get; set; }
    }

describes that the grid table must contain 3 columns (Name, Enabled and FooDate) with custom options. It also enables paging for this grid table and page size as 20 rows.

The steps to build a grid razor page using data annotations with GridBlazor are:

  1. Create a service with a method to get all items for the grid. An example of this type of service is:

        public class FooService
        {
            ...
    
            public ItemsDTO<Foo> GetFooGridRows(QueryDictionary<StringValues> query)
            {
                var repository = new FooRepository(_context);
                var server = new GridServer<Foo>(repository.GetAll(), new QueryCollection(query),
                    true, "fooGrid", null).AutoGenerateColumns();
    
                // return items to displays
                return server.ItemsToDisplay;
            }
        }

    Notes:

    • The method must have 1 parameter, a dictionary to pass query parameters such as grid-page. It must be ot type QueryDictionary

    • The columns parameter passed to the GridServer constructor must be null

    • You must use the AutoGenerateColumns method of the GridServer

  2. Create a razor page to render the grid. The page file must have a .razor extension. An example of razor page is:

        @page "/gridsample"
        @inject FooService fooService
    
        @if (_task.IsCompleted)
        {
            <GridComponent T="Foo" Grid="@_grid"></GridComponent>
        }
        else
        {
            <p><em>Loading...</em></p>
        }
    
        @code
        {
            private CGrid<Foo> _grid;
            private Task _task;
    
            protected override async Task OnParametersSetAsync()
            {
                var query = new QueryDictionary<StringValues>();
                query.Add("grid-page", "2");
    
                var locale = new CultureInfo("fr-FR");
                var client = new GridClient<Foo>(q => fooService.GetFooGridRows(q), query, false,
                    "fooGrid", null, locale).AutoGenerateColumns();
                _grid = client.Grid;
    
                // Set new items to grid
                _task = client.UpdateGrid();
                await _task;
            }
        }

    Notes:

    • The columns parameter passed to the GridClient constructor must be null

    • You must use the AutoGenerateColumns method of the GridClient object to configure a grid.

GridBlazor will generate columns based on your data annotations when the AutoGenerateColumns method is invoked.

You can add custom columns after or before this method is called, for example:

    var server = new GridServer<Foo>(...).AutoGenerateColumns().Columns(columns=>columns.Add(foo=>foo.Child.Price))
    var client = new GridClient<Foo>(...).AutoGenerateColumns().Columns(columns=>columns.Add(foo=>foo.Child.Price))

You can also overwrite grid options. For example using the WithPaging method:

    var server = new GridServer<Foo>(...).AutoGenerateColumns().WithPaging(10)

Note: If you use the Position custom option to order the columns, you must use it on all the columns of the table including the ones using the NotMappedColumn attribute. If you don't do it the AutoGenerateColumns will throw an exception.

<- Localization | Render button, checkbox, etc. in a grid cell ->