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

Saving entry in scheduler

Hi, when I add an entry to the schedule it doesn't save, how do I do this as I can't find any examples. Thanks!


1 Reply

RV Ravikumar Venkatesan Syncfusion Team March 15, 2023 06:30 PM UTC

Hi Gavin,


UG: https://blazor.syncfusion.com/documentation/scheduler/data-binding#using-custom-adaptor


You can save an entry in the Schedule using the CustomAdaptor as highlighted in the below code snippet. Try the shared sample and let us know if you need any further assistance. Refer to the above UG for more details.


[index.razor]

@using Syncfusion.Blazor.Schedule

@using Syncfusion.Blazor.Data

@using syncfusion_blazor_app.Data

 

<SfSchedule @ref="AvailabilityScheduleRef" TValue="AppointmentData" Width="100%" Height="600px" @bind-SelectedDate="@SelectedDate">

    <ScheduleEventSettings TValue="AppointmentData" Query="@QueryData">

        <SfDataManager AdaptorInstance="@typeof(AppointmentDataAdaptor)" Adaptor="Adaptors.CustomAdaptor">

        </SfDataManager>

    </ScheduleEventSettings>

</SfSchedule>


[AppointmentDataAdaptor.cs]

namespace syncfusion_blazor_app.Data {

    public class AppointmentDataAdaptor : DataAdaptor {

        private readonly AppointmentDataService _appService;

        public AppointmentDataAdaptor(AppointmentDataService appService) {

            _appService = appService;

        }

 

        List<AppointmentData>? EventData;

        public override async Task<object> ReadAsync(DataManagerRequest dataManagerRequest, string key = null) {

            await Task.Delay(100); //To mimic asynchronous operation, we delayed this operation using Task.Delay

            EventData = await _appService.Get();

            return dataManagerRequest.RequiresCounts ? new DataResult() { Result = EventData, Count = EventData.Count() } : EventData;

        }

        public async override Task<object> InsertAsync(DataManager dataManager, object data, string key) {

            await Task.Delay(100); //To mimic asynchronous operation, we delayed this operation using Task.Delay

            await _appService.Insert(data as AppointmentData);

            return data;

        }

        public async override Task<object> UpdateAsync(DataManager dataManager, object data, string keyField, string key) {

            await Task.Delay(100); //To mimic asynchronous operation, we delayed this operation using Task.Delay

            await _appService.Update(data as AppointmentData);

            return data;

        }

        public async override Task<object> RemoveAsync(DataManager dataManager, object data, string keyField, string key) //triggers on appointment deletion through public method DeleteEvent

        {

            await Task.Delay(100); //To mimic asynchronous operation, we delayed this operation using Task.Delay

            var app = await _appService.GetByID((int)data);

            await _appService.Delete(app);

            return data;

        }

        public async override Task<object> BatchUpdateAsync(DataManager dataManager, object changedRecords, object addedRecords, object deletedRecords, string keyField, string key, int? dropIndex)             {

            await Task.Delay(100); //To mimic asynchronous operation, we delayed this operation using Task.Delay

            object records = deletedRecords;

            List<AppointmentData>? deleteData = deletedRecords as List<AppointmentData>;

            if(deleteData != null) {

                foreach (var data in deleteData) {

                    await _appService.Delete(data as AppointmentData);

                }

            }

            List<AppointmentData>? addData = addedRecords as List<AppointmentData>;

            if(addData != null) {

                foreach (var data in addData) {

                    await _appService.Insert(data as AppointmentData);

                    records = addedRecords;

                }

            }

            List<AppointmentData>? updateData = changedRecords as List<AppointmentData>;

            if (updateData != null) {

                foreach (var data in updateData) {

                    await _appService.Update(data as AppointmentData);

                    records = changedRecords;

                }

            }

            return records;

        }

    }

}


[AppointmentDataService.cs]

namespace syncfusion_blazor_app.Data

{

    public class AppointmentDataService

    {

        private readonly AppointmentDataContext _appointmentDataContext;

 

        public AppointmentDataService(AppointmentDataContext appDBContext)

        {

            _appointmentDataContext = appDBContext;

        }

 

        public async Task<List<AppointmentData>> Get()

        {

            return await _appointmentDataContext.AppointmentDatas.ToListAsync();

        }

 

        public async Task Insert(AppointmentData appointment)

        {

            var app = new AppointmentData();

            app.Id = appointment.Id;

            app.UserID = appointment.UserID;

            app.Subject = appointment.Subject;

            app.StartTime = appointment.StartTime;

            app.EndTime = appointment.EndTime;

            app.IsAllDay = appointment.IsAllDay;

            app.Location = appointment.Location;

            app.Description = appointment.Description;

            app.RecurrenceRule = appointment.RecurrenceRule;

            app.RecurrenceID = appointment.RecurrenceID;

            app.RecurrenceException = appointment.RecurrenceException;

            app.StartTimezone = appointment.StartTimezone;

            app.EndTimezone = appointment.EndTimezone;

            app.IsReadOnly = appointment.IsReadOnly;

            await _appointmentDataContext.AppointmentDatas.AddAsync(app);

            await _appointmentDataContext.SaveChangesAsync();

        }

 

        public async Task<AppointmentData> GetByID(int Id)

        {

            var app = await _appointmentDataContext.AppointmentDatas.FirstAsync(c => c.Id == Id);

            return app;

        }

 

        public async Task Update(AppointmentData appointment)

        {

            var app = await _appointmentDataContext.AppointmentDatas.FirstAsync(c => c.Id == appointment.Id);

 

            if (app != null)

            {

                app.UserID = appointment.UserID;

                app.Subject = appointment.Subject;

                app.StartTime = appointment.StartTime;

                app.EndTime = appointment.EndTime;

                app.IsAllDay = appointment.IsAllDay;

                app.Location = appointment.Location;

                app.Description = appointment.Description;

                app.RecurrenceRule = appointment.RecurrenceRule;

                app.RecurrenceID = appointment.RecurrenceID;

                app.RecurrenceException = appointment.RecurrenceException;

                app.StartTimezone = appointment.StartTimezone;

                app.EndTimezone = appointment.EndTimezone;

                app.IsReadOnly = appointment.IsReadOnly;

 

                _appointmentDataContext.AppointmentDatas?.Update(app);

                await _appointmentDataContext.SaveChangesAsync();

            }

        }

 

        public async Task Delete(AppointmentData appointment)

        {

            var app = await _appointmentDataContext.AppointmentDatas.FirstAsync(c => c.Id == appointment.Id);

 

            if (app != null)

            {

                _appointmentDataContext.AppointmentDatas?.Remove(app);

                await _appointmentDataContext.SaveChangesAsync();

            }

        }

    }

}


Regards,

Ravikumar Venkatesan


Attachment: syncfusion_blazor_scheduler_custom_adaptor_c9990bae.zip

Loader.
Live Chat Icon For mobile
Up arrow icon