Flight Booking
For a great booking experience a good date picker is essential. The best implementations are contextual and take the intent (what people want to do) and context (where and how are they doing it) into account. Learn how to use and customize the range picker and see how you can add your flavor that matches with the overall experience of your application.
Demo
When building flight booking systems you want to make sure that you set up min & max boundaries for the selectable days. This way you ensure that people can't select dates from the past.
The range picker can be set up on one or two inputs. If you are building a fully custom experience, you can embed it into your own modal.
html<label>
Range
<input id="picker" mbsc-input placeholder="Please select..." />
</label>
jsvar min = new Date(),
max = new Date(min.getFullYear(), min.getMonth() + 6, min.getDate());
mobiscroll.datepicker('#picker', {
controls: ['calendar'],
select: 'range',
min: min,
max: max
});
Instead of a single input you can have two, one for Outbound and another for Return.
html<label>
Outbound
<input id="start" mbsc-input placeholder="Please select..." />
</label>
<label>
Return
<input id="end" mbsc-input placeholder="Please select..." />
</label>
jsvar min = new Date(),
max = new Date(min.getFullYear(), min.getMonth() + 6, min.getDate());
mobiscroll.datepicker('#start', {
controls: ['calendar'],
select: 'range',
startInput: '#start',
endInput: '#end',
min: now,
max: max
});
Besides setting min & max boundaries, days without flights should be disabled. These could be single occurrence or recurring dates, like specific weekdays. Setting inRangeInvalid: true allows ranges to contain invalids, it only makes sure that the start and end dates are on valid days. If inRangeInvalid is false, no invalid dates are allowed inside a selection (useful for hotel booking).
html<label>
Outbound
<input id="start" mbsc-input placeholder="Please select..." />
</label>
<label>
Return
<input id="end" mbsc-input placeholder="Please select..." />
</label>
jsmobiscroll.datepicker('#start', {
controls: ['calendar'],
select: 'range',
startInput: '#start',
endInput: '#end',
invalid: [
{
recurring: {
repeat: 'weekly',
weekDays: 'TU,TH'
},
},
new Date(now.getFullYear(), now.getMonth(), 25)
],
inRangeInvalid: true
});
You will probably need to give people the option to select a single date for one way trips. This can be easily accomplished by dynamically setting the select option to 'date'. Toggling with a radio button between 'date'/'range' will switch between single date selection and range selection.
document.querySelectorAll('.flight-type').forEach(function (elm) {
var oneWay = this.value == 'oneway';
elm.addEventListener('change', function (ev) {
mobiscroll.getInst(end).setOptions({ disabled: this.value == oneWay });
if (oneWay) {
booking.setOptions({
select: 'date'
});
} else {
booking.setOptions({
select: 'range'
});
}
});
});;
If you don't want to add another control (like a radio button or segmented) to your UI, you can solve the "one way question" with a custom button on the dialog and a little custom logic. The idea is to track if at least one value is selected and dynamically enable the button.
var calendar = mobiscroll.datepicker('#picker', {
controls: ['calendar'],
select: 'range',
buttons: ['set', 'cancel', {
text: 'One way only',
disabled: true,
handler: function () {
var start = calendar.getTempVal()[0];
calendar.setVal([start, null]);
calendar.close();
}
}],
onTempChange: function (event, inst) {
inst.setOptions({
buttons: [
'cancel',
{
text: 'One way only',
disabled: !inst.getTempVal()[0],
handler: function () {
var start = calendar.getTempVal()[0];
calendar.setVal([start, null]);
calendar.close();
}
},
'set'
]
});
}
});
mobiscroll.setOptions({
locale: ,
theme: ' ',
themeVariant: ' '
});
var now = new Date();
var min = now;
var max = new Date(now.getFullYear(), now.getMonth() + 6, now.getDate());
mobiscroll.datepicker('#demo-flight-booking-single', {
controls: ['calendar'],
select: 'range',
display: 'anchored',
min: min,
max: max,
pages: 2,
buttons: ['set', 'cancel'],
});
mobiscroll.datepicker('#demo-flight-booking-outbound', {
controls: ['calendar'],
select: 'range',
display: 'anchored',
startInput: '#demo-flight-booking-outbound',
endInput: '#demo-flight-booking-return',
min: min,
max: max,
pages: 2,
});
mobiscroll.datepicker('#demo-flight-booking-inv-outbound', {
controls: ['calendar'],
select: 'range',
display: 'anchored',
inRangeInvalid: true,
startInput: '#demo-flight-booking-inv-outbound',
endInput: '#demo-flight-booking-inv-return',
min: min,
max: max,
pages: 2,
invalid: [
{
recurring: {
repeat: 'weekly',
weekDays: 'TU,TH',
},
},
new Date(now.getFullYear(), now.getMonth(), 25),
],
});
var booking = mobiscroll.datepicker('#demo-flight-booking-type-outbound', {
controls: ['calendar'],
select: 'range',
display: 'anchored',
startInput: '#demo-flight-booking-type-outbound',
endInput: '#demo-flight-booking-type-return',
min: min,
max: max,
pages: 2,
});
document.querySelectorAll('.demo-flight-type').forEach(function (item) {
item.addEventListener('change', function (event) {
var oneWay = event.target.value == 'oneway';
mobiscroll.getInst(document.getElementById('demo-flight-booking-type-return')).setOptions({ disabled: oneWay });
if (oneWay) {
booking.setOptions({
select: 'date',
});
} else {
booking.setOptions({
select: 'range',
});
}
});
});
var singleBooking = mobiscroll.datepicker('#demo-flight-booking-custom', {
controls: ['calendar'],
select: 'range',
display: 'anchored',
min: min,
max: max,
pages: 2,
buttons: [
'cancel',
{
text: 'One way only',
disabled: true,
handler: function () {
var start = singleBooking.getTempVal()[0];
singleBooking.setVal([start, null]);
singleBooking.close();
},
},
'set',
],
onTempChange: function (event, inst) {
inst.setOptions({
buttons: [
'cancel',
{
text: 'One way only',
disabled: !inst.getTempVal()[0],
handler: function () {
var start = singleBooking.getTempVal()[0];
singleBooking.setVal([start, null]);
singleBooking.close();
},
},
'set',
],
});
},
});
<div class="mbsc-grid mbsc-no-padding">
<div class="mbsc-row">
<div class="mbsc-col-12">
<label>
Pick your flight
<input id="demo-flight-booking-single" mbsc-input data-input-style="outline" data-label-style="stacked" placeholder="Please select..." />
</label>
</div>
</div>
<div class="mbsc-row">
<div class="mbsc-col-6">
<label>
Outbound
<input id="demo-flight-booking-outbound" mbsc-input data-input-style="outline" data-label-style="stacked" placeholder="Please select..." />
</label>
</div>
<div class="mbsc-col-6">
<label>
Return
<input id="demo-flight-booking-return" mbsc-input data-input-style="outline" data-label-style="stacked" placeholder="Please select..." />
</label>
</div>
</div>
<div class="mbsc-row">
<div class="mbsc-col-6">
<label>
Outbound
<input id="demo-flight-booking-inv-outbound" mbsc-input data-input-style="outline" data-label-style="stacked" placeholder="Please select..." />
</label>
</div>
<div class="mbsc-col-6">
<label>
Return
<input id="demo-flight-booking-inv-return" mbsc-input data-input-style="outline" data-label-style="stacked" placeholder="Please select..." />
</label>
</div>
</div>
<div class="mbsc-row">
<label>
Round trip
<input class="demo-flight-type" value="round" mbsc-radio data-theme="material" data-theme-variant="light" type="radio" name="flight-type" checked>
</label>
<label>
One way
<input class="demo-flight-type" value="oneway" mbsc-radio data-theme="material" data-theme-variant="light" type="radio" name="flight-type">
</label>
</div>
<div class="mbsc-row">
<div class="mbsc-col-6">
<label>
Outbound
<input id="demo-flight-booking-type-outbound" mbsc-input data-input-style="outline" data-label-style="stacked" placeholder="Please select..." />
</label>
</div>
<div class="mbsc-col-6">
<label>
Return
<input id="demo-flight-booking-type-return" mbsc-input data-input-style="outline" data-label-style="stacked" placeholder="Please select..." />
</label>
</div>
</div>
<div class="mbsc-row">
<div class="mbsc-col-12">
<label>
Pick your flight
<input id="demo-flight-booking-custom" mbsc-input data-input-style="outline" data-label-style="stacked" placeholder="Please select..." />
</label>
</div>
</div>
</div>