Vue Scheduler - Add Appointments

User Interaction

To add an appointment, a user follows the steps listed below.

  1. Clicks a cell in the timetable to set focus on it.
  2. Clicks the focused cell once again. The appointment details form will be shown.
  3. In the form, specifies required fields and clicks the "Done" button. This will create an appointment and add it to the data source.

To prevent an end user from adding an appointment, set the editing.allowAdding property to false.

jQuery
JavaScript
$(function() {
    $("#schedulerContainer").dxScheduler({ 
        // ...
        editing: { allowAdding: false }
    });
});
Angular
HTML
TypeScript
<dx-scheduler ... >
    <dxo-editing [allowAdding]="false"></dxo-editing>
</dx-scheduler>
import { DxSchedulerModule } from "devextreme-angular";
// ...
export class AppComponent {
    // ...
}
@NgModule({
    imports: [
        // ...
        DxSchedulerModule
    ],
    // ...
})
Vue
App.vue
<template>
    <DxScheduler ... >
        <DxEditing :allow-adding="true" />
    </DxScheduler>
</template>

<script>
import 'devextreme/dist/css/dx.light.css';

import DxScheduler, { DxEditing } from 'devextreme-vue/scheduler';

export default {
    components: {
        DxScheduler,
        DxEditing
    },
    data() {
        return {
            // ...
        }
    }
}
</script>
React
App.js
import React from 'react';

import 'devextreme/dist/css/dx.light.css';

import Scheduler, { Editing } from 'devextreme-react/scheduler';

class App extends React.Component {
    render() {
        return (
            <Scheduler ... >
                <Editing allowAdding={true} />
            </Scheduler>
        );
    }
}
export default App;

API

To add an appointment to the data source, call the addAppointment(appointment) method. Note that the structure of the added appointment should be the same as the UI component's data source items.

jQuery
JavaScript
$(function() {
    var scheduler = $("#schedulerContainer").dxScheduler({
        dataSource: [{
            text: "Planning",
            startDate: new Date("2016-04-25T09:00:00.000Z"),
            endDate: new Date("2016-04-25T09:30:00.000Z")
        }, 
        // ...
        ],
        currentDate: new Date(2016, 4, 25)
    }).dxScheduler("instance");

    $("#addButton").dxButton({
        text: "Add",
        onClick: function () {
            scheduler.addAppointment({
                text: "Website Re-Design Plan",
                startDate: new Date("2016-04-25T09:30:00.000Z"),
                endDate: new Date("2016-04-25T11:30:00.000Z")
            });
        }
    });
});
Angular
HTML
TypeScript
<dx-scheduler
    [(dataSource)]="appointments"
    [currentDate]="currentDate">
</dx-scheduler>

<dx-button
    text="Add"
    (onClick)="addAppointment()">
</dx-button>
import { ..., ViewChild } from "@angular/core";
import { DxButtonModule, DxSchedulerModule, DxSchedulerComponent } from "devextreme-angular";
// ...
export class AppComponent  {
    @ViewChild(DxSchedulerComponent, { static: false }) scheduler: DxSchedulerComponent;
    // Prior to Angular 8
    // @ViewChild(DxSchedulerComponent) scheduler: DxSchedulerComponent;
    appointments = [{
        text: "Planning",
        startDate: new Date("2016-04-25T09:00:00.000Z"),
        endDate: new Date("2016-04-25T09:30:00.000Z")
    }, 
    // ...
    ];
    currentDate = new Date(2016, 4, 25);

    addAppointment() {
        this.scheduler.instance.addAppointment({
            text: "Website Re-Design Plan",
            startDate: new Date("2016-04-25T01:30:00.000Z"),
            endDate: new Date("2016-04-25T02:30:00.000Z")
        });
    }
}
@NgModule({
    imports: [
        // ...
        DxSchedulerModule,
        DxButtonModule
    ],
    // ...
})
Vue
App.vue
<template>
    <div>
        <DxScheduler
            :data-source="dataSource"
            :current-date="currentDate"
            :ref="schedulerRefKey"
        />
        <DxButton
            text="Add"
            @click="addAppontment"
        />
    </div>
</template>

<script>
import 'devextreme/dist/css/dx.light.css';

import DxScheduler from 'devextreme-vue/scheduler';
import DxButton from 'devextreme-vue/button';

const schedulerRefKey = "my-scheduler";

