How to monitor the completion of table data loading ?

929Free 601 Reputation points
2025-04-22T07:01:21.2066667+00:00

hello, I want to perform some tasks after loading data in the table. How should I proceed?

for example :

    <table class="table">
        <thead>
            <tr>
                <th>Date</th>
                <th aria-label="Temperature in Celsius">Temp. (C)</th>
                <th aria-label="Temperature in Farenheit">Temp. (F)</th>
                <th>Summary</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var forecast in forecasts)
            {
                <tr>
                    <td>@forecast.Date.ToShortDateString()</td>
                    <td>@forecast.TemperatureC</td>
                    <td>@forecast.TemperatureF</td>
                    <td>@forecast.Summary</td>
                </tr>
            }
        </tbody>
    </table>
}

Weather.razor :


@code {
    private WeatherForecast[]? forecasts;

    protected override async Task OnInitializedAsync()
    {
        // Simulate asynchronous loading to demonstrate a loading indicator
        await Task.Delay(500);

        var startDate = DateOnly.FromDateTime(DateTime.Now);
        var summaries = new[] { "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" };
        forecasts = Enumerable.Range(1, 5).Select(index => new WeatherForecast
        {
            Date = startDate.AddDays(index),
            TemperatureC = Random.Shared.Next(-20, 55),
            Summary = summaries[Random.Shared.Next(summaries.Length)]
        }).ToArray();
    }

    private class WeatherForecast
    {
        public DateOnly Date { get; set; }
        public int TemperatureC { get; set; }
        public string? Summary { get; set; }
        public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
    }
}

Can you monitor the loading and rendering of forecasts ? thanks

Blazor Training
Blazor Training
Blazor: A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.Training: Instruction to develop new skills.
31 questions
0 comments No comments
{count} votes

Accepted answer
  1. Pradeep M 8,185 Reputation points Microsoft External Staff
    2025-04-22T08:03:53.3166667+00:00

    Hi 929Free

    Thank you for reaching out to Microsoft Q & A forum.  

    To perform an action after the table data has finished loading and rendering, you can use the OnAfterRenderAsync lifecycle method along with a flag to detect when data loading has just completed. Here's a simplified example: 

    @code {
        private WeatherForecast[]? forecasts;
        private bool dataJustLoaded;
        protected override async Task OnInitializedAsync()
        {
            await Task.Delay(500); // Simulate data loading
            forecasts = GetForecasts();
            dataJustLoaded = true;
        }
        protected override async Task OnAfterRenderAsync(bool firstRender)
        {
            if (dataJustLoaded)
            {
                dataJustLoaded = false;
                await HandlePostRenderLogic();
            }
        }
        private Task HandlePostRenderLogic()
        {
            Console.WriteLine("Table has been rendered.");
            return Task.CompletedTask;
        }
        private WeatherForecast[] GetForecasts() => Enumerable.Range(1, 5).Select(i =>
            new WeatherForecast
            {
                Date = DateOnly.FromDateTime(DateTime.Now.AddDays(i)),
                TemperatureC = 20
            }).ToArray();
        private class WeatherForecast
        {
            public DateOnly Date { get; set; }
            public int TemperatureC { get; set; }
            public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
        }
    }
    
    

    This approach ensures that any logic you wish to execute after the table has rendered runs only once, immediately following data load and render completion.      

    If you have found the answer provided to be helpful, please click on the "Accept answer/Upvote" button so that it is useful for other members in the Microsoft Q&A community.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.