Skip to content

Commit

Permalink
[update] integration with vue
Browse files Browse the repository at this point in the history
  • Loading branch information
serhiipylypchuk1991 committed Jun 6, 2024
1 parent 5234e1d commit 530875e
Show file tree
Hide file tree
Showing 2 changed files with 280 additions and 14 deletions.
17 changes: 5 additions & 12 deletions docs/guides/integration_with_svelte.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ Check the details in the [related article](https://svelte.dev/docs/introduction#

### Installation of dependencies

Let's name the project as **event-calendar-svelte** and go to the app directory:
Let's name the project as **my-svelte-event-calendar-app** and go to the app directory:

~~~json
cd event-calendar-svelte
cd my-svelte-event-calendar-app
~~~

Install dependencies and run the app. For this, use a package manager:
Expand Down Expand Up @@ -104,14 +104,7 @@ To display Event Calendar on the page, you need to set the container to render t
let container;
</script>

<div bind:this={container} class="container"></div>

<style>
.container {
width: 100%;
height: 100%;
}
</style>
<div bind:this={container} style="width: 100%; height: 100%;"></div>
~~~

Then you need to render Event Calendar in the container. Use the `new EventCalendar()` constructor inside the `onMount()` method of Svelte, to initialize Event Calendar inside of the container:
Expand All @@ -129,7 +122,7 @@ Then you need to render Event Calendar in the container. Use the `new EventCalen
});
</script>

<div bind:this={container} class="container"></div>
<div bind:this={container} style="width: 100%; height: 100%;"></div>
~~~

#### Loading data
Expand Down Expand Up @@ -192,7 +185,7 @@ Open the ***EventCalendar.svelte*** file and apply the passed **props** to the E
});
</script>

<div bind:this={container} class="container"></div>
<div bind:this={container} style="width: 100%; height: 100%;"></div>
~~~

You can also use the `parse()` method inside the `onMount()` method of Svelte to load data into Event Calendar:
Expand Down
277 changes: 275 additions & 2 deletions docs/guides/integration_with_vue.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,279 @@ description: You can learn about the integration with Vue in the documentation o

# Integration with Vue

DHTMLX Event Calendar is compatible with **Vue**. We have prepared code examples of how to use DHTMLX Event Calendar with **Vue**. To check online samples, please refer to the corresponding [**Examples on CodeSandbox**](https://codesandbox.io/u/DHTMLX).
:::tip
You should be familiar with the basic concepts and patterns of [**Vue**](https://vuejs.org/) before reading this documentation. To refresh your knowledge, please refer to the [**Vue 3 documentation**](https://v3.vuejs.org/guide/introduction.html#getting-started).
:::

<iframe src="https://codesandbox.io/p/sandbox/dhtmlx-event-calendar-with-vue3-mr6ks5" frameborder="0" class="snippet_iframe" width="100%" height="700"></iframe>
DHTMLX Event Calendar is compatible with **Vue**. We have prepared code examples on how to use DHTMLX Event Calendar with **Vue 3**. For more information, refer to the corresponding [**Example on GitHub**](https://github.com/DHTMLX/vue-event-calendar-demo).

## Creating a project

:::info
Before you start to create a new project, install [**Node.js**](https://nodejs.org/en/).
:::

To create a **Vue** project, run the following command:

~~~json
npm create vue@latest
~~~

This command installs and executes `create-vue`, the official **Vue** project scaffolding tool. Check the details in the [Vue.js Quick Start](https://vuejs.org/guide/quick-start.html#creating-a-vue-application).

Let's name the project as **my-vue-event-calendar-app**.

### Installation of dependencies

Go to the app directory:

~~~json
cd my-vue-event-calendar-app
~~~

Install dependencies and start the dev server. For this, use a package manager:

- if you use [**yarn**](https://yarnpkg.com/), run the following commands:

~~~json
yarn install
yarn dev
~~~

- if you use [**npm**](https://www.npmjs.com/), run the following commands:

~~~json
npm install
npm run dev
~~~

The app should run on a localhost (for instance `http://localhost:3000`).

## Creating Event Calendar

Now you should get the DHTMLX Event Calendar code. First of all, stop the app and proceed with installing the Event Calendar package.

### Step 1. Package installation

Download the [**trial Event Calendar package**](/how_to_start/#installing-event-calendar-via-npm-and-yarn) and follow steps mentioned in the README file. Note that trial Event Calendar is available 30 days only.

### Step 2. Component creation

Now you need to create a Vue component, to add a Event Calendar into the application. Create a new file in the ***src/components/*** directory and name it ***EventCalendarComponent***.

#### Importing source files

Open the ***EventCalendarComponent.vue*** file and import Event Calendar source files. Note that:

- if you use PRO version and install the Event Calendar package from a local folder, the import paths look like this:

~~~html title="EventCalendarComponent.vue"
import { EventCalendar } from 'dhx-eventcalendar-package';
import 'dhx-eventcalendar-package/dist/event-calendar.css';
~~~

Note that depending on the used package, the source files can be minified. In this case make sure that you are importing the CSS file as **event-calendar.min.css**.

- if you use the trial version of Event Calendar, specify the following paths:

~~~html title="EventCalendarComponent.vue"
import { EventCalendar } from '@dhx/trial-eventcalendar';
import '@dhx/trial-eventcalendar/dist/event-calendar.css';
~~~

In this tutorial you can see how to configure the **trial** version of Event Calendar.

#### Setting the container and adding Event Calendar

To display Event Calendar on the page, you need to set the container to render the component inside. Check the code below:

~~~html {6-8} title="EventCalendarComponent.vue"
<script>
import { EventCalendar } from "@dhx/trial-eventcalendar";
import "@dhx/trial-eventcalendar/dist/event-calendar.css";
</script>

<template>
<div ref="container" style="width: 100%; height: 100%; "></div>
</template>
~~~

Then you need to render Event Calendar in the container. Use the `new EventCalendar()` constructor inside the `mounted()` method of Vue to initialize Event Calendar inside of the container:

~~~html title="EventCalendarComponent.vue"
<script>
export default {
mounted: function() {
this.calendar = new EventCalendar(this.$refs.container, {});
}
};
</script>

<template>
<div ref="container" style="width: 100%; height: 100%; "></div>
</template>
~~~

To clear the component as it has unmounted, use the `calendar.destructor()` call and remove the container after that inside the `unmounted()` method of ***Vue.js***, as follows:

~~~html {7-10} title="EventCalendarComponent.vue"
<script>
export default {
mounted: function() {
this.calendar = new EventCalendar(this.$refs.container, {});
},
unmounted() {
this.calendar.destructor();
this.$refs.container.innerHTML = "";
}
}
</script>

<template>
<div ref="container" style="width: 100%; height: 100%;"></div>
</template>
~~~

#### Loading data

To add data into the Event Calendar, you need to provide a data set. You can create the ***data.js*** file in the ***src/*** directory and add some data into it:

~~~jsx title="data.js"
export function getData() {
return [
{
id: '27',
type: 'work',
start_date: new Date('2024-06-10T14:00:00'),
end_date: new Date('2024-06-10T18:30:00'),
text: ' Olympiastadion - Berlin ',
details: ' Berlin, GER '
},
{
id: '28',
type: 'rest',
start_date: new Date('2024-06-12T14:00:00'),
end_date: new Date('2024-06-12T16:00:00'),
text: ' Commerz Bank Arena ',
details: ' Frankfurt, GER '
},
{
id: '29',
type: 'meeting',
start_date: new Date('2024-06-13T11:00:00'),
end_date: new Date('2024-06-13T16:00:00'),
text: ' Olympic Stadium - Munich ',
details: ' Munich, GER '
}
];
}
~~~

Then open the ***App.vue*** file, import data, and initialize it via the inner `data()` method. After this you can pass data into the new created `<EventCalendar/>` components as **props**:

~~~html {3,9,17} title="App.vue"
<script>
// ...
import { getData } from "./data";
export default {
// ...
data() {
return {
records: getData(),
date: new Date(2024, 5, 10)
};
}
};
</script>

<template>
<EventCalendar :events="records" :date="date" />
</template>
~~~

Open the ***EventCalendarComponent.vue*** file and apply the passed **props** to the Event Calendar configuration object:

~~~html {3,7-8} title="EventCalendarComponent.vue"
<script>
export default {
props: ["events", "date"],
mounted() {
this.calendar = new EventCalendar(this.$refs.container, {
events: this.events,
date: this.date
});
}
}
</script>
~~~

You can also use the `parse()` method inside the `mounted()` method of Vue to load data into Event Calendar:

~~~html {7} title="EventCalendarComponent.vue"
<script>
export default {
props: ["events", "date"],
mounted() {
this.calendar = new EventCalendar(this.$refs.cont, {});
this.calendar.parse({ events: this.events, date: this.date });
}
}
</script>
~~~

Now the Event Calendar component is ready. When the element will be added to the page, it will initialize the Event Calendar object with data. You can provide necessary configuration settings as well. Visit our [Event Calendar API docs](/api/overview/properties_overview/) to check the full list of available properties.

#### Handling events

When a user makes some action in the Event Calendar, it invokes an event. You can use these events to detect the action and run the desired code for it. See the [full list of events](/api/overview/events_overview/).

Open ***EventCalendarComponent.vue*** and complete the `mounted()` method:

~~~html {6-8} title="EventCalendarComponent.vue"
<script>
export default {
// ...
mounted() {
this.calendar = new EventCalendar(this.$refs.cont, {});
this.calendar.events.on("add-event", (obj) => {
console.log(obj);
});
}
}
</script>
~~~

### Step 3. Adding Event Calendar into the app

To add the component into the app, open the **App.vue** file and replace the default code with the following one:

~~~html title="App.vue"
<script>
import EventCalendar from "./components/EventCalendarComponent.vue";
import { getData } from "./data";
export default {
components: { EventCalendar },
data() {
return {
events: getData(),
date: new Date(2024, 5, 10)
};
}
};
</script>

<template>
<EventCalendar :events="records" :date="date" />
</template>
~~~

After that, when you can start the app to see Event Calendar loaded with data on a page.

![Event Calendar initialization](../assets/trial_eventcalendar.png)

Now you know how to integrate DHTMLX Event Calendar with Vue. You can customize the code according to your specific requirements. The final example you can find on [**GitHub**](https://github.com/DHTMLX/vue-event-calendar-demo).

0 comments on commit 530875e

Please sign in to comment.