Jquery FullCalendar,在输出

时间:2015-11-28 14:33:09

标签: javascript jquery fullcalendar

我有一个Jquery FullCalendar。我能够从json获取值的行和列的总值。我想将周数添加到行输出,将Day名添加到列输出

  

例如: - 这是我的行输出值950 500 550 200,这些是我的列输出值700 300 300 200 250 200 250

Fiddle到目前为止的工作!!

我希望按如下方式生成输出: -

  

对于行值(W 44)950,(W 45)500,(W 46)550,(W 47)200总计:(2200),对于列值,此类(太阳)700(星期一)300(星期二)300(星期三)200(星期四)250(星期五)200(星期六)250

完整代码: -

var baseEvent;

var $calendar = $('#calendar').fullCalendar({
 weekNumbers: true,
header: {
    left: 'prev,next today',
    center: 'title',
    right: 'month,agendaWeek,agendaDay'
},
editable: true,
selectable: true,
selectHelper: true,
eventRender: eventRenderCallback,
viewRender: viewRenderCallback,
eventAfterAllRender: eventAfterAllRenderCallback,
defaultView: 'month',
events: [
    {"title":"500","start":"Oct 1 2015"},   
    {"title":"50","start":"Oct 2 2015"},
    {"title":"500","start":"Oct 3 2015"},   
    {"title":"50","start":"Oct 4 2015"},
    {"title":"500","start":"Oct 6 2015"},   
    {"title":"50","start":"Oct 7 2015"},
    {"title":"500","start":"Oct 8 2015"},   
    {"title":"50","start":"Oct 9 2015"},
    {"title":"500","start":"Oct 10 2015"},   
    {"title":"50","start":"Oct 11 2015"},
    {"title":"500","start":"Oct 12 2015"},   
    {"title":"50","start":"Oct 13 2015"},
    {"title":"500","start":"Oct 14 2015"},   
    {"title":"50","start":"Oct 15 2015"},
    {"title":"500","start":"Oct 16 2015"},   
    {"title":"50","start":"Oct 17 2015"},
    {"title":"500","start":"Nov 1 2015"},   
    {"title":"50","start":"Nov 2 2015"},
    {"title":"100","start":"Nov 3 2015"},   
    {"title":"50","start":"Nov 4 2015"},
    {"title":"100","start":"Nov 5 2015"},   
    {"title":"50","start":"Nov 6 2015"},
    {"title":"100","start":"Nov 7 2015"},   
    {"title":"50","start":"Nov 8 2015"},
    {"title":"100","start":"Nov 9 2015"},   
    {"title":"50","start":"Nov 10 2015"},
    {"title":"100","start":"Nov 11 2015"},   
    {"title":"50","start":"Nov 12 2015"},
    {"title":"100","start":"Nov 13 2015"},   
    {"title":"50","start":"Nov 14 2015"},
    {"title":"100","start":"Nov 15 2015"},   
    {"title":"50","start":"Nov 16 2015"},
    {"title":"100","start":"Nov 17 2015"},   
    {"title":"50","start":"Nov 18 2015"},
    {"title":"100","start":"Nov 19 2015"},   
    {"title":"50","start":"Nov 20 2015"},
    {"title":"100","start":"Nov 21 2015"},   
    {"title":"50","start":"Nov 22 2015"},
    {"title":"100","start":"Nov 23 2015"},   
    {"title":"50","start":"Nov 24 2015"},

    {"title":"500","start":"Dec 1 2015"},   
    {"title":"50","start":"Dec 2 2015"}

]
});

var rowTotals = [];
var columnTotals = [];
var offset = 0;

function eventRenderCallback(event,element){
var viewDateMonth = $('#calendar').fullCalendar('getDate').getMonth();
if(event.start.getMonth() == viewDateMonth){
    addToDateArray(event.start.getDate(), parseInt(event.title));
}
}

function eventAfterAllRenderCallback(view){
var total = 0;
for (var i = 0; i < 6; i++){
    total += rowTotals[i];
    var label = $("#rows label:nth-child(" + (i+1) + ")");
    label.html(rowTotals[i]);     
}

for (var i = 0; i < 7; i++){
    var label = $("#cols label:nth-child(" + (i+1) + ")");
    label.html(columnTotals[i]);     
}

$("#total").html("("+total+")");
}

function viewRenderCallback(view,element){
offset = $("tr.fc-week.fc-first td.fc-other-month").length;
rowTotals = [0,0,0,0,0,0];
columnTotals = [0,0,0,0,0,0,0];

if(view.name == "agendaDay"){
    $("#rows label").show();
    $("#cols label").hide();
    $("#cols label:nth-child(" + (view.start.getDay()+1) + ")").show();
}else if(view.name == "agendaWeek"){
    $("#cols label").show();
    $("#rows label").hide();
    var row = Math.floor((view.start.getDate() + offset - 1) / 7);
    $("#rows label:nth-child(" + (row+1) + ")").show();
}else {
    $("#cols label").show();
    $("#rows label").show();
}
}                   

function addToDateArray(date,num){
var pos = date + offset;
var row = Math.floor((pos - 1) / 7);
var col = (pos - 1) % 7

rowTotals[row] += num;
columnTotals[col] += num;
}

请为任何解决方案编辑小提琴。感谢!!!

1 个答案:

答案 0 :(得分:0)

由于您在fullcalendar中使用weekNumbers选项,因此它会为您计算周数。得分了!他们可以使用$(“。fc-week-number div”)从dom中检索它们。您可以使用简单的索引日期名称数组来获取这一天。

Fiddle

function eventAfterAllRenderCallback(view){
    var total = 0;
    for (var i = 0; i < 6; i++){
        total += rowTotals[i];
        var label = $("#rows label:nth-child(" + (i+1) + ")");
        var week = $(".fc-week-number div").eq(i).html();
        label.html("(W"+(week)+") " + rowTotals[i]);     
    }

    var dayNames = ['Sun', 'Mon', 'Tue', 'Wed', 'Thur', 'Fri', 'Sat'];
    for (var i = 0; i < 7; i++){
        var label = $("#cols label:nth-child(" + (i+1) + ")");
        label.html("(" + dayNames[i] + ") " + columnTotals[i]);     
    }

    $("#total").html("("+total+")");
}