material icon indicating copy to clipboard operation
material copied to clipboard

mdDatepicker calendar opens up to wrong view

Open ppham27 opened this issue 9 years ago • 31 comments

Actual Behavior:

  • What is the issue? * When I click on the datepicker, the calendar view shows the wrong month.
  • What is the expected behavior? It should open up to the currently selected date or the current date.

CodePen (or steps to reproduce the issue): *

  • CodePen Demo which shows your issue: https://material.angularjs.org/latest/demo/datepicker
  • Details: Here's a video, https://youtu.be/N3EK1ByvhV0

Angular Versions: *

  • Angular Version: 1.5.9
  • Angular Material Version: 1.1.1

Additional Information:

  • Browser Type: * Chrome
  • Browser Version: * 55
  • OS: * Ubuntu 12.04
  • Stack Traces: N/A

I should note that this problem doesn't occur in OS X. It also only occurs on the Angular Material Demo page, but works fine in the CodePen.

ppham27 avatar Dec 15 '16 19:12 ppham27

I see the same problem with Angular Material 1.0.6. It actually scrolls to April 2017 for me.

jsgoupil avatar Dec 15 '16 21:12 jsgoupil

I have created a plnkr with a similiar problem. md-datepicker doesn't even render.

  • Angular Version: 1.6.0
  • Angular Material Version: 1.1.1
  • Google Chrome Version: 54

https://plnkr.co/edit/4YBK4Eb8LVNeQbH5wIac

xxmuaddib avatar Dec 16 '16 07:12 xxmuaddib

I have this problem with:

  • Angular 1.5.9
  • Angular Material 1.1.1
  • Chrome 55
  • OS: macOS 10.12.1

rhinoceraptor avatar Dec 16 '16 18:12 rhinoceraptor

Confirming same problem:

  • Angular 1.5.8
  • Angular Material 1.1.1
  • Chrome 55.0.2883.87 (64-bit)
  • Kubuntu 16.04.1 LTS

Note that this issue does not seem to occur in Firefox. I have not tested in other browsers.