export default {
    components: {
        DxScheduler,
        DxButton
    },
    data() {
        return {
            dataSource: [{
                text: "Planning",
                startDate: new Date("2016-04-25T09:00:00.000Z"),
                endDate: new Date("2016-04-25T09:30:00.000Z")
            }, 
            // ...
            ],
            currentDate: new Date(2016, 4, 25),
            schedulerRefKey
        },
    },
    methods: {
        addAppontment(e) {
            this.scheduler.addAppointment({
                text: "Website Re-Design Plan",
                startDate: new Date("2016-04-25T09:30:00.000Z"),
                endDate: new Date("2016-04-25T11:30:00.000Z")
            });
        }
    },
    computed: {
        scheduler: function() {
            return this.$refs[schedulerRefKey].instance;
        }
    }
}
</script>
React
App.js
import React from 'react';

import 'devextreme/dist/css/dx.light.css';

import Scheduler from 'devextreme-react/scheduler';
import Button from 'devextreme-react/button';

const dataSource = [{
    text: "Planning",
    startDate: new Date("2016-04-25T09:00:00.000Z"),
    endDate: new Date("2016-04-25T09:30:00.000Z")
}, 
// ...
];

class App extends React.Component {
    constructor(props) {
        super(props);

        this.schedulerRef = React.createRef();

        this.addAppointment = () => {
            this.schedulerRef.current.instance.addAppointment({
                text: "Website Re-Design Plan",
                startDate: new Date("2016-04-25T09:30:00.000Z"),
                endDate: new Date("2016-04-25T11:30:00.000Z")
            });
        };
    }
    render() {
        return (
            <React.Fragment>
                <Scheduler
                    dataSource={dataSource}
                    defaultCurrentDate={new Date(2016, 4, 25)}
                    ref={this.schedulerRef}
                />
                <Button
                    text="Add"
                    onClick={this.addAppointment}
                />
            </React.Fragment>
        );
    }
}
export default App;

Events

To execute certain commands before or after an appointment was added, handle the appointmentAdding or appointmentAdded event. If the event handling function is not going to be changed during the lifetime of the UI component, assign it to the corresponding onEventName property when you configure the UI component.

jQuery
JavaScript
$(function () {
    $("#schedulerContainer").dxScheduler({ 
        // ...
        onAppointmentAdding: function (e) {
            // Handler of the "appointmentAdding" event
        },
        onAppointmentAdded: function (e) {
            // Handler of the "appointmentAdded" event
        }
    });
});
Angular
HTML
TypeScript
<dx-scheduler ...
    (onAppointmentAdding)="onAppointmentAdding($event)"
    (onAppointmentAdded)="onAppointmentAdded($event)">
</dx-scheduler>
import { DxSchedulerModule } from "devextreme-angular";
// ...
export class AppComponent {
    onAppointmentAdding (e) {
        // Handler of the "appointmentAdding" event
    }

    onAppointmentAdded (e) {
        // Handler of the "appointmentAdded" event
    }
}
@NgModule({
    imports: [
        // ...
        DxSchedulerModule
    ],
    // ...
})
Vue
App.vue
<template>
    <DxScheduler ...
        @appointment-adding="onAppointmentAdding"
        @appointment-added="onAppointmentAdded"
    />
</template>

<script>
import 'devextreme/dist/css/dx.light.css';

import DxScheduler from 'devextreme-vue/scheduler';

export default {
    components: {
        DxScheduler
    },
    data() {
        return {
            // ...
        }
    },
    methods: {
        onAppointmentAdding: (e) => {
            // Handler of the "appointmentAdding" event
        },
        onAppointmentAdded: (e) => {
            // Handler of the "appointmentAdded" event
        }
    }
}
</script>
React
App.js
import React from 'react';

import 'devextreme/dist/css/dx.light.css';

import Scheduler from 'devextreme-react/scheduler';

const onAppointmentAdding = (e) => {
    // Handler of the "appointmentAdding" event
}
const onAppointmentAdded = (e) => {
    // Handler of the "appointmentAdded" event
}

class App extends React.Component {
    render() {
        return (
            <Scheduler ...
                onAppointmentAdding={onAppointmentAdding}
                onAppointmentAdded={onAppointmentAdded}
            />
        );
    }
}
export default App;
jQuery

If you are going to change event handlers at runtime, or if you need to attach several handlers to a single event, subscribe to the event using the on(eventName, eventHandler) method.

JavaScript
var addedEventHandler1 = function (e) {
    // First handler of the "added" event
};

var addedEventHandler2 = function (e) {
    // Second handler of the "added" event
};

$("#schedulerContainer").dxScheduler("instance")
    .on("appointmentAdded", addedEventHandler1)
    .on("appointmentAdded", addedEventHandler2);
See Also