We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Bind API data to schedule

Hi,

I’m wondering if there is a way to add data from a weather api that I am consuming into the scheduler so I could show the weather forecast for that day?

Thanks!


1 Reply

VR Vijay Ravi Syncfusion Team March 8, 2023 02:46 PM UTC

Hi Moloney,


You can achieve your requirement by loading the weather forecast data to the Scheduler in the OnInitializedAsync lifecycle of the Blazor component as shown in the below code snippet.


[weatherForcastService.cs]

public Task<WeatherForecast[]> GetForecastAsync(DateTime startDate)

        {

            return Task.FromResult(Enumerable.Range(0, 5).SelectMany(day =>

            {

                var date = startDate.AddDays(day);

                return Enumerable.Range(0, 3).Select(index =>

                {

                    var startTime = date.AddHours(Random.Shared.Next(8, 12));

                    return new WeatherForecast

                    {

                        TemperatureC = Random.Shared.Next(-20, 55),

                        Subject = Summaries[Random.Shared.Next(Summaries.Length)],

                        StartTime = startTime,

                        EndTime = startTime.AddHours(2) // set the end time to 2 hours after the start time

                    };

                });

            }).ToArray());

        }


[weatherForecast.cs]

public class WeatherForecast

    {

        public DateTime StartTime { get; set; }

        public DateTime EndTime { get; set; }

        public int TemperatureC { get; set; }

        public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);

        public string? Subject { get; set; }

    }


[index.razor]

@using BlazorScheduler.Data

@inject WeatherForecastService ForecastService

 

 

private WeatherForecast[]? forecasts;

    protected override async Task OnInitializedAsync()

    {

        forecasts = await ForecastService.GetForecastAsync(DateTime.Now);

 

        foreach (var forecast in forecasts)

        {

            var appointment = new AppointmentData

                {

                    Id = forecast.GetHashCode(),

                    Subject = forecast.Subject,

                    Location = "",

                    StartTime = forecast.StartTime,

                    EndTime = forecast.EndTime,

                    Description = "",

                    IsAllDay = false,

                    RecurrenceRule = "",

                    RecurrenceException = "",

                    RecurrenceID = null

                };

 

            Appointments.Add(appointment);

        }

    }


Regards,

Vijay Ravi


Attachment: weatherforecast_e64046e7.zip

Loader.
Live Chat Icon For mobile
Up arrow icon