CRUD operations in Scheduler

Hi,

The datasource I am using to populate the scheduler control is non-updatable.  When I edit a particular appointment, I need to be able to update the underlying table.  How do I do that?

TIA,

Miles


1 Reply

NR Nevitha Ravi Syncfusion Team June 28, 2021 07:51 AM UTC

Hi Miles, 

Greetings from Syncfusion Support. 

You can use our DataManager to fetch and update database. In the following sample we have used CustomAdaptor for your reference to get and update the event from and to the database. 

    <SfSchedule TValue="AppointmentData" Height="550px" CurrentView="View.Week" SelectedDate="@(new DateTime(2018, 4, 19))"> 
    <ScheduleEventSettings TValue="AppointmentData"> 
        <SfDataManager AdaptorInstance="@typeof(CustomAdaptor)" Adaptor="Adaptors.CustomAdaptor"></SfDataManager> 
    </ScheduleEventSettings> 
</SfSchedule> 
@code { 
    static HttpClient client = new HttpClient(); 
    public class CustomAdaptor : DataAdaptor 
    { 
        public async override Task<object> ReadAsync(DataManagerRequest dataManagerRequest, string key = null) //triggers on initial load 
        { 
            var json = await client.GetStringAsync(https://localhost:44382/api/schedule); 
            AppointmentData[] datasource = JsonConvert.DeserializeObject<AppointmentData[]>(json); 
            return dataManagerRequest.RequiresCounts ? new DataResult() { Result = datasource, Count = datasource.Count() } : (object)datasource; 
        } 
        public async override Task<object> InsertAsync(DataManager dataManager, object data, string key) 
        { 
 
            AppointmentData insertData = data as AppointmentData; 
            var formContent = new FormUrlEncodedContent(new[] 
            { 
                new KeyValuePair<string, string>("Id", insertData.Id.ToString()), 
                new KeyValuePair<string, string>("Subject", insertData.Subject), 
                new KeyValuePair<string, string>("StartTime", insertData.StartTime.ToString()), 
                new KeyValuePair<string, string>("EndTime", insertData.EndTime.ToString()), 
                new KeyValuePair<string, string>("RecurrenceID", insertData.RecurrenceID.ToString()), 
                new KeyValuePair<string, string>("RecurrenceRule", insertData.RecurrenceRule), 
                new KeyValuePair<string, string>("RecurrenceException", insertData.RecurrenceException), 
                new KeyValuePair<string, string>("IsAllDay", insertData.IsAllDay.ToString()) 
            }); 
            var json = await client.PostAsync(https://localhost:44382/api/schedule, formContent); 
            return data; 
        } 
 
        public async override Task<object> UpdateAsync(DataManager dataManager, object data, string keyField, string key) 
        { 
 
            AppointmentData updateData = data as AppointmentData; 
            var formContent = new FormUrlEncodedContent(new[] 
            { 
                new KeyValuePair<string, string>("Id", updateData.Id.ToString()), 
                new KeyValuePair<string, string>("Subject", updateData.Subject), 
                new KeyValuePair<string, string>("StartTime", updateData.StartTime.ToString()), 
                new KeyValuePair<string, string>("EndTime", updateData.EndTime.ToString()), 
                new KeyValuePair<string, string>("RecurrenceID", updateData.RecurrenceID.ToString()), 
                new KeyValuePair<string, string>("RecurrenceRule", updateData.RecurrenceRule), 
                new KeyValuePair<string, string>("RecurrenceException", updateData.RecurrenceException), 
                new KeyValuePair<string, string>("IsAllDay", updateData.IsAllDay.ToString()) 
            }); 
            var json = await client.PutAsync(https://localhost:44382/api/schedule, formContent); 
            return data; 
        } 
 
        public async override Task<object> RemoveAsync(DataManager dataManager, object data, string keyField, string key) //triggers on appointment deletion through public method DeleteEvent 
        { 
            var uri = https://localhost:44382/api/schedule/ + data; 
            var json = await client.DeleteAsync(uri); 
            return data; 
        } 
 
        public async override Task<object> BatchUpdateAsync(DataManager dataManager, object changedRecords, object addedRecords, object deletedRecords, string keyField, string key, int? dropIndex) 
        { 
            object records = deletedRecords; 
            List<AppointmentData> deleteData = deletedRecords as List<AppointmentData>; 
            foreach (var data in deleteData) 
            { 
                var uri = https://localhost:44382/api/schedule/ + data.Id; 
                var json = await client.DeleteAsync(uri); 
                records = deletedRecords; 
            } 
            List<AppointmentData> addData = addedRecords as List<AppointmentData>; 
            foreach (var data in addData) 
            { 
                var formContent = new FormUrlEncodedContent(new[] 
               { 
                    new KeyValuePair<string, string>("Id", data.Id.ToString()), 
                    new KeyValuePair<string, string>("Subject", data.Subject), 
                    new KeyValuePair<string, string>("StartTime", data.StartTime.ToString()), 
                    new KeyValuePair<string, string>("EndTime", data.EndTime.ToString()), 
                    new KeyValuePair<string, string>("RecurrenceID", data.RecurrenceID.ToString()), 
                    new KeyValuePair<string, string>("RecurrenceRule", data.RecurrenceRule), 
                    new KeyValuePair<string, string>("RecurrenceException", data.RecurrenceException), 
                    new KeyValuePair<string, string>("IsAllDay", data.IsAllDay.ToString()) 
                }); 
                var json = await client.PostAsync(https://localhost:44382/api/schedule, formContent); 
                records = addedRecords; 
            } 
            List<AppointmentData> updateData = changedRecords as List<AppointmentData>; 
            foreach (var data in updateData) 
            { 
                var formContent = new FormUrlEncodedContent(new[] 
               { 
                    new KeyValuePair<string, string>("Id", data.Id.ToString()), 
                    new KeyValuePair<string, string>("Subject", data.Subject), 
                    new KeyValuePair<string, string>("StartTime", data.StartTime.ToString()), 
                    new KeyValuePair<string, string>("EndTime", data.EndTime.ToString()), 
                    new KeyValuePair<string, string>("RecurrenceID", data.RecurrenceID.ToString()), 
                    new KeyValuePair<string, string>("RecurrenceRule", data.RecurrenceRule), 
                    new KeyValuePair<string, string>("RecurrenceException", data.RecurrenceException), 
                    new KeyValuePair<string, string>("IsAllDay", data.IsAllDay.ToString()) 
                }); 
                var json = await client.PutAsync(https://localhost:44382/api/schedule, formContent); 
                records = changedRecords; 
            } 
            return records; 
        } 
    } 
} 


Kindly try the above sample and get back to us if you need any further assistance. 

Regards, 
Nevitha 


Loader.
Up arrow icon