Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep jquery ui datepicker in place (stick to the input control) while scrolling?

After reading a lot of on the subject I'm still in the dark about how to approach resolving the problem. To clarify problem, I want calendar to scroll altogether with input control associated and not to stay fixed at whatever screen position. Here is image with no scroll: (that is what I want to preserve on scroll as well) enter image description here

and here is image while scrolling : (that is not what I want)

enter image description here

Any tip would be greatly appreciated. UPDATE: Here is a link that closely portray the problem, I would like calendar to stick to the control while scrolling.

like image 492
krul Avatar asked May 17 '12 15:05

krul


People also ask

How do I fix my datepicker position?

To change the position of the jQuery UI Datepicker just modify . ui-datepicker in the css file. The size of the Datepicker can also be changed in this way, just adjust the font size.

How do I keep my datepicker open?

You have to use . show() method to keep your dateTimePicker visible, note that all the DateTimePicker's functions are reachable via the data attribute e.g.

How do I limit datepicker?

You can restrict the users from selecting a date within the particular range by specifying MinDate and MaxDate properties. The default value of MinDate property is 1/1/1920 and MaxDate property is 12/31/2120 . Dates that appears outside the minimum and maximum date range will be disabled (blackout).

How do I set a datepicker range?

Steps to add Datepicker – $(document). ready(function(){ $("#setMin,#setMax"). datepicker({ dateFormat: "yy-mm-dd" }); $('#datepicker'). datepicker({ dateFormat: "yy-mm-dd", maxDate:'+1m +10d', minDate: -10 }); });


1 Answers

I am not sure if your's one is a scrollable div, but the problem occurring here is jQuery UI date picker doesn't move with HTML content when inside a scrollable div.

Here is a fiddle which demo http://jsfiddle.net/joycse06/m6A5q/embedded/result/ demonstrating the problem. Click on input and try scrolling that div.

It's happening because jQuery UI create the calendar UI as position absolute relative to document or window not the container scrollable div. So when the scrollable div is scrolled main window's scrolling context isn't changed and hence the datepicker calendar doesn't follow other html element in that div.

So the solution can be to hide the calendar when that div is scrolled and it makes sense, if the input is not visible you can hide the datepicker calendar. So the updated code will be

var datePicker = $('#datepicker').datepicker();

$(".demo").scroll(function() {
  datePicker.datepicker('hide');
  $('#datepicker').blur();
});

$(window).resize(function() {
  datePicker.datepicker('hide');
  $('#datepicker').blur();
});

It will hide the date picker when container div or window is scrolled. here is a working fiddle http://jsfiddle.net/joycse06/m6A5q/2/

I am using the $('#datepicker').blur(); because when user scrolls .demo calendar hides but the input is still focused, so when he scrolls back he can become confused. So as I blur it he will have to click on the input again and date picker will show up.

like image 90
Prasenjit Kumar Nag Avatar answered Sep 19 '22 13:09

Prasenjit Kumar Nag