简体   繁体   English

样式化CLNDR.js日历多日事件的第一天和最后几天

[英]Styling first and last days of CLNDR.js calendar multi-day events

I am using clndr.js ( http://kylestetz.github.io/CLNDR/ ) to display dates that a holiday cottage is booked for. 我正在使用clndr.js( http://kylestetz.github.io/CLNDR/ )来显示假日小屋预订的日期。 These are always shown using the multi-day event system as the minimum booking is 3 days. 这些始终使用多日活动系统显示,因为最低预订为3天。 I now need to style the first and last days of the event differently, to show they are changeover days. 我现在需要以不同的方式设计活动的第一天和最后一天,以显示它们是转换日。 Ideally I would do this by adding a class to the td . 理想情况下,我会通过向td添加一个类来完成此操作。 This is what I have so far: 这是我到目前为止:

JS JS

$('#calendar').clndr({
          template: $('#calendar-template').html(),
          weekOffset: 1,
           daysOfTheWeek: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
           targets: {
               nextButton: 'clndr-next',
               previousButton: 'clndr-previous'
           },
           multiDayEvents: {
               startDate: 'start',
               endDate: 'end'
           },
           events: events,
           clickEvents: {
               click: function(target) {
                   //alert(target);
               }
           }
      });

Example JSON 示例JSON

var events = [
  {start:'2016-05-29T00:00:00+00:00',
  end:'2016-06-01T00:00:00+00:00',
  title:'Mrs A N Human',},
  {start:'2016-08-10T00:00:00+00:00',
  end:'2016-08-17T00:00:00+00:00',
  title:'Mr A Person',}
];

HTML / Underscore HTML /下划线

 <div id="calendar">
  <script type="text/template" id="calendar-template">
   <table class="table">
    <thead>
     <tr>
      <th class='clndr-previous'>&lt;</th>
      <th colspan="5"><%= month %> <%= year %></th>
      <th class='clndr-next'>&gt;</th>
     </tr>
     <tr>
      <% _.each(daysOfTheWeek, function(day) { %>
      <th class="header-day"><%= day %></th>
      <% }); %>
     </tr>
    </thead>
    <tbody>
      <tr class="days"><% _.each(days, function(day, index) { %>
      <td class="<%= day.classes %>" id="<%= day.id %>">
       <span class="day-number">
       <%= day.day %>
       </span>
     </td>
      <% if ((index + 1) % 7 == 0) { 
           %> </tr><tr> <% 
           } %><% }); %>
     </tr>
    </tbody>
   </table>
  </script>
</div>

I am struggling to work out how to target those first and last days to apply some different styling. 我正在努力找出如何针对那些第一天和最后几天来应用一些不同的造型。 I am using moment.js as well if that can be used. 我正在使用moment.js,如果可以使用的话。 Help greatly appreciated! 非常感谢!

So eventually I spent some time looking at using Underscore and Moment to achieve what I was after. 所以最终我花了一些时间看着使用Underscore和Moment来实现我追求的目标。 Below is my updated code, using a conditional in Underscore to check the date using Moment, and add a class to the <td> if necessary. 下面是我更新的代码,使用Underscore中的条件来使用Moment检查日期,并在必要时向<td>添加一个类。 This was combined with an adjustment to how my events were represented in the JSON array: 这与我在JSON数组中表示事件的方式相结合:

HTML / Underscore HTML /下划线

<div id="calendar">
  <script type="text/template" id="calendar-template">
    <table class="table">
      <thead>
        <tr>
          <th class='clndr-previous'>&lt;</th>
          <th colspan="5"><%= month %> <%= year %></th>
          <th class='clndr-next'>&gt;</th>
        </tr>
        <tr>
          <% _.each(daysOfTheWeek, function(day) { %>
          <th class="header-day"><%= day %></th>
          <% }); %>
        </tr>
      </thead>
      <tbody>
        <tr class="days">
          <% _.each(days, function(day, index) { %>
            <td class="<%= day.classes %> <% _.each(day.events, function(event) { %><% if( moment(event.start).isSame( day.date ) ){ %>start<% } %><% }); %> <% _.each(day.events, function(event) { %><% if( moment(event.end).isSame( day.date ) ){ %>end<% } %><% }); %>">
              <span class="day-number"><%= day.day %></span>
            </td>
          <% if ((index + 1) % 7 == 0) { %> </tr><tr>  <% } %>
          <% }); %>
         </tr>
       </tbody>
    </table>
  </script>
</div>

JSON JSON

var events = [
  {start:'2016-05-29',
  end:'2016-06-01',
  title:'Mrs A N Human',},
  {start:'2016-08-10',
  end:'2016-08-17',
  title:'Mr A Person',}
];

Okay so I hope you can find some of this useful as it took me a long time to get this to work. 好的,所以我希望你能找到一些有用的东西,因为我花了很长时间才能让它发挥作用。 You will probably need to modify some bits for your template etc. but should give you a good idea. 您可能需要为模板等修改一些位,但应该给您一个好主意。 Pay attention to the <% _.each part as that's what makes the class work from the multi event JSON 注意<% _.each部分,因为这使得该类可以从多事件JSON中工作

Here's my CLNDR template code: 这是我的CLNDR模板代码:

<div id="mini-clndr">
  <script id="mini-clndr-template" type="text/template">
    <div class="controls">
      <div class="clndr-previous-button"><img src="/assets/local/img/calendar-left-arrow.png"></div>
      <div class="month">
        <%= month %>
          <%= year %>
      </div>
      <div class="clndr-next-button"><img src="/assets/local/img/calendar-right-arrow.png"></div>
    </div>

    <div class="days-container">
      <div class="days">
        <div class="headers">
          <% _.each(daysOfTheWeek, function(day) { %>
            <div class="day-header">
              <%= day %>
            </div>
            <% }); %>
        </div>
        <% _.each(days, function(day) {
                    var classes = '';
                    if( day.events.length ){
                        for( var i = 0; i < day.events.length; i++ ){
                            classes += ' ' + day.events[i].class;
                        }
                    }
                %>
          <div class="<%= day.classes %><%= classes %>" id="<%= day.id %>">
            <%= day.day %>
          </div>
          <% }); %>
      </div>
      <div class="events">
        <div class="headers">
          <div class="x-button">x</div>
          <div class="event-header">EVENTS</div>
        </div>
        <div class="events-list">
          <% _.each(eventsThisMonth, function(event) { %>
            <div class="event">
              <a target="blank" href="<%= event.url %>">
                <%= moment(event.date).format('MMMM Do') %>:
                  <%= event.title %>
              </a>
            </div>
            <% }); %>
        </div>
      </div>
    </div>
  </script>
</div>

Here's my CLNDR.js code: 这是我的CLNDR.js代码:

  var clndr = {};

  $(function() {

    var currentMonth = moment().format('YYYY-MM');
    var nextMonth = moment().add(1, 'month').format('YYYY-MM');

    var events = [
      // Multiday Event Code
      {
        start: '2016-05-17',
        end: '2016-05-19',
        title: 'Monday to Friday Event',
        class: 'long-event'
      },
    ];

    $('#mini-clndr').clndr({
      template: $('#mini-clndr-template').html(),
      weekOffset: 1,
      events: events,
      multiDayEvents: {
        endDate: 'end',
        singleDay: 'date',
        startDate: 'start'
      },
      clickEvents: {
        click: function(target) {
          if (target.events.length) {
            var daysContainer = $('#mini-clndr').find('.days-container');
            daysContainer.toggleClass('show-events', true);
            $('#mini-clndr').find('.x-button').click(function() {
              daysContainer.toggleClass('show-events', false);
            });
          }
        }
      },
      adjacentDaysChangeMonth: true,
      forceSixRows: false,
      showAdjacentMonths: false,
    });
  });

Then my CSS code: 然后是我的CSS代码:

  .long-event {
    background: orange;
  }

  :not(.long-event) + .long-event, .long-event:first-child {
    background: orange;
    border-radius: 50% 0 0 50%;
  }

  .last {
    border-radius: 0 50% 50% 0;
  }

And then finally, some jQuery to make those classes work. 最后,一些jQuery使这些类工作。 You'll also need to add a .date class to every date on the calendar: 您还需要在日历上的每个日期添加.date类:

$('.long-event + .date:not(.long-event)').prev().addClass('last');

Although this jsFiddle isn't using CLNDR.js, you can see the styling working :) 虽然这个jsFiddle没有使用CLNDR.js,你可以看到造型工作:)

https://jsfiddle.net/andyjh07/ke0euh5m/ https://jsfiddle.net/andyjh07/ke0euh5m/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM