gpt4 book ai didi

angular - 如何从此 PrimeNG FullCalendar 动态删除事件(单击事件)?

转载 作者:行者123 更新时间:2023-12-03 15:54:39 31 4
gpt4 key购买 nike

我正在使用 PrimeNG Full Calendar 组件开发一个 Angular 应用程序,这个:https://primefaces.org/primeng/showcase/#/fullcalendar

这是基于 Angular FullCalendar 组件的,这个:https://fullcalendar.io/

在这里你可以找到我的完整代码:https://bitbucket.org/dgs_poste_team/soc_calendar/src/master/

所以我有一个这样的日历:

enter image description here

我在这个阶段要实现的是,当用户单击特定事件时,该日历中的所有事件都将被删除(在第二阶段,我将实现仅删除特定单击的事件,但在时刻我保持逻辑尽可能简单)。

所以这是控制我的日历组件的类:

import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
import { EventService } from '../event.service';
import dayGridPlugin from '@fullcalendar/daygrid';
import timeGridPlugin from '@fullcalendar/timegrid';
import listPlugin from '@fullcalendar/list';
import interactionPlugin, { Draggable } from '@fullcalendar/interaction';
import { FullCalendar } from 'primeng';

@Component({
selector: 'app-fullcalendar',
templateUrl: './fullcalendar.component.html',
styleUrls: ['./fullcalendar.component.css']
})
export class FullcalendarComponent implements OnInit {

events: any[];
options: any;
header: any;

//people: any[];

@ViewChild('fullcalendar') fullcalendar: FullCalendar;

calendar:any;


constructor(private eventService: EventService) {}

ngOnInit() {
//this.eventService.getEvents().then(events => { this.events = events;});

this.eventService.getEvents().subscribe(events => { this.events = events.map((event) => {
var date = new Date(event.start);
var hour = date.getHours();
//event['backgroundColor'] = hour === 7? 'red': (hour === 7 ? 'green' : 'black');

//let events = this.calendar.getEvents();
//events[

console.log("EVENTS: " + this.events);

if(hour === 7) {
event['backgroundColor'] = 'red';
}
else if(hour === 15) {
event['backgroundColor'] = 'green';
}
else if(hour === 23) {
event['backgroundColor'] = 'black';
}


return event;
})});

this.options = {
plugins:[ dayGridPlugin, timeGridPlugin, interactionPlugin, listPlugin ],
defaultDate: '2017-02-01',
header: {
left: 'prev,next',
center: 'title',
right: 'dayGridMonth,timeGridWeek,timeGridDay'
},
editable: true,
nextDayThreshold: '09:00:00',
allDayDefault: false,

dateClick: (dateClickEvent) => { // <-- add the callback here as one of the properties of `options`
console.log("DATE CLICKED !!!");
},

eventClick: (eventClickEvent) => {
console.log("EVENT CLICKED !!!");
console.log(eventClickEvent.event.id);
//this.eventService.removeEventById(eventClickEvent.event.id);
this.eventService.removeEventById(eventClickEvent.event.id).subscribe(events => console.log(events));


/*
events => { this.events = events.map((event) => {
var date = new Date(event.start);
var hour = date.getHours();

console.log("EVENTS: " + this.events);

if(hour === 7) {
event['backgroundColor'] = 'red';
}
else if(hour === 15) {
event['backgroundColor'] = 'green';
}
else if(hour === 23) {
event['backgroundColor'] = 'black';
}

return event;
})});
*/
},

eventDragStop: (eventDragStopEvent) => {
console.log("EVENT DRAG STOP !!!");
},

eventReceive: (eventReceiveEvent) => {
console.log(eventReceiveEvent);
//eventReceiveEvent.event.setAllDay(false, {maintainDuration: true});



//eventReceiveEvent['backgroundColor'] = 'red';
this.eventService.addEvent(eventReceiveEvent);
}
};

}

ngAfterViewInit() {

this.calendar = this.fullcalendar.getCalendar();
console.log("CALENDAR: " + this.calendar);

}

}

如您所见 ngOnInit() 方法我订阅 获取事件()将呈现事件列表返回为 Observable 的服务的方法(我这样做是因为在返回的输出中我为不同的事件类型处理不同的颜色,但这与我的问题并不严格相关......它工作正常)。

所以,你可以看到这个组件类包含这个钩子(Hook)方法 事件点击:(事件点击事件)处理点击我的日历事件。我正在考虑调用在我的服务中定义的删除事件方法并再次呈现它,但它不起作用(我已经做了几次尝试,但它不起作用)。我也不确定这是解决问题的正确方法。

从我能理解到 ngOnInit() 方法我订阅了我的日历事件数组,因此链接此数组的内容,此更改已发布并且应该再次呈现,我也尝试过,但它不起作用......所以我认为我可能遗漏了一些东西 Angular 逻辑。

这是我的服务类的代码:
import { Injectable } from '@angular/core';
import { HttpClientModule, HttpClient } from '@angular/common/http'
import { BehaviorSubject, Observable } from 'rxjs';

@Injectable()
export class EventService {

private events = [];

/*
private events = [
{id: 1, title: 'All Day Event', start: '2017-02-01'},
{id: 2, title: 'Long Event', start: '2017-02-07', end: '2017-02-10'},
{id: 3, title: 'Repeating Event', start: '2017-02-09T16:00:00'},
{id: 3, title: 'test', start: '2017-02-20T07:00:00'},
];
*/

private people = [
{id: 1, name: "PERSONA 1"},
{id: 2, name: "PERSONA 2"},
{id: 3, name: "PERSONA 3"},
{id: 4, name: "PERSONA 4"},
{id: 5, name: "PERSONA 5"},
]

public eventData = new BehaviorSubject(this.events);

constructor(private http: HttpClient) {}

getEvents(): Observable<any[]> {
return this.eventData.asObservable();
}


addEvent(event) {
//console.log(event.event.start);
//console.log(event);
const newEvent = {id: 5, title: event.event.title, start: event.event.start, end: event.event.end};
this.events.push(newEvent);
this.eventData.next([...this.events]);
//console.log(this.events);
}

removeEventById(id:number): Observable<any[]> {
console.log("EVENTDATA: " + this.eventData.getValue());

return this.eventData.asObservable();
}


/*
removeEventById = (id:number) => {
this.events = [];
//return this.events.filter(each=>each.id!=id);

}
*/

getPeople(): Promise<any[]> {
return Promise.all(this.people)
.then(res => {
console.log(res);
return res;
})
}

}

那么有什么问题呢?我错过了什么?什么是正确处理我的问题的聪明解决方案?

最佳答案

从 fullcalendar.io 开发的primeng fullcalendar,您可以过滤数据然后分配给eventData。我在 Demo 中创建了您的代码

 this.eventService.removeEventById(eventClickEvent.event.id).subscribe(events => console.log(events));

在你的方法中
removeEventById(id:number): Observable<any[]>  {
this.events=this.events.filter(each=>each.id!=id);
this.eventData.next([...this.events]);
return this.eventData.asObservable();
}

关于angular - 如何从此 PrimeNG FullCalendar 动态删除事件(单击事件)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62199949/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com