Steps to reproduce on the demo page (https://material.angularjs.org/latest/demo/datepicker):

  • Open first datepicker, note the start date is correct
  • Click <> to view code
  • Click same datepicker, note the start date is not correct

DAquilina avatar Dec 20 '16 16:12 DAquilina

Same issue with:

Angular: 1.5.8 Material: 1.1.0 Browser: Chrome 55.0.2883.95 (64-bits) Mac OS X El Capitan 10.11.6

julioponce avatar Dec 21 '16 18:12 julioponce

Same issue with:

Angular: 1.5.8 Material: 1.1.1 Browser: Chrome 55.0.2883.87 OS: Windows 10

Adding: How far ahead it scrolls appears to be related with it's position on the page. The further down on the page the datepicker is, more months past the selected date it will scroll

Dinosys avatar Dec 22 '16 08:12 Dinosys

Confirming same issue:

Angular. 1.5.10 Material: 1.1.1 Browser: Chrome 56.0.2924.28 beta (64-bit) OS: Windows 10

If I resize the browser window or scroll the md-content a little bit the issue disappears.

paynam avatar Dec 22 '16 21:12 paynam

Has anyone been able to fix this problem?

kc1116 avatar Dec 23 '16 17:12 kc1116

I submitted a pull request that should fix the issue. Please try the below CodePens below to see if it fixes it for you.

CodePens:

ppham27 avatar Dec 25 '16 01:12 ppham27

@ppham27 Your patch seems to work with 1.1.1, but not with 1.0.6.

I'm not expecting to make it work with that old of a version, but letting people know that if they take only his change in their version, it might not work.

jsgoupil avatar Dec 28 '16 02:12 jsgoupil

Same issue (version 1.1.1 - before fix):

  • Angular 1.5.5
  • Angular Material 1.1.1
  • Chrome 55.0.2883.87
  • OS: Windows 10

Codepen: http://codepen.io/czesiu/pen/dNpdWO

I tired to fix this and for me works commenting this line: https://github.com/angular/material/blob/master/src/components/virtualRepeat/virtual-repeater.js#L357

EDIT: @ppham27 fix also works for me

czesiu avatar Jan 16 '17 08:01 czesiu

Any patch release soon? Or otherwise, a pointer on how we can point to master and get the latest code using npm is appreciated

EDIT: looks like PR not merged yet https://github.com/angular/material/pull/10177

ernsheong avatar Jan 24 '17 04:01 ernsheong

@ernsheong With NPM, just update your package.json to specify angular-material#master. That should grab the latest.

topherfangio avatar Jan 30 '17 19:01 topherfangio

This is ~~~a bit~~~ very gross, but while we wait for the PR to be merged, I've done the following

angular.module("material.components.virtualRepeat")._invokeQueue[1][2][1]().controller.prototype.virtualRepeatUpdate_ = function (items, oldItems) {
        this.isVirtualRepeatUpdating_ = true;

        var itemsLength = items && items.length || 0;
        var lengthChanged = false;

        // If the number of items shrank, keep the scroll position.
        if (this.items && itemsLength < this.items.length && this.container.getScrollOffset() !== 0) {
            this.items = items;
            var previousScrollOffset = this.container.getScrollOffset();
            this.container.resetScroll();
            this.container.scrollTo(previousScrollOffset);
        }

        if (itemsLength !== this.itemsLength) {
            lengthChanged = true;
            this.itemsLength = itemsLength;
        }

        this.items = items;
        if (items !== oldItems || lengthChanged) {
            this.updateIndexes_();
        }

        this.parentNode = this.$element[0].parentNode;

        if (lengthChanged) {
            this.container.setScrollSize(itemsLength * this.itemSize);
        }

        var cleanupFirstRender = false, firstRenderStartIndex;
        if (this.isFirstRender) {
            cleanupFirstRender = true;
            this.isFirstRender = false;
            firstRenderStartIndex = this.$attrs.mdStartIndex ?
                this.$scope.$eval(this.$attrs.mdStartIndex) :

                this.container.topIndex;
            this.container.scrollToIndex(firstRenderStartIndex);
        }


        // Detach and pool any blocks that are no longer in the viewport.
        Object.keys(this.blocks).forEach(function (blockIndex) {
            var index = parseInt(blockIndex, 10);
            if (index < this.newStartIndex || index >= this.newEndIndex) {
                this.poolBlock_(index);
            }
        }, this);

        // Add needed blocks.
        // For performance reasons, temporarily block browser url checks as we digest
        // the restored block scopes ($$checkUrlChange reads window.location to
        // check for changes and trigger route change, etc, which we don't need when
        // trying to scroll at 60fps).
        this.$browser.$$checkUrlChange = angular.noop;

        var i, block,
            newStartBlocks = [],
            newEndBlocks = [];

        // Collect blocks at the top.
        for (i = this.newStartIndex; i < this.newEndIndex && this.blocks[i] == null; i++) {
            block = this.getBlock_(i);
            this.updateBlock_(block, i);
            newStartBlocks.push(block);
        }

        // Update blocks that are already rendered.
        for (; this.blocks[i] != null; i++) {
            this.updateBlock_(this.blocks[i], i);
        }
        var maxIndex = i - 1;

        // Collect blocks at the end.
        for (; i < this.newEndIndex; i++) {
            block = this.getBlock_(i);
            this.updateBlock_(block, i);
            newEndBlocks.push(block);
        }

        // Attach collected blocks to the document.
        if (newStartBlocks.length) {
            this.parentNode.insertBefore(
                this.domFragmentFromBlocks_(newStartBlocks),
                this.$element[0].nextSibling);
        }
        if (newEndBlocks.length) {
            this.parentNode.insertBefore(
                this.domFragmentFromBlocks_(newEndBlocks),
                this.blocks[maxIndex] && this.blocks[maxIndex].element[0].nextSibling);
        }


        if (cleanupFirstRender) {
            this.container.scrollToIndex(firstRenderStartIndex);
        }


        // Restore $$checkUrlChange.
        this.$browser.$$checkUrlChange = this.browserCheckUrlChange;

        this.startIndex = this.newStartIndex;
        this.endIndex = this.newEndIndex;

        this.isVirtualRepeatUpdating_ = false;
    };

nganbread avatar Feb 19 '17 20:02 nganbread

did we get this fixed yet ? Please let me know, if there is a quick way around this ?

shardool avatar Feb 22 '17 02:02 shardool

This is not the best solution but a quick fix, just reseting the scroller to 0 to keep the top of the scroll in hideVerticalScrollbar function:

CalendarCtrl.prototype.hideVerticalScrollbar = function(childCtrl) {
...
...
var scroller = childCtrl.calendarScroller;
scroller.scrollTop = 0;
...
...
};

julioponce avatar Feb 22 '17 04:02 julioponce

I have pulled the master code using bower install angular-material#master --save command but I am experiencing same problem and the datepicker is showing 3 months in future upon opening.

Can anyone help me in that ?

ghost avatar Feb 25 '17 12:02 ghost

In my application, the issue still occurs when the md-min-date is set. When I use md-date-filter instead of the md-min-date it works.

cisvanammel avatar Mar 02 '17 15:03 cisvanammel

The issue is still occurring in master branch. Can we reopen this @ppham27 ?

ernsheong avatar Mar 03 '17 09:03 ernsheong

I'm seeing this problem on:

Angular: 1.6.0. Angular Material: 1.1.1.

lobo-tuerto avatar Mar 09 '17 22:03 lobo-tuerto