We have syncfusion scheduler with javascript. We are using CustomDataAdapter where we call ajax and bind the control. Now our new requirement is to add some dummy events on scheduler but scheduleObj.addEvent or deleteEvent never calls CustomAdaptor's addRecord or deleteRecord.
Please find attached sample to keep it simple we have removed ajax and used static object for data within adaptor.
const customDataAdaptor = new ej.data.CustomDataAdaptor({ getData: function (options) { // Simulate a successful fetch of event data options.onSuccess(eventData); }, batchUpdate: function (options) { let data = JSON.parse(options.data); if(data.added.length > 0){ //add event data.added.forEach((record) => { const newEvent = { ...record, Id: eventData.length + 1 }; // Assign a new ID eventData.push(newEvent); // Add to the dummy data array options.onSuccess(newEvent); }); } if(data.changed.length > 0){ // change event data.changed.forEach((record) => { const index = eventData.findIndex(event => event.Id === record.Id); if (index !== -1) { eventData[index] = record; // Update the event options.onSuccess(record); } else { options.onFailure('Event not found'); } }); } if(data.deleted.length > 0){ // delete event data.deleted.forEach((record) => { const index = eventData.findIndex(event => event.Id === record.Id); if (index !== -1) { eventData.splice(index, 1); // Remove the event from the array options.onSuccess(eventData); } else { options.onFailure('Event not found'); } }); } }, }); // Add Event for Nancy document.getElementById('addEventBtn').onclick = function () { const newEvent = { Id: eventData.length + 1, // New ID Subject: 'New Event for Nancy', StartTime: new Date(2023, 8, 14), // Start time (all day) EndTime: new Date(2023, 8, 15), // End time (next day) IsAllDay: true, ResourceId: [1] // Assign to Nancy }; scheduleObj.addEvent(newEvent); // Add event to the Scheduler }; // Delete Event for Nancy document.getElementById('deleteEventBtn').onclick = function () { const eventToDelete = eventData.find(event => event.Subject === 'New Event for Nancy' && event.ResourceId.includes(1)); if (eventToDelete) { scheduleObj.deleteEvent(eventToDelete, 'Delete'); // Delete event from the Scheduler } else { alert('No event found for Nancy to delete.'); } }; |
Hi Swathi
Thank you so much for responding. It was quite a blocker for us.
Batch update is being called now fine. Although we couldn't find it in any documentation.
And we have 2 more questions:
1>When does addRecord and deleteRecord of Adaptor gets called? only if we use array based datasource?
2>Right now, we are adding dummy events for some kind of preview, so on bacthUpdate how do we add them if we are not using array based datasource but custom api. How do we update data for scheduler from batchUpdate? and remove also?
Thanks
Grishma
batchUpdate: function (options) { let optionsData = JSON.parse(options.data); createRequest(baseUrl + 'your-method-name', optionsData); } |
@Swathi
On batch update, we don't need to send anything to server. But when we call onSuccess from batch on add/delete event it is triggering "getData" method of adapter which we don't want. Hence we have put flag in bacthUpdate as true and within getData if that flag is truewe do not call ajax . But this is not a clean solution so if there is any better solution please let us know.