• 「实用场景教程」如何用日程控件DHTMLX Scheduler制作酒店预订日历?(二)


    dhtmlxScheduler是一个类似于Google日历的JavaScript日程安排控件,日历事件通过Ajax动态加载,支持通过拖放功能调整事件日期和时间,事件可以按天,周,月三个种视图显示。

    DHTMLX Scheduler正式版下载

    在本教程中,我们将使用两个强大的工具:DHTMLX Scheduler库和Angular框架来创建一个全面的酒店客房预订应用程序。在上文中(点击这里回顾>>)我们为大家介绍了一些基础的准备工作及如何创建Scheduler组件,本文将继续介绍数据相关的问题、服务器配置等。

    Step 4 – 提供和保存数据
    数据加载

    要在Angular Scheduler中添加数据加载,您需要添加reservation和collections服务。但在此之前,让我们为项目创建并配置一个环境文件,执行如下命令:

    ng generate environments

    在src/environments文件夹下新创建的environment.development.ts文件中,我们将添加以下代码:

    1. export const environment = {
    2. production: false,
    3. apiBaseUrl: 'http://localhost:3000/data'
    4. };

    我们还将为错误创建一个助手,当出现错误时,它将通过向控制台发送错误消息来通知用户。为此,用以下代码在app/services.ts文件夹中创建service- helpers文件:

    1. export function HandleError(error: any): Promise{
    2. console.log(error);
    3. return Promise.reject(error);
    4. }

    现在让我们创建预订和收集服务,执行如下命令:

    ng generate service services/reservation --flat --skip-tests
    ng generate service services/collections --flat --skip-tests

    在services文件夹中新创建的reservation.service.ts文件中,我们将添加以下代码:

    1. import { Injectable } from '@angular/core';
    2. import { Reservation } from "../models/reservation";
    3. import { HttpClient } from "@angular/common/http";
    4. import { HandleError } from "./service-helper";
    5. import { firstValueFrom } from 'rxjs';
    6. import { environment } from '../../environments/environment.development';
    7. @Injectable()
    8. export class ReservationService {
    9. private reservationUrl = `${environment.apiBaseUrl}/reservations`;
    10. constructor(private http: HttpClient) { }
    11. get(): Promise<Reservation[]>{
    12. return firstValueFrom(this.http.get(this.reservationUrl))
    13. .catch(HandleError);
    14. }
    15. insert(reservation: Reservation): Promise<Reservation> {
    16. return firstValueFrom(this.http.post(this.reservationUrl, reservation))
    17. .catch(HandleError);
    18. }
    19. update(reservation: Reservation): Promise<void> {
    20. return firstValueFrom(this.http.put(`${this.reservationUrl}/${reservation.id}`, reservation))
    21. .catch(HandleError);
    22. }
    23. remove(id: number): Promise<void> {
    24. return firstValueFrom(this.http.delete(`${this.reservationUrl}/${id}`))
    25. .catch(HandleError);
    26. }
    27. }

    在新创建的collections.service.ts文件中,添加以下代码行:

    1. import { Injectable } from '@angular/core';
    2. import { Room } from "../models/room.model";
    3. import { RoomType } from "../models/room-type.model";
    4. import { CleaningStatus } from "../models/cleaning-status.model";
    5. import { BookingStatus } from "../models/booking-status.model";
    6. import { HttpClient } from "@angular/common/http";
    7. import { HandleError } from "./service-helper";
    8. import { firstValueFrom } from 'rxjs';
    9. import { environment } from '../../environments/environment.development';
    10. @Injectable()
    11. export class CollectionsService {
    12. private collectionsUrl = `${environment.apiBaseUrl}/collections`;
    13. constructor(private http: HttpClient) { }
    14. getRooms(): Promise<Room[]>{
    15. return firstValueFrom(this.http.get(`${this.collectionsUrl}/rooms`))
    16. .catch(HandleError);
    17. }
    18. updateRoom(room: Room): Promise<void> {
    19. return firstValueFrom(this.http.put(`${this.collectionsUrl}/rooms/${room.id}`, room))
    20. .catch(HandleError);
    21. }
    22. getRoomTypes(): Promise<RoomType[]>{
    23. return firstValueFrom(this.http.get(`${this.collectionsUrl}/roomTypes`))
    24. .catch(HandleError);
    25. }
    26. getCleaningStatuses(): Promise<CleaningStatus[]>{
    27. return firstValueFrom(this.http.get(`${this.collectionsUrl}/cleaningStatuses`))
    28. .catch(HandleError);
    29. }
    30. getBookingStatuses(): Promise<BookingStatus[]>{
    31. return firstValueFrom(this.http.get(`${this.collectionsUrl}/bookingStatuses`))
    32. .catch(HandleError);
    33. }
    34. }

    get()、getRooms()、getRoomTypes()、getCleaningStatuses()和getBookingStatuses()方法从服务器检索数据。

    reservationUrl和collectionurl是服务的私有元素,它们包含REST API的URL。为了发送HTTP请求,一个HTTP类被注入到服务中。

    要插入新项,需要向URL发送POST请求,请求体中包含新项。

    要更新项,需要向url/item_id发送一个PUT请求。此请求还在其主体中包含更新后的项。

    要删除项,需要向url/item_id发送删除请求。

    CRUD操作

    服务应该处理调度器中的CRUD操作,通过在reservations.service.ts和collections.service.ts文件中添加HttpClient模块,HTTP通信已经启用:

    import { HttpClient } from "@angular/common/http";

    这一步允许我们在Angular应用中无缝地获取数据。

    要利用HttpClient模块,还需要从@angular/common/http包中包含必需的HttpClientModule。在app.module.ts文件中,您应该像下面这样更新imports数组:

    1. import { NgModule } from '@angular/core';
    2. import { BrowserModule } from '@angular/platform-browser';
    3. import { AppRoutingModule } from './app-routing.module';
    4. import { AppComponent } from './app.component';
    5. import { SchedulerComponent } from './scheduler/scheduler.component';
    6. import { FormsModule } from '@angular/forms';
    7. import { HttpClientModule } from '@angular/common/http';
    8. @NgModule({
    9. declarations: [
    10. AppComponent,
    11. SchedulerComponent
    12. ],
    13. imports: [
    14. BrowserModule,
    15. AppRoutingModule,
    16. FormsModule,
    17. HttpClientModule
    18. ],
    19. providers: [],
    20. bootstrap: [AppComponent]
    21. })
    22. export class AppModule { }

    HTMLX Scheduler组件应该使用ReservationService和CollectionsService来获取/插入/更新/删除预订和集合,为了启用这些选项,向组件添加ReservationService和CollectionsService。首先在scheduler.component.ts中导入服务所需的模块:

    1. import { ReservationService } from '../services/reservation.service';
    2. import { CollectionsService } from '../services/collections.service';

    您还应该将@Component装饰器中指定EventService作为提供商:

    providers: [ ReservationService, CollectionsService ]

    现在每次初始化一个新的SchedulerComponent时,都会创建一个新的服务实例。

    服务应该准备好被注入到组件中。为此,将以下构造函数添加到SchedulerComponent类中:

    1. constructor(
    2. private reservationService: ReservationService,
    3. private collectionsService: CollectionsService
    4. ) { }

    接下来,我们将添加updateRoom()方法来在数据库中保存room清洁状态的更改:

    1. handleCleaningStatusChange(target: HTMLSelectElement) {
    2. ...
    3. this.collectionsService.updateRoom(roomToUpdate);
    4. }

    您需要修改ngOnInit函数来调用服务获取该函数,然后等待响应来将数据放入调度器。

    1. scheduler.init(this.schedulerContainer.nativeElement, new Date(), 'timeline');
    2. const dp = scheduler.createDataProcessor({
    3. event: {
    4. create: (data: Reservation) => this.reservationService.insert(data),
    5. update: (data: Reservation) => this.reservationService.update(data),
    6. delete: (id: number) => this.reservationService.remove(id),
    7. }
    8. });
    9. forkJoin({
    10. reservations: this.reservationService.get(),
    11. rooms: this.collectionsService.getRooms(),
    12. roomTypes: this.collectionsService.getRoomTypes(),
    13. cleaningStatuses: this.collectionsService.getCleaningStatuses(),
    14. bookingStatuses: this.collectionsService.getBookingStatuses()
    15. }).subscribe({
    16. next: ({ reservations, rooms, roomTypes, cleaningStatuses, bookingStatuses }) => {
    17. const data = {
    18. events: reservations,
    19. collections: {
    20. rooms,
    21. roomTypes,
    22. cleaningStatuses,
    23. bookingStatuses,
    24. }
    25. };
    26. scheduler.parse(data);
    27. },
    28. error: error => {
    29. console.error('An error occurred:', error);
    30. }
    31. });

    scheduler.parse接受JSON格式的数据对象,为了有效地等待多个异步请求的完成并将它们的数据(保留和集合)加载到调度器中,可以利用RxJS库中的forkJoin操作符。请包括导入:

    import { forkJoin } from 'rxjs';

    你可以在GitHub上查看scheduler.components.ts文件的完整代码。

  • 相关阅读:
    Spring boot与Spring cloud 之间的关系
    2022“杭电杯”中国大学生算法设计超级联赛(第一场) to be continued
    前端项目使用钉钉字体
    Katalon框架测试web(二十二)配置git与git项目创建
    前端请求XMLHttpRequest详解
    JSP简单学习
    IDEA2022.3 @Slf4j cannot find symbol 无法解析问题处理
    如何使用STL中的模板类
    聚焦“硬核”技术,开放原子开源大赛苏州站圆满落幕
    Open set
  • 原文地址:https://blog.csdn.net/AABBbaby/article/details/134547072