遍历对象

时间:2018-11-01 12:03:13

标签: javascript jquery html calendar

我知道这个问题已经被问过很多次了,但是我找不到解决方案,而且我对jquery还是陌生的 我正在建立一个日历,以显示员工多天的班次。我已经硬编码了title,start和end的值,并且此代码可以正常工作。  我将从后端获取title,start和end的值。我将获得多个值。因此,我必须遍历这些值,并在事件对象内传递所有值。

我将获得

之类的值
[morningShift,10/09/2018,20/09/2018],[eveningShift,21/09/2018,23/09/2018]

我应该动态传递标题开始日期和结束日期内的值,并且其格式应为

{
            title: 'morningShift',
            start: new Date('10/09/2018'),
            end: new Date('20/09/2018'),
            className: 'bg-primary'
        },
         {
            title: 'eveningShift',
            start: new Date('21/09/2018'),
            end: new Date('23/09/2018'),
            className: 'bg-primary'
        }

 var calendar = $('#calendar').fullCalendar({
        slotDuration: '00:15:00', /* If we want to split day time each 15minutes */
        minTime: '00:00:00', /* calendar start Timing */
        maxTime: '24:00:00',  /* calendar end Timing */
        defaultView: 'month',  
        handleWindowResize: true,   
        height: $(window).height() - 200,   
        header: {
            left: 'prev,next today',
            center: 'title',
            right: 'month,agendaWeek,agendaDay'
            // right: ''

        },
        events: [
           {
            title: 'Morning Shift',
            start: new Date('10/25/2018'),
            end: new Date('11/05/2018'),
            className: 'bg-primary'
        },

    ],
        editable: true,
        droppable: true, // this allows things to be dropped onto the calendar !!!
        eventLimit: true, // allow "more" link when too many events
        selectable: true,

        select: function (start, end, allDay) {
            $modal.modal({ 
                backdrop: 'static'
            });


            calendar.fullCalendar('unselect');
        }
    });

1 个答案:

答案 0 :(得分:1)

新日期(mm / dd / yyyy)接受此格式的值。

let apiData = [["morningShift","5/09/2018","10/09/2018"],['eveningShift','1/09/2018','10/09/2018']]
events = apiData.map(([title, start, end])=>({
            title,
            start: new Date(start),
            end: new Date(end),
            className: 'bg-primary'})
          )



console.log(events)