Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery ui datepicker positioning problem when scrolling down webpage

I have a webpage that uses multiple instances of the jQuery ui datepicker. My webpage will display ~80 records which extends beyond a single screenshot.

<% foreach (var record in Model) { %>
    <div class="recordname"><%=record.name%></div>
    <%=Html.TextBox("DateTimePicker", null, new { @class = "date-pick" } )%>
    // <-- additional html here -->
<% } %> 

I have set the defaults of my datepicker as follows:

    $(".date-pick").each(function() {
    $(this).datepicker({
        dateFormat: 'dd M yy',
        showOn: 'button',
        buttonImage: '/Images/datepickericon.png',
        buttonImageOnly: true
        });
    });

When the page first loads, if I click any datepicker icon that is visible on screen (i.e. without scrolling) then the datepicker appears as expected.

However, if I scroll down the page and then click a datepicker icon, the datepicker does not appear in the screen window but is instead rendered right back near the top of the screen.

Any ideas how to solve this?

I am using:

  • IE7
  • asp.net mvc
  • jquery.ui.datepicker.js (UI/API/1.8/Datepicker)
like image 714
Alex P Avatar asked May 14 '10 14:05

Alex P


2 Answers

I had the same problem too, I'm using IE9 but instead to use document.documentElement.scrollTop I edit the follwing line on my JS code

$.datepicker._pos[1] += input.offsetHeight + document.body.scrollTop;

This is because document.documentElement.scrollTop returns 0, for me the above code solves my problem

like image 54
Christian Suarez Avatar answered Sep 28 '22 03:09

Christian Suarez


I think I have managed to resolve my issue but at the same time I think I may have uncovered a bug (?) in the jquery ui datepicker code.

Before I go further then let me just say that I am new to jquery / javascript and would appreciate any input on the below.

To recap the problem, I am using asp.net mvc and I am trying to show ~80 datepickers on a page. For any datepicker in the initial viewport, the datepickers position fine. However, when I scroll down the datepickers are still rendered near the top of the screen.

I started to view the source code and do some debugging. What I noticed is that the extent to which the datepicker was offset towards the top of the screen was directly proportional to the amount by which I had scrolled i.e. more scroll = greater offset.

The key issue in the code I found (see below) is that when the code works out where to position the datepicker it does not take into account how far down the screen a user has scrolled:

_showDatepicker: 
function(input) { 
      input = input.target || input;
      //....more code...

      if (!$.datepicker._pos) { // position below input
      $.datepicker._pos = $.datepicker._findPos(input);
      $.datepicker._pos[1] += input.offsetHeight; //add the height
       }

In the final line the offsetHeight method does not take account of how much you have scrolled down the screen by.

If you update this line to the following then this solves the issue I have raised:

  $.datepicker._pos[1] += input.offsetHeight + document.documentElement.scrollTop; //add the height 

This simply gets the scrollbar position and adds it to offsetHeight to get the correct position.

In the interests of full disclosure I am not exactly sure why this works and would appreciate some insight.

I have spent ~ 3 days on this issue and researched the web and this is the best I can come up with.

Interestingly, there was a similar query in the jquery forums:

Click here to view

Reading the report it seemed to suggest that the bug was fixed in a prior version to 1.8?

Thanks

like image 31
Alex P Avatar answered Sep 28 '22 03:09

Alex P