Building Blazor WebAssembly Applications with gRPC by Václav Pekárek

Building Blazor WebAssembly Applications with gRPC by Václav Pekárek

Author:Václav Pekárek [Václav Pekárek]
Language: eng
Format: epub
Publisher: Packt Publishing
Published: 2022-11-04T00:00:00+00:00


Viewing the data

When we want to render the data to the table, we have more options on how to use it. One of the common approaches is creating the component as a table wrapper and using the ChildContent parameter to render each row of the table. This allows us to modify the columns presented in the table, but forces us to specify the ChildContent parameter anywhere we are using the component.

In our demo, we will use a different approach. We will create a component that will load data from the given endpoint and render a column for each property in the returned data. To be able to present data as a table, we need to create some C# structures similar to the table:

Shared\Model\TableCell.cs

namespace MediaLibrary.Client.Shared.Model; public class TableCell { public object? Value { get; set; } }

The preceding code shows the class for a single cell in the table. The cell can contain only the Value as we don’t know the exact type we are using for the object.

We need to be able to present the whole table row:

Shared\Model\TableRow.cs

namespace MediaLibrary.Client.Shared.Model; public class TableRow<TItem> { public TableRow(TItem originValue) { OriginValue = originValue; } public List<TableCell> Values { get; set; } = new(); public TItem OriginValue { get; set; } }

The preceding code shows the class for a single table row. We are using the generic TItem property, which will hold the original value of the data, a single item in the collection, list, or array, and the list of values.

When we have the rows, we need to specify table columns to render the table header:

Shared\Model\TableColumn.cs

using System.Reflection; namespace MediaLibrary.Client.Shared.Model; public class TableColumn { public string Name { get; set; } = string.Empty; public PropertyInfo PropertyInfo { get; set; } = null!; }

In the preceding code, you can see the model of TableColumn. This class holds information about a single property. We are keeping the name of the column, which is the name of the property, and PropertyInfo to be able to check the type of the values in the column.

The last model is the Table class, which will have a collection of columns and rows:

Shared\Model\Table.cs

namespace MediaLibrary.Client.Shared.Model; public class Table<TItem> { public IEnumerable<TableColumn> Columns { get; set; } = new List<TableColumn>(); public List<TableRow<TItem>> Rows { get; set; } = new(); }

The preceding code shows how the Columns and Rows properties are defined.

Let’s combine all the code in the new component called DataView. This component will render the table with a column for each property in our model and a row for each record in our database. It will also show the navigation buttons to navigate the user to the detail of the item or to create a new item.

Start with creating two files, DataView.razor and DataView.razor.cs. We will first create the logic in the code behind the file, then we will create the HTML content of our component:

Shared\DataView.razor.cs

using Microsoft.AspNetCore.Components; using System.Net.Http.Json; using MediaLibrary.Client.Shared.Model; namespace MediaLibrary.Client.Shared { public partial class DataView<TItem> where TItem : MediaLibrary.Shared.Model.IModel, new() { // The code goes here } }

The preceding code shows the basic structure of the DataView class.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Popular ebooks
SvelteKit Up and Running by Dylan Hildenbrand(6017)
Building Blazor WebAssembly Applications with gRPC by Václav Pekárek(3548)
Hands-On Application Development with PyCharm by Bruce M. Van Horn II
 Quan Nguyen(2173)
Designing Web APIs with Strapi: Get started with the Strapi headless CMS by building a complete learning management system API by Khalid Elshafie Mozafar Haider(1050)
Django 4 for the Impatient. Learn the core concepts of Python web development with Django in one weekend by G. Lim D. Correa(901)
Vue.js 3 Design Patterns and Best Practices by Pablo David Garaguso(835)
Accelerating Server-Side Development with Fastify by Manuel Spigolon & Maksim Sinik & Matteo Collina(831)
Drupal 10 Module Development - Fourth Edition by Daniel Sipos(754)
Mastering CSS Grid by Thormeier Pascal;(709)
Going the Distance with Babylon.js: Building extensible, maintainable, and attractive browser-based interactive applications using JavaScript by Josh Elster(689)
Simplifying State Management in React Native by Aleksandra Desmurs-Linczewska(601)
Java Memory Management by Maaike van Putten & Seán Kennedy(534)
Hands-On Application Development with Pycharm by II Bruce M. Van Horn;Nguyen Quan;(516)
Joomla!® Explained: Your Step-by-Step Guide (Joanne Romanovich's Library) by Stephen Burge(311)
Python & JavaScript Mastery: 2 Books In 1- Learn And Master Two Powerful Programming Languages by Alex iversion(290)
Beginning Modern JavaScript: A Step-By-Step Gentle Guide to Learn JavaScript for Beginners (Code With Nathan) by Sebhastian Nathan(243)
Understanding JavaScript Promises by Nicholas C. Zakas(230)
Create GUI Applications with Python & Qt6: The hands-on guide to making apps with Python by Martin Fitzpatrick(229)
Programming With Java by Edet Theophilus(225)
NextJS 13 and React Crash Course: Build a Full Stack NextJS 13 App with React, Tailwind and Prisma backend by Lim Greg(212)