|
| 1 | +--- |
| 2 | +sidebar_label: Integration with Angular |
| 3 | +title: Integration with Angular |
| 4 | +description: You can learn about the integration with Angular in the documentation of the DHTMLX JavaScript Booking library. Browse developer guides and API reference, try out code examples and live demos, and download a free 30-day evaluation version of DHTMLX Booking. |
| 5 | +--- |
| 6 | + |
| 7 | +# Integration with Angular |
| 8 | + |
| 9 | +:::tip |
| 10 | +You should be familiar with basic concepts and patterns of **Angular** before reading this documentation. To refresh your knowledge, please refer to the [**Angular documentation**](https://angular.io/docs). |
| 11 | +::: |
| 12 | + |
| 13 | +DHTMLX Booking is compatible with **Angular**. We have prepared code examples on how to use DHTMLX Booking with **Angular**. For more information, refer to the corresponding [**Example on GitHub**](https://github.com/DHTMLX/angular-booking-demo). |
| 14 | + |
| 15 | +## Creating a project |
| 16 | + |
| 17 | +:::info |
| 18 | +Before you start to create a new project, install [**Angular CLI**](https://angular.io/cli) and [**Node.js**](https://nodejs.org/en/). |
| 19 | +::: |
| 20 | + |
| 21 | +Create a new **my-angular-booking-app** project using Angular CLI. Run the following command for this purpose: |
| 22 | + |
| 23 | +~~~json |
| 24 | +ng new my-angular-booking-app |
| 25 | +~~~ |
| 26 | + |
| 27 | +:::note |
| 28 | +If you want to follow this guide, disable Server-Side Rendering (SSR) and Static Site Generation (SSG/Prerendering) when creating new Angular app! |
| 29 | +::: |
| 30 | + |
| 31 | +The command above installs all the necessary tools, so you don't need to run any additional commands. |
| 32 | + |
| 33 | +### Installation of dependencies |
| 34 | + |
| 35 | +Go to the new created app directory: |
| 36 | + |
| 37 | +~~~json |
| 38 | +cd my-angular-booking-app |
| 39 | +~~~ |
| 40 | + |
| 41 | +Install dependencies and start the dev server. For this, use the [**yarn**](https://yarnpkg.com/) package manager: |
| 42 | + |
| 43 | +~~~json |
| 44 | +yarn |
| 45 | +yarn start |
| 46 | +~~~ |
| 47 | + |
| 48 | +The app should run on a localhost (for instance `http://localhost:3000`). |
| 49 | + |
| 50 | +## Creating Booking |
| 51 | + |
| 52 | +Now you should get the DHTMLX Booking source code. First of all, stop the app and proceed with installing the Booking package. |
| 53 | + |
| 54 | +### Step 1. Package installation |
| 55 | + |
| 56 | +Download the [**trial Booking package**](/how-to-start/#installing-trial-booking-via-npm-and-yarn) and follow steps mentioned in the README file. Note that trial Booking is available 30 days only. |
| 57 | + |
| 58 | +### Step 2. Component creation |
| 59 | + |
| 60 | +Now you need to create an Angular component, to add Booking into the application. Create the ***booking*** folder in the ***src/app/*** directory, add a new file into it and name it ***booking.component.ts***. Then complete the steps described below. |
| 61 | + |
| 62 | +#### Import source files |
| 63 | + |
| 64 | +Open the file and import Booking source files. Note that: |
| 65 | + |
| 66 | +- if you use PRO version and install the Booking package from a local folder, the imported path looks like this: |
| 67 | + |
| 68 | +~~~jsx |
| 69 | +import { Booking } from 'dhx-booking-package'; |
| 70 | +~~~ |
| 71 | + |
| 72 | +- if you use the trial version of Booking, specify the following path: |
| 73 | + |
| 74 | +~~~jsx |
| 75 | +import { Booking } from '@dhx/trial-booking'; |
| 76 | +~~~ |
| 77 | + |
| 78 | +:::info |
| 79 | +In this tutorial you can see how to configure the **trial** version of Booking. |
| 80 | +::: |
| 81 | + |
| 82 | +#### Set the container and initialize Booking |
| 83 | + |
| 84 | +To display Booking on the page, you need to set the container to render the component inside and initialize Booking using the corresponding constructor: |
| 85 | + |
| 86 | +~~~jsx {1,8,12-13,18-19} title="booking.component.ts" |
| 87 | +import { Booking } from '@dhx/trial-booking'; |
| 88 | +import { Component, ElementRef, OnInit, ViewChild, OnDestroy, ViewEncapsulation } from '@angular/core'; |
| 89 | + |
| 90 | +@Component({ |
| 91 | + encapsulation: ViewEncapsulation.None, |
| 92 | + selector: "booking", // a template name used in the "app.component.ts" file as <booking /> |
| 93 | + styleUrls: ["./booking.component.css"], // include a css file |
| 94 | + template: `<div #container class="widget"></div>`, |
| 95 | +}) |
| 96 | + |
| 97 | +export class BookingComponent implements OnInit, OnDestroy { |
| 98 | + // initialize container for Booking |
| 99 | + @ViewChild('container', { static: true }) booking_container!: ElementRef; |
| 100 | + |
| 101 | + private _booking!: Booking; |
| 102 | + |
| 103 | + ngOnInit() { |
| 104 | + // initialize the Booking component |
| 105 | + this._booking = new Booking(this.booking_container.nativeElement, {}); |
| 106 | + } |
| 107 | + |
| 108 | + ngOnDestroy(): void { |
| 109 | + this._booking.destructor(); // destruct Booking |
| 110 | + } |
| 111 | +} |
| 112 | +~~~ |
| 113 | + |
| 114 | +#### Adding styles |
| 115 | + |
| 116 | +To display Booking correctly, you need to provide the corresponding styles. For this purpose, you can create the ***booking.component.css*** file in the **src/app/booking/** directory and specify important styles for Booking and its container: |
| 117 | + |
| 118 | +~~~css title="booking.component.css" |
| 119 | +/* import Booking styles */ |
| 120 | +@import "@dhx/trial-booking/dist/booking.css"; |
| 121 | + |
| 122 | +/* specify styles for initial page */ |
| 123 | +html, |
| 124 | +body { |
| 125 | + margin: 0; |
| 126 | + padding: 0; |
| 127 | + height: 100%; |
| 128 | +} |
| 129 | + |
| 130 | +/* specify styles for the Booking container */ |
| 131 | +.widget { |
| 132 | + height: 100%; |
| 133 | +} |
| 134 | +~~~ |
| 135 | + |
| 136 | +#### Loading data |
| 137 | + |
| 138 | +To add data into Booking, you need to provide a data set. You can create the ***data.ts*** file in the ***src/app/booking/*** directory and add some data into it: |
| 139 | + |
| 140 | +~~~jsx title="data.ts" |
| 141 | +export function getData() : any { |
| 142 | + function getDate(addDays : any, hoursValue = 0, minutesValue = 0) { |
| 143 | + const date = new Date(); |
| 144 | + const secondsValue = 0; // round to minutes |
| 145 | + const msValue = 0; |
| 146 | + |
| 147 | + date.setDate(date.getDate() + addDays); |
| 148 | + date.setHours(hoursValue, minutesValue, secondsValue, msValue); |
| 149 | + |
| 150 | + return date.getTime(); |
| 151 | + } |
| 152 | + |
| 153 | + return [ |
| 154 | + { |
| 155 | + id: "ee828b5d-a034-420c-889b-978840015d6a", |
| 156 | + title: "Natalie Tyson", |
| 157 | + category: "Therapist", |
| 158 | + subtitle: "2 years of experiece", |
| 159 | + details: "Cleveland Clinic\n9500 Euclid Ave", |
| 160 | + preview: "https://snippet.dhtmlx.com/codebase/data/booking/01/img/01.jpg", |
| 161 | + price: "$35", |
| 162 | + review: { |
| 163 | + stars: 4, |
| 164 | + count: 120 |
| 165 | + }, |
| 166 | + slots: [ |
| 167 | + { |
| 168 | + from: 9, |
| 169 | + to: 20, |
| 170 | + days: [1, 2, 3, 4, 5] |
| 171 | + }, |
| 172 | + { |
| 173 | + from: 10, |
| 174 | + to: 18, |
| 175 | + days: [6, 0] |
| 176 | + } |
| 177 | + ] |
| 178 | + }, |
| 179 | + { |
| 180 | + id: "9b037564-77be-429f-b719-eebbe499027a", |
| 181 | + title: "Emma Johnson", |
| 182 | + category: "Cardiologist", |
| 183 | + subtitle: "2 years of experience", |
| 184 | + details: "Stanford Health Care\n1468 Madison Ave", |
| 185 | + preview: "https://snippet.dhtmlx.com/codebase/data/booking/01/img/03.jpg", |
| 186 | + price: "$25", |
| 187 | + review: { |
| 188 | + stars: 5, |
| 189 | + count: 10 |
| 190 | + }, |
| 191 | + slots: [ |
| 192 | + { |
| 193 | + from: 14, |
| 194 | + to: 17, |
| 195 | + size: 30, |
| 196 | + gap: 10 |
| 197 | + }, |
| 198 | + { |
| 199 | + from: 12, |
| 200 | + to: 19, |
| 201 | + size: 50, |
| 202 | + gap: 20, |
| 203 | + days: [2], |
| 204 | + dates: [getDate(0)] |
| 205 | + }, |
| 206 | + { |
| 207 | + from: "18:30", |
| 208 | + to: 20, |
| 209 | + size: 20, |
| 210 | + gap: 20, |
| 211 | + days: [3, 4, 5] |
| 212 | + }, |
| 213 | + ], |
| 214 | + usedSlots: [getDate(0, 12), getDate(0, 18)] |
| 215 | + }, |
| 216 | + // ... |
| 217 | + ]; |
| 218 | +} |
| 219 | +~~~ |
| 220 | + |
| 221 | +Then open the ***booking.component.ts*** file. Import the file with data and specify the corresponding data properties to the configuration object of Booking within the `ngOnInit()` method, as shown below. |
| 222 | + |
| 223 | +~~~jsx {2,18,20} title="booking.component.ts" |
| 224 | +import { Booking } from '@dhx/trial-booking'; |
| 225 | +import { getData } from "./data"; // import data |
| 226 | +import { Component, ElementRef, OnInit, ViewChild, OnDestroy, ViewEncapsulation } from '@angular/core'; |
| 227 | + |
| 228 | +@Component({ |
| 229 | + encapsulation: ViewEncapsulation.None, |
| 230 | + selector: "booking", |
| 231 | + styleUrls: ["./booking.component.css"], |
| 232 | + template: `<div #container class="widget"></div>`, |
| 233 | +}) |
| 234 | + |
| 235 | +export class BookingComponent implements OnInit, OnDestroy { |
| 236 | + @ViewChild('container', { static: true }) booking_container!: ElementRef; |
| 237 | + |
| 238 | + private _booking!: Booking; |
| 239 | + |
| 240 | + ngOnInit() { |
| 241 | + const data = getData(); // initialize data property |
| 242 | + this._booking = new Booking(this.booking_container.nativeElement, { |
| 243 | + data |
| 244 | + }); |
| 245 | + } |
| 246 | + |
| 247 | + ngOnDestroy(): void { |
| 248 | + this._booking.destructor(); |
| 249 | + } |
| 250 | +} |
| 251 | +~~~ |
| 252 | + |
| 253 | +Now the Booking component is ready to use. When the element will be added to the page, it will initialize the Booking with data. You can provide necessary configuration settings as well. Visit our [Booking API docs](/api/overview/booking-properties-overview/) to check the full list of available properties. |
| 254 | + |
| 255 | +#### Handling events |
| 256 | + |
| 257 | +When a user makes some action in the Booking, 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/booking-events-overview/). |
| 258 | + |
| 259 | +Open the ***booking.component.ts*** file and complete the `ngOnInit()` method as in: |
| 260 | + |
| 261 | +~~~jsx {7-10} title="booking.component.ts" |
| 262 | +// ... |
| 263 | +ngOnInit() { |
| 264 | + this._booking = new Booking(this.booking_container.nativeElement, { |
| 265 | + date: new Date(2024, 5, 10), |
| 266 | + }); |
| 267 | + |
| 268 | + // output the id of the selected slot |
| 269 | + this._booking.api.on("select-slot", (obj) => { |
| 270 | + console.log(obj.id); |
| 271 | + }); |
| 272 | +} |
| 273 | + |
| 274 | +ngOnDestroy(): void { |
| 275 | + this._booking.destructor(); |
| 276 | +} |
| 277 | +~~~ |
| 278 | + |
| 279 | +### Step 3. Adding Booking into the app |
| 280 | + |
| 281 | +To add the ***BookingComponent*** component into the app, open the ***src/app/app.component.ts*** file and replace the default code with the following one: |
| 282 | + |
| 283 | +~~~jsx {5} title="app.component.ts" |
| 284 | +import { Component } from "@angular/core"; |
| 285 | + |
| 286 | +@Component({ |
| 287 | + selector: "app-root", |
| 288 | + template: `<booking/>` // a template created in the "booking.component.ts" file |
| 289 | +}) |
| 290 | +export class AppComponent { |
| 291 | + name = ""; |
| 292 | +} |
| 293 | +~~~ |
| 294 | + |
| 295 | +Then create the ***app.module.ts*** file in the ***src/app/*** directory and specify the *BookingComponent* as shown below: |
| 296 | + |
| 297 | +~~~jsx {4-5,8} title="app.module.ts" |
| 298 | +import { NgModule } from "@angular/core"; |
| 299 | +import { BrowserModule } from "@angular/platform-browser"; |
| 300 | + |
| 301 | +import { AppComponent } from "./app.component"; |
| 302 | +import { BookingComponent } from "./booking/booking.component"; |
| 303 | + |
| 304 | +@NgModule({ |
| 305 | + declarations: [AppComponent, BookingComponent], |
| 306 | + imports: [BrowserModule], |
| 307 | + bootstrap: [AppComponent] |
| 308 | +}) |
| 309 | +export class AppModule {} |
| 310 | +~~~ |
| 311 | + |
| 312 | +The last step is to open the ***src/main.ts*** file and replace the existing code with the following one: |
| 313 | + |
| 314 | +~~~jsx title="main.ts" |
| 315 | +import { platformBrowserDynamic } from "@angular/platform-browser-dynamic"; |
| 316 | +import { AppModule } from "./app/app.module"; |
| 317 | +platformBrowserDynamic() |
| 318 | + .bootstrapModule(AppModule) |
| 319 | + .catch((err) => console.error(err)); |
| 320 | +~~~ |
| 321 | + |
| 322 | +After that, you can start the app to see Booking loaded with data on a page. |
| 323 | + |
| 324 | + |
| 325 | + |
| 326 | +Now you know how to integrate DHTMLX Booking with Angular. You can customize the code according to your specific requirements. The final example you can find on [**GitHub**](https://github.com/DHTMLX/angular-booking-demo). |
0 commit comments