UX Cards with Blazor


Well this exercised turned out to be a little surprising.  It turns out the use of UI cards is mainly controlled and implemented by the CSS framework you are using.  In my case bootstrap.  I still wanted to see how to implement the UX of using cards instead of a list or data grid.

For this, I am using the standard Server Blazor project template.  It does not matter if you are using Server or Client side for this project.  Both work the same.

I am taking the Forecast page of the template and changing from using the html table to using html cards.    To implement this, I just replace the table HTML with the HTML for Cards:


        <div class="container">
            <div class="row">
                @foreach (var forecast in forecasts)
                {
                    <div class="col-xs-12 col-sm-8 col-md-6">
                        <div class="">
                            <div class="card">
                                <div class="card-body text-center">
                                    <div class="preview text-center">
                                        <img class="preview-img" src='@forecast.ImagePath' alt="Preview Image" width="100" height="100" />
                                    </div>
                                    <h4 class="card-title">Current Weather: @forecast.Summary</h4>
                                    <div class="text-left">
                                        <h5>Date: @forecast.Date.ToShortDateString()</h5>
                                    </div>
                                    <div class="form-group text-left">
                                        <h5>Temp. (C): @forecast.TemperatureC.ToString()</h5>
                                    </div>
                                    <div class="form-group text-left">
                                        <h5>Temp. (F): @forecast.TemperatureF.ToString()</h5>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                }
            </div>
        </div>
    </section>


The Blazor part of this is what goes inside of the cards body.  In this case the for loop to populate the fields on the cards.  The CSS framework takes care of the rest.  I did add a new extra CSS for padding and to make the border more rounded, because I like that.



    .card {
        border-radius:50px;
        margin: 5px;
    }

I did add the image to the card.  It is based on the summary of the weather.  I found some images that represent the different types of weather.  Those are in the image folder.

While I was changing things, I always found it disturbing that the weather summary did not match the actual temperature.  For example, the summary would be “Chilly” but it is 103F,  that is not right.  I added a new method to match the summary to the temperature.  At least with this change, I get to write some C# code.

New Get Forecast Method:

        public Task<WeatherForecast[]> GetForecastAsync(DateTime startDate)
        {
            List<WeatherForecast> result = new List<WeatherForecast>();
            var rng = new Random();            
            for(int i = 0; i < recordNumber; i++)
            { 
                int temp = rng.Next(-20, 55);
                string summary = GetSummary(temp);
                int index = i;
                WeatherForecast entity = new WeatherForecast
                {
                    Date = startDate.AddDays(index),
                    TemperatureC = temp,
                    Summary = summary,
                    ImagePath = $"/img/{summary}.jpg"
                };
                result.Add(entity);
            }
            return Task.FromResult(result.ToArray());

        }


I added a new class to hold the weather description and the min and max range of the temperature for that description.  I created a collection of those to use to set the summary in the weather collection.

Class:


    public class WeatherSumary
    {
        public int MinValueC { get; set; }
        public int MaxValueC { get; set; }
        public string Description { get; set; }

    }

Lookup Method:


        public string GetSummary(int temp)
        {
            string result = "";
            result = (from a in Summaries
                     where (a.MinValueC <= temp) && (temp <= a.MaxValueC)
                     select a.Description).FirstOrDefault();
            return result;
        }


New Forecast Page UI





Pretty cool looking.  I will most definitely be using the UX Cards in my future projects.


Comments

Popular posts from this blog

Yes, Blazor Server can scale!

Blazor new and improved Search Box

Blazor Wizard Step Component