Unsaved changes

    The request Save sends

    
                

    jQuery Scheduler: Save Changes in a Batch

    Edits made in jqxScheduler are kept as unsaved changes and saved in one batch. The list of changes - and the request that would go to the server - follow every edit, undo and redo; Discard takes them all back with undo, and while a save is under way appointmentAdding, appointmentChanging and appointmentDeleting turn edits down, with a reason the edit dialog shows.

    A live JavaScript/jQuery example of jqxScheduler, the jQWidgets Scheduler component, with its complete source code below. It is one of 45 jqxScheduler demos for jQuery.

    jqxScheduler API used in this demo

    Source code

    To run it in your own page, load jqx.base.css, a theme and the library files below from npm (npm install jqwidgets-scripts) or the jsDelivr CDN, then add the markup and the script. jQuery is not required: jqxcore.js provides $. Getting started with jqxScheduler shows the setup step by step.

    The page loads jqx.base.css, a theme and these library files: jqxcore.js, jqxbuttons.js, jqxscrollbar.js, jqxdata.js, jqxdate.js, jqxscheduler.js, jqxscheduler.api.js, jqxdatetimeinput.js, jqxmenu.js, jqxcalendar.js, jqxtooltip.js, jqxwindow.js, jqxcheckbox.js, jqxlistbox.js, jqxdropdownlist.js, jqxnumberinput.js, jqxradiobutton.js, jqxinput.js.

    It also loads helper scripts of this demo site, which are not part of the library: demos.js (getWidth() and other layout helpers).

    script.js

    $(document).ready(function () {
        // A team calendar whose changes go to a server in one request. What is unsaved is worked out by
        // comparing the appointments with the last saved state - so an edit that is undone, or moved back
        // where it was, is no change at all.
        var at = function (day, hour, minute) { return new Date(2026, 10, day, hour, minute || 0); };
        var meetings = [
            { id: 'm1', subject: 'Planning', location: 'Room 1', start: at(16, 9, 0), end: at(16, 10, 0) },
            { id: 'm2', subject: 'Design review', location: 'Room 2', start: at(17, 10, 0), end: at(17, 11, 30) },
            { id: 'm3', subject: 'Customer demo', location: 'Online', start: at(18, 14, 0), end: at(18, 15, 0) },
            { id: 'm4', subject: 'One-to-one', location: 'Room 3', start: at(19, 11, 0), end: at(19, 11, 30) },
            { id: 'm5', subject: 'Retrospective', location: 'Room 1', start: at(20, 15, 0), end: at(20, 16, 0) }
        ];
        var source = {
            dataType: 'array',
            dataFields: [
                { name: 'id', type: 'string' },
                { name: 'subject', type: 'string' },
                { name: 'location', type: 'string' },
                { name: 'start', type: 'date' },
                { name: 'end', type: 'date' }
            ],
            id: 'id',
            localData: meetings
        };
        $('#scheduler').jqxScheduler({
            date: new $.jqx.date(2026, 11, 18),
            width: '100%',
            height: 480,
            source: new $.jqx.dataAdapter(source),
            view: 'weekView',
            rowsHeight: 24,
            // the meetings have times: no all-day row
            showAllDayRow: false,
            appointmentDataFields: { from: 'start', to: 'end', id: 'id', subject: 'subject', location: 'location' },
            views: [
                { type: 'weekView', showWeekends: false, timeRuler: { formatString: 'HH:mm' } },
                { type: 'dayView', timeRuler: { formatString: 'HH:mm' } }
            ],
            ready: function () {
                $('#scheduler').jqxScheduler('scrollToTime', 8);
            }
        });
    
        // ---- the saved state, and what differs from it ----
        var pad = function (n) { return (n < 10 ? '0' : '') + n; };
        var stamp = function (date) {
            var d = date.toDate();
            return d.getFullYear() + '-' + pad(d.getMonth() + 1) + '-' + pad(d.getDate()) + 'T' + pad(d.getHours()) + ':' + pad(d.getMinutes());
        };
        var record = function (a) {
            return { id: a.id, subject: a.subject, location: a.location || '', start: stamp(a.from), end: stamp(a.to) };
        };
        var saved = {};
        var takeSnapshot = function () {
            saved = {};
            $.each($('#scheduler').jqxScheduler('getAppointments'), function () { saved[this.id] = record(this); });
        };
        var changes = function () {
            var now = {}, result = { added: [], updated: [], deleted: [] };
            $.each($('#scheduler').jqxScheduler('getAppointments'), function () { now[this.id] = record(this); });
            $.each(now, function (id, r) {
                if (!saved[id]) result.added.push(r);
                else if (JSON.stringify(saved[id]) !== JSON.stringify(r)) result.updated.push(r);
            });
            $.each(saved, function (id) {
                if (!now[id]) result.deleted.push(id);
            });
            return result;
        };
    
        var days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
        var when = function (r) {
            var d = new Date(r.start);
            return days[d.getDay()] + ' ' + r.start.slice(11) + '-' + r.end.slice(11);
        };
        var saving = false;
        var render = function () {
            var c = changes(), count = c.added.length + c.updated.length + c.deleted.length;
            var items = [];
            $.each(c.added, function () { items.push(['added', this.subject, when(this)]); });
            $.each(c.updated, function () { items.push(['changed', this.subject, when(saved[this.id]) + ' → ' + when(this)]); });
            $.each(c.deleted, function () { items.push(['deleted', saved[this].subject, when(saved[this])]); });
            $('#changes').empty().append(items.length ? $.map(items, function (i) {
                return $('<li></li>').append($('<span class="kind"></span>').addClass(i[0]).text(i[0]), document.createTextNode(i[1] + ' · ' + i[2]));
            }) : $('<li class="empty"></li>').text('No unsaved changes.'));
            $('#payload').text('POST /api/calendar/changes\n' + JSON.stringify(c, null, 2));
            $('#save').jqxButton({ value: count ? 'Save ' + count + ' change' + (count > 1 ? 's' : '') : 'Save', disabled: !count || saving });
            $('#discard').jqxButton({ disabled: !count || saving });
            if (!saving) {
                $('#state').toggleClass('dirty', count > 0).text(count ? count + ' unsaved' : 'All changes saved');
            }
        };
    
        // ---- save and discard ----
        $('#save').jqxButton({ height: 30, disabled: true }).on('click', function () {
            var c = changes(), count = c.added.length + c.updated.length + c.deleted.length;
            if (!count || saving) return;
            saving = true;
            $('#state').removeClass('dirty').text('Saving…');
            render();
            // a stand-in for $.ajax({ url: '/api/calendar/changes', method: 'POST', data: JSON.stringify(c) })
            setTimeout(function () {
                saving = false;
                takeSnapshot();
                // what is saved cannot be undone any more
                $('#scheduler').jqxScheduler('clearUndoRedo');
                render();
                var t = new Date();
                $('#state').text('Saved ' + count + ' change' + (count > 1 ? 's' : '') + ' at ' + pad(t.getHours()) + ':' + pad(t.getMinutes()) + ':' + pad(t.getSeconds()));
            }, 900);
        });
        $('#discard').jqxButton({ height: 30, disabled: true }).on('click', function () {
            // the history starts at the last save, so undoing all of it is the saved state again
            while ($('#scheduler').jqxScheduler('undo')) { }
            render();
        });
        $('#undo').jqxButton({ height: 30, disabled: true }).on('click', function () { $('#scheduler').jqxScheduler('undo'); });
        $('#redo').jqxButton({ height: 30, disabled: true }).on('click', function () { $('#scheduler').jqxScheduler('redo'); });
        $('#scheduler').on('undoRedoChange', function (event) {
            $('#undo').jqxButton({ disabled: !event.args.canUndo || saving });
            $('#redo').jqxButton({ disabled: !event.args.canRedo || saving });
        });
    
        // no edits while the request is under way; the edit dialog shows the reason
        $('#scheduler').on('appointmentAdding appointmentChanging appointmentDeleting', function (event) {
            if (saving) {
                event.args.cancel = true;
                event.args.reason = { type: 'saving', message: 'Saving - try again in a moment' };
            }
        });
        // every change, undo and redo included (event.args.action), updates the list
        $('#scheduler').on('appointmentAdd appointmentChange appointmentDelete', render);
    
        takeSnapshot();
        render();
    });

    body.html

    <div id="content" class="demo">
        
        <div class="demo-bar">
            <input type="button" id="save" value="Save">
            <input type="button" id="discard" value="Discard">
            <span class="sep"></span>
            <input type="button" id="undo" value="Undo">
            <input type="button" id="redo" value="Redo">
            <span id="state" class="demo-state" role="status" aria-live="polite"></span>
        </div>
        <div id="scheduler"></div>
        <div class="demo-panels">
            <div>
                <h3>Unsaved changes</h3>
                <ul id="changes" class="demo-changes"></ul>
            </div>
            <div>
                <h3>The request Save sends</h3>
                <pre id="payload" class="demo-payload"></pre>
            </div>
        </div>
    </div>

    Learn more

    More jQuery Scheduler demos