Scroll to current date when opening md-calender

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Scroll to current date when opening md-calender



Currently building an app with angular material where we need a md-calendar component. We want to customize the button style and content and therefore don't use the normal md-datepicker. Problem is that when the md-calender is opened the scroll position is on 1933. The current date is correctly set.



How can I set the scroll position to be the current date?



The md-datepicker is using the md-calender as a subcomponent where the calendar is scrolled to the current date so should not be that hard to achieve.



The current work-around is to set the md-min-date property to a date close to the current date but this is not a good solution as it prohibits navigation to earlier dates.



Code pen example:
https://codepen.io/adam-wiman/pen/QKqRzd
<md-calendar>


<md-calendar>




1 Answer
1



Wll first I tried to upgrade to the latest angular material v1.1.10 which was the solution I got when trying to answer this SO Answer, but doing this did not help solve your issue, although you can upgrade to latest, if you want to get rid of some bugs.


angular material v1.1.10


SO Answer



Anyway, the problem is due to the md-datepicker not being properly intialized, there could be a number of reasons for that, my solution for your problem is to use the good old trusty, ng-if to reinitialize the md-calendar, doing so solves this issue.


md-datepicker


ng-if


md-calendar



Note: Using ng-if will create an isolated scope, thus there is a possiblity of $scopevariables not updating properly, in these scenarios, you need to use the $parent property to solve it, refer here.


ng-if


$scope


$parent


angular.module('myApp',['ngMaterial']).controller('AppCtrl', function($scope)
$scope.myDate = new Date();

$scope.minDate = new Date(
$scope.myDate.getFullYear(),
$scope.myDate.getMonth() - 2,
$scope.myDate.getDate());

);

/*
Copyright 2016 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that can be in foundin the LICENSE file at https://material.angularjs.org/license.
*/


<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-messages.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-aria.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-animate.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-material/1.1.10/angular-material.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/angular-material/1.1.10/angular-material.min.css" rel="stylesheet"/>
<body ng-app="myApp" ng-controller="AppCtrl" ng-cloak>

<md-menu>
<md-button style="text-transform: none;background-color:grey;" aria-label="Select date" ng-click="$mdMenu.open($event);tempHide=true;" ng-init="tempHide=false;">Select Date</md-button>
<md-menu-content style="overflow: hidden;" width="5" >
<md-calendar ng-model="myDate" ng-if="tempHide">
</md-calendar>
</md-menu-content>
</md-menu>

<md-menu>
<md-button style="text-transform: none;background-color:grey;" aria-label="Select date" ng-click="$mdMenu.open($event)">Select Date (using min-date)</md-button>
<md-menu-content style="overflow: hidden;" width="5" >
<md-calendar ng-model="myDate" md-min-date="minDate">
</md-calendar>
</md-menu-content>
</md-menu>

</body>

<!--
Copyright 2016 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that can be in foundin the LICENSE file at https://material.angularjs.org/license.
-->






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered