This is a migrated thread and some comments may be shown as answers.

Dynamic loading of Data

4 Answers 224 Views
Calendar & Scheduling
This is a migrated thread and some comments may be shown as answers.
virginie
Top achievements
Rank 1
virginie asked on 18 Jul 2017, 09:13 AM

Hello,

i would like to know if it's possible to load Appointments dynamically as we Navigate in the calendar, right now i m loading all the data at once, and it made the calendar very slow.

Thanks

4 Answers, 1 is accepted

Sort by
0
Lance | Manager Technical Support
Telerik team
answered on 18 Jul 2017, 03:39 PM
Hi Virginie,

At this time, the Calendar doesn't have a load-on-demand feature for AppointmentsSource. If you'd like to add a feature request, you do so here in the Feedback Portal.

However, you can try using async/await to load the data from your data source so that it doesn't tie up the UI thread while it's loading the data. Go here to learn more about using the async/await pattern in Xamarin.Forms.

Regards,
Lance | Tech Support Engineer, Sr.
Progress Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Glenn Henriksen
Top achievements
Rank 1
answered on 26 Jul 2017, 12:21 PM
Wait, what? Are you saying that I cannot start by showing the calendar with appointments for one month, and when I move to the next month I can't add more appointments to the collection (and perhaps remove old ones)? I have to assign all the appointments right away?
0
Lance | Manager Technical Support
Telerik team
answered on 26 Jul 2017, 03:28 PM
Hi Glenn,

The Calendar doesn't have built-in support for data source types that implement ISupportIncrementalLoading (MSDN). We do support this for the ListView via the LoadOnDemandCollection, but it's not implemented for the Calendar.

You can certainly add or remove appointments from a bound collection as long as that supports CollectionChanged, for example: ObservableCollection<Appointment>.


Demo

I've attached a demo that only loads the appointments for the currently visible month and updates it when the month changes. Here's an overview of the approach.

If you have a RadCalendar on the page and the AppointmentSource is bound to a collection in the view model:

<telerikInput:RadCalendar AppointmentsSource="{Binding MyAppointments}"
                  DisplayDateChanged="Calendar_OnDisplayDateChanged"/>

Also in the view model is a list of all the appointments and just the collection bound to Calendar:

public class ViewModel
{
    private readonly List<Appointment> allAppointments;
 
    public ViewModel()
    {
        // This contains all of the appointments
        allAppointments = GetAllAppointments();
    }
         
    public ObservableCollection<Appointment> MyAppointments { get; set; } = new ObservableCollection<Appointment>();
}


Now, lets add a method to the view model that can filter the allAppointments list and add them to the bound MyAppointments collection:

public void FilterAppointments(DateTime filterDateTime)
{
    // Clear any appointments you don't want, in this case I'm clearing everything
    MyAppointments.Clear();
             
    // Here I filter by just the appointments for the currently visible Month
    // "allAppointments" is the entire list of 200 appointments
    var filteredAppointments = allAppointments.Where(a
        => a.StartDate.Month == filterDateTime.Month
        && a.StartDate.Year == filterDateTime.Year).ToList();
 
    // now, add the filtered appointments to the
    // collection bound to the calendar's AppointmentsSource
    foreach (var appt in filteredAppointments)
        MyAppointments.Add(appt);
}



The last thing to do is hook into the Calendar's DisplayDateChanged event, this way when the month changes, only the appointments for that month are shown.

private void Calendar_OnDisplayDateChanged(object sender, ValueChangedEventArgs<object> e)
{
    (BindingContext as ViewModel)?.FilterAppointments((DateTime)e.NewValue);
}


The takeaway is that it's the ObservableCollection that is what contains the appointments bound to the Calendar. You can change those items as you see fit.

Regards,
Lance | Tech Support Engineer, Sr.
Progress Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Glenn Henriksen
Top achievements
Rank 1
answered on 26 Jul 2017, 06:34 PM
Ah, that's okay then. Thank you for a very thorough example!
Tags
Calendar & Scheduling
Asked by
virginie
Top achievements
Rank 1
Answers by
Lance | Manager Technical Support
Telerik team
Glenn Henriksen
Top achievements
Rank 1
Share this question
or