2017-03-13 14 views
0

誰かのために「シフト」カレンダーを作成しようとしていますが、パターンの開始日を知っていて、その日のオン/オフパターンを知っています。しかし、それをコードに翻訳するのに苦労しています。カレンダーのパターンに基づいてイベントを作成するにはどうすればよいですか?

作業4日、作業中止3日、作業4、作業中止3日後、作業中4日後、2日後、繰り返します。私はこれに基づいてカレンダーのイベントを作成するためにいくつかのロジックを作成する必要があります。

これは私が持っているものです。

$(document).ready(function() { 

    var on = [4, 4, 4]; 
    var off = [3, 3, 2]; 
    var startPattern = "2017-03-04"; 

    var days = $('#calendar').fullCalendar('getDate').daysInMonth(); 
    var events = []; 

    for (var i = $('#calendar').fullCalendar('getDate').day(); i < days; i++) { 
    var event = { 
     title: "work", 
     start: '' 
    } 
    events.push(event); 
    } 


    $('#calendar').fullCalendar({ 
    // put your options and callbacks here 
    events: events 

    }); 
}); 
+0

あなたが仕事4オフ、3オフ作業4、3オフ、あなたが仕事4によって何を意味するか、例を挙げて説明することができますどのタイプのシフトパターンであるかわからない – MGA

+1

3月4日に始まり、4日間働いてから3日間休み、4日間働いてから3日間休み、その後4日間働いてから2日オフ。その後、次のように開始してから再起動してください。 4-3-4-3-4-2 – Nathan

答えて

0

Plunker:https://plnkr.co/edit/xIfaWB?p=preview

$(document).ready(function() { 

    // define the schedule; 
    // duration is days; 
    // title is what is shown on the calendar 
    // color is how to color event 
    var schedule = [{ 
    duration: 4, 
    title: 'work', 
    color: 'red' 
    }, { 
    duration: 3, 
    title: 'off', 
    color: 'blue' 
    }, { 
    duration: 4, 
    title: 'work', 
    color: 'red' 
    }, { 
    duration: 3, 
    title: 'off', 
    color: 'blue' 
    }, { 
    duration: 4, 
    title: 'work', 
    color: 'red' 
    }, { 
    duration: 2, 
    title: 'off', 
    color: 'blue' 
    }, ]; 

    // define the range of events to generate 
    var startDay = moment("2017-03-04"); 
    var endDay = moment("2017-05-04"); 


// generate the events 
    var events = []; 
    // we loop from the start until we have passed the end day 
    // the way the code is defined, it will always complete a schedule segment 
    for (var s = 0, day = startDay; day.isBefore(endDay);) { 
    // loop for each day of a schedule segment 
    for (var i = 0; i < schedule[s].duration; i++) { 
     // add the event with the current properties 
     events.push({ 
     title: schedule[s].title, 
     color: schedule[s].color, 
     // we have to clone because the add() call below mutates the date 
     start: day.clone(), 
     allday: true 
     }); 
     // go to the next day 
     day = day.add(1, 'day'); 
    } 

    // go to the next schedule segment 
    s = (s + 1) % schedule.length; 
    } 



    // render the calendar 
    $('#calendar').fullCalendar({ 
    events: events 
    }); 

}); 
+0

これはうまくいくようです。詳細なソリューションをありがとう!私はそれの周りに私の頭を包むのに苦労していた。 – Nathan

関連する問題