问题描述
有人可以解释一下如何使用 Angular Material 将自定义日期应用为过滤器,而不会导致该天的每周都可以选择吗?
我遵循了文档,可以让它在该周的特定日期工作,但它会导致同一天的每个星期都被过滤.所有文档似乎都强调这是允许/不允许用户使用日期选择器选择特定日期的必要功能.
请看下面的代码.基本上,我所做的就是制作一个我们希望允许的日期数组.然后只使用 index of 来检查是否允许这一天.此外,您可以编写一个函数获取月份中的当前天数,如果用户选择日期,则从 currentDaysInMonth 数组中弹出该天.这是我的 codepen.希望有帮助,我可以根据需要更新.
angular.module('MyApp', ['ngMaterial', 'ngMessages', 'material.svgAssetsCache']).controller('AppCtrl', function($范围) {$scope.myDate = 新日期();$scope.minDate = 新日期($scope.myDate.getFullYear(),$scope.myDate.getMonth() - 2,$scope.myDate.getDate());$scope.maxDate = 新日期($scope.myDate.getFullYear(),$scope.myDate.getMonth() + 2,$scope.myDate.getDate());//如果你处理的日期没有用某种时间戳格式化//你可以使用这样的函数来格式化然后相应地过滤var daysAvailableThisMonth = [1, 15, 30];函数格式化日期(天){var currentMonth = new Date().getMonth();var currentYr = new Date().getFullYear();返回 {日:新日期(currentYr,currentMonth,day),预订:假};}函数 getDaysInMonth(月,年){var date = new Date(年,月,1);var 天 = [];while (date.getMonth() === 月) {//您可以根据需要设置默认标志,但它会帮助过滤.天.推({日:新日期(日期),预订:真实});date.setDate(date.getDate() + 1);}返回天数;}var currentMonthDayArray = getDaysInMonth(new Date().getMonth(), new Date().getFullYear());daysAvailableThisMonth.forEach(function(day, index) {daysAvailableThisMonth[index] = formattedDate(day);});currentMonthDayArray.forEach(函数(预订){daysAvailableThisMonth.forEach(function(date) {if (date.day.getTime() == booking.day.getTime()) {预订.预订=假;}})});$scope.onlyWeekendsPredicate = 函数(日期){for (var i = 0; i < currentMonthDayArray.length; i++) {if (currentMonthDayArray[i].day.getTime() === date.getTime() && currentMonthDayArray[i].booked === false) {返回真;}}};});/**版权所有 2016 Google Inc.保留所有权利.此源代码的使用受 MIT 风格的许可管理,该许可可在 http://material.angularjs.org/HEAD/license 的许可文件中找到.**/
<md-content layout-padding=""><div layout-gt-xs="row"><div flex-gt-xs=""><h4>只有给定范围内的周末是可选的</h4><md-datepicker ng-model="myDate" md-placeholder="输入日期" md-min-date="minDate" md-max-date="maxDate" md-date-filter="onlyWeekendsPredicate"><;/md-datepicker><div flex-gt-xs=""><h4>输入焦点时打开日历</h4><md-datepicker ng-model="myDate" md-placeholder="输入日期" md-open-on-focus=""></md-datepicker>