admin管理员组文章数量:1332388
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 acplish 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 acplish the correct result.
Appreciate any feedback/help.
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 acplish 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 acplish the correct result.
Appreciate any feedback/help.
Share Improve this question asked Nov 20, 2016 at 3:18 rubberchickenrubberchicken 1,3222 gold badges22 silver badges49 bronze badges2 Answers
Reset to default 6I 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.
Here's the same solution for fullcalendar v5:
datesSet: function(dateInfo) {
$.each($(".fc-col-header-cell.fc-day"), function(key, val) {
var dateYMD = $(this).attr("data-date");
$(this).append("<div class='fc-dailytotal' id='dailytotal-" + dateYMD + "'></div>");
});
$(".fc-dailytotal").html(0);
},
eventDidMount: function(info) {
var currentday = moment(info.event.start).format("YYYY-MM-DD");
if (info.event._def.extendedProps.totalhrs > 0) {
var prev = parseInt($("#dailytotal-" + currentday).html()) || 0;
$("#dailytotal-" + currentday).html(prev + info.event._def.extendedProps.totalhrs);
console.log(info.event._def.extendedProps.totalhrs);
}
},
本文标签: javascriptfullcalendar how to add total duration of all events for each dayStack Overflow
版权声明:本文标题:javascript - fullcalendar: how to add total duration of all events for each day - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742289854a2447591.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论