fullcalendar:如何为每天添加所有事件的总持续时间 [英] fullcalendar: how to add total duration of all events for each day
问题描述
我已经注意到这个问题被问了几次,但没有真正的正确答案或良好的反馈来指引正确的道路。
我正在使用 fullcalendar javascript plugin并尝试为每天添加多个活动的总小时数,然后我将在页眉或页脚中显示总和每一天。
我已经尝试了很多不同的方法来完成这个任务,但是最接近我的结果的是这个代码:
eventAfterRender:function(event,element,view){
$ b $ if(event.totalhrs> 0){
var sd = event.startdate;
if(dateTotal.hasOwnProperty(sd)){
dateTotal [event.startdate] =(dateTotal [event.startdate] + + event.totalhrs);
} else {
dateTotal [event.startdate] = +(event.totalhrs);
$(。fc-day-top [data-date ='+ event.startdate +')。find('。fc-dailytotal').text (dateTotal [event.startdate]);
}
}
这适用于日历呈现这是第一次,但如果发生事件变化,它将不断增加错误地显示非常高的值的总数。我明白为什么它不正确地添加总数(dateTotal [event.startdate] + + event.totalhrs)
,但我希望有人能帮助指导我以正确的方向完成正确的结果。
感谢任何反馈/帮助。
想出了一个替代方法来完成这项工作,而没有一天持有总和的日期数组。我希望这可以帮助任何一直在搜索的人。
请记住,此示例仅适用于月视图...有一些调整使其适用于周/日视图。
此外,该活动必须有一个总小时数对象,用于总和。您将在下面看到 event.totalhrs
viewRender:function(view,element){
$ .each($(.fc-day-top),function(key,val){
var dateYMD = $(this).attr(data-date);
$(this).append(< div class ='fc-dailytotal'id ='dailytotal - + dateYMD +'>< / div>);
});
},
eventRender:function(event,element,view){
$(。fc-dailytotal)。text(0); //清除总金额
},
eventAfterRender:function(event,element,view){
var current_day = moment(event.start).format(YYYY-MM -DD);
if(event.totalhrs> 0){
var prev = $(#dailytotal - + currentday).text()|| 0;
$(#dailytotal - + currentday).text(+ prev + + event.totalhrs);
code
$ b您可以使用此方法计算每周总计为好。
I have noticed this question asked a few times but with no actual correct answer or good feedback to direct in the right path.
I am using fullcalendar javascript plugin and trying to add the total hours of multiple events for each day which then I will display the sum in the header or footer of each day.
I have tried many different ways to accomplish this but the closest I got to my result is with this code:
eventAfterRender: function(event, element, view) {
if (event.totalhrs > 0) {
var sd = event.startdate;
if (dateTotal.hasOwnProperty(sd)) {
dateTotal[event.startdate] = (dateTotal[event.startdate] + +event.totalhrs);
} else {
dateTotal[event.startdate] = +(event.totalhrs);
}
$(".fc-day-top[data-date='"+event.startdate+"']").find('.fc-dailytotal').text(dateTotal[event.startdate]);
}
}
This works when the calendar is rendered for the first time, but if there is an event change, it will keep adding the totals incorrectly showing very high values. I understand why its adding the totals incorrectly (dateTotal[event.startdate] + +event.totalhrs)
but I am hoping someone can help direct me in the right direction to accomplish the correct result.
Appreciate any feedback/help.
解决方案 I figured out an alternative way to make this work without an Array of dates holding the sum for each day. I hope this helps anyone that has been searching as long as I have.
Keep in mind, this example is only for the month view... there's a few tweaks to make it work for week/day view.
Also, the event must have a total hours object which is used to sum the total. You will see this below as event.totalhrs
viewRender: function(view, element) {
$.each($(".fc-day-top"), function(key, val) {
var dateYMD = $(this).attr("data-date");
$(this).append("<div class='fc-dailytotal' id='dailytotal-"+dateYMD+"'></div>");
});
},
eventRender: function(event, element, view) {
$(".fc-dailytotal").text(0); //Clear total sum
},
eventAfterRender: function(event, element, view) {
var currentday = moment(event.start).format("YYYY-MM-DD");
if (event.totalhrs > 0) {
var prev = $("#dailytotal-"+currentday).text() || 0;
$("#dailytotal-"+currentday).text(+prev + +event.totalhrs);
}
}
You can use this method to calculate a weekly total as well.
这篇关于fullcalendar:如何为每天添加所有事件的总持续时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!