Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open second UI Datepicker on closing the first one

I´m trying to add some functionality to a form with JQuery UI´s Datepicker.

When a user selects the arrival date (onSelect), a few things should happen:

  1. Update the departure to 3 days after the arrival date (This works now :))
  2. Split the departure date in 3 and add each part to an input field (see arrival date example in Fiddle)
  3. When the arrival-datepicker closes (onClose), the departure-datepicker should open automatically, defaulting to the date selected in point 1 (example: http://www.kayak.es )

I managed to get point 1 to work, but I´m a bit lost with the other features. If anyone can show me how to do this or get me on the right track that would be really great! I´m still a beginner but learning fast.

Here´s a Fiddle of what I have now: http://jsfiddle.net/mattvic/B6Kax/13/

like image 327
Mattvic Avatar asked Jun 26 '11 17:06

Mattvic


2 Answers

Split the departure date in 3 and add each part to an input field

Looks like you already have the code written for this in the onSelect handler of the destination datepicker. Unfortunately, it looks like setDate does not trigger that event, and you cannot trigger it manually. What I would recommend is breaking that code out into a separate function that you can call from both places:

function splitDepartureDate(dateText) {
    var depSplit = dateText.split("-", 3);
    $('#alt-vertrek-d').val(depSplit[0]);
    $('#alt-vertrek-m').val(depSplit[1]);
    $('#alt-vertrek-y').val(depSplit[2]);
}

Then change your departure datepicker onSelect handler to point to that function:

$('#vertrek').datepicker({

    // Prevent selecting departure date before arrival:
    beforeShow: customRange,
    onSelect: splitDepartureDate
});

Lastly, call that function from your arrival's onSelect event handler:

/* snip: */

// Populate departure date field 
var nextDayDate = $('#aankomst').datepicker('getDate', '+3d');
nextDayDate.setDate(nextDayDate.getDate() + 3);
$('#vertrek').datepicker('setDate', nextDayDate);

// Manually call splitDepartureDate
splitDepartureDate($("#vertrek").val());

When the arrival-datepicker closes (onClose), the departure-datepicker should open automatically, defaulting to the date selected in point 1

This is just a matter of using the onClose event handler:

onClose: function() {
    $("#vertrek").datepicker("show");
}

Here's your updated example: http://jsfiddle.net/zXMke/

like image 54
Andrew Whitaker Avatar answered Nov 14 '22 20:11

Andrew Whitaker


Answering the title's question :

 $("#firsDate").datepicker('option',"onClose", function() { $( "#secondDate" ).datepicker( "show" ); });

Source :

http://api.jqueryui.com/datepicker/#method-show

http://api.jqueryui.com/datepicker/#option-onClose

I recommend to use onSelect instead of onClose event in order to make sure that the 1st date has been selected before opening the second datepicker.

like image 45
Thomas Allier Avatar answered Nov 14 '22 21:11

Thomas Allier