Custom Range Picker View

The timeline comes with built-in support for day, week, month and year views. For those situations where a custom range makes more sense, it is possible to override the calendar navigation component with a range picker.

The calendar header can be easily customized and a two-way synchronization needs to be kept between the timeline and range picker.

Download and try example

Demo

Loading the live demo

Preview in full screen

Replace the Timeline's default header navigation with a range picker — let users set any custom date range, synced two-way with the timeline.

Code

mobiscroll.setOptions({
  locale:  ,
  theme: ' ',
  themeVariant: ' '
});

var formatDate = mobiscroll.formatDate;
var startDate;
var endDate;
var end;

var myCalendar = mobiscroll.eventcalendar('#demo-custom-range-view', {
  clickToCreate:  ,
  dragToCreate:  ,
  dragToMove:  ,
  dragToResize:  ,
  eventDelete:  ,
  resources: [
    { id: 1, name: 'Resource A', color: '#e20000' },
    { id: 2, name: 'Resource B', color: '#76e083' },
    { id: 3, name: 'Resource C', color: '#4981d6' },
    { id: 4, name: 'Resource D', color: '#e25dd2' },
    { id: 5, name: 'Resource E', color: '#1dab2f' },
    { id: 6, name: 'Resource F', color: '#d6d145' },
  ],
  view: {
    timeline: {
      type: 'day',
      size: 14,
      eventDisplay: 'fill',
    },
  },
  renderHeader: function () {
    return (
      '<div id="custom-date-range"><button mbsc-button data-variant="flat" class="mbsc-calendar-button">' +
      '<span id="custom-date-range-text" class="mbsc-calendar-title">Button' +
      '</span></button></div>' +
      '<div class="md-custom-range-view-controls">' +
      '<div mbsc-calendar-prev></div>' +
      '<div mbsc-calendar-today></div>' +
      '<div mbsc-calendar-next></div>' +
      '</div>'
    );
  },
  onPageLoaded: function (args) {
    startDate = args.firstDay;
    end = args.lastDay;
    endDate = new Date(end.getFullYear(), end.getMonth(), end.getDate() - 1, 0);
    // Set button text
    rangeButton.innerText = getFormattedRange(startDate, endDate);
    // Set range value
    myRange.setVal([startDate, endDate]);
  },
});

var myRange = mobiscroll.datepicker('#custom-date-range', {
  select: 'range',
  display: 'anchored',
  showOverlay: false,
  touchUi: true,
  buttons: [],
  onClose: function (args, inst) {
    var date = inst.getVal();
    if (date[0] && date[1]) {
      if (date[0].getTime() !== startDate.getTime()) {
        // Navigate the calendar
        setTimeout(function () {
          myCalendar.navigate(date[0]);
        });
      }
      startDate = date[0];
      endDate = date[1];
      // Set calendar view
      myCalendar.setOptions({
        refDate: startDate,
        view: {
          timeline: {
            type: 'day',
            size: getNrDays(startDate, endDate),
            eventDisplay: 'fill',
          },
        },
      });
    } else {
      myRange.setVal([startDate, endDate]);
    }
  },
});

var rangeButton = document.getElementById('custom-date-range-text');

// Returns the formatted date
function getFormattedRange(start, end) {
  return (
    formatDate('MMM D, YYYY', new Date(start)) +
    (end && getNrDays(start, end) > 1 ? ' - ' + formatDate('MMM D, YYYY', new Date(end)) : '')
  );
}

// Returns the number of days between two dates
function getNrDays(start, end) {
  return Math.round(Math.abs((end.setHours(0) - start.setHours(0)) / (24 * 60 * 60 * 1000))) + 1;
}

mobiscroll.getJson(
  'https://trial.mobiscroll.com/timeline-events/?vers=5',
  function (events) {
    myCalendar.setEvents(events);
  },
  'jsonp',
);
  

Duplicate Session

You have signed in on a different machine. To keep everything in sync, you have been signed out of this session.

If you need to invite team members, manage seats and accounts you can do it from the licenses page.

Duplicate Session

We noticed you are logged in on a different machine and have closed your old session.

If you need to invite team members, manage seats and accounts you can do it from the licenses page.