使用工厂响应 UI Bootstrap Datepicker 禁用日期 [英] Disable dates using factory response UI Bootstrap Datepicker

查看:23
本文介绍了使用工厂响应 UI Bootstrap Datepicker 禁用日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果某个日期已经有 3 个或更多事件,我正在尝试在连接到 Google 日历的 UI Bootstrap Datepicker 中禁用该日期.

I'm trying to disable a date in the UI Bootstrap Datepicker connected to a Google calendar if that dates already have 3 or more events.

到目前为止,我使用这样的 Angular Factory 获取事件数组:

Thus far I get the array of events using an Angular Factory like this:

gardenpage.factory('Dates', function($http, $q) {
var deffered = $q.defer();
var data = [];  
var Dates = {};

Dates.async = function() {
   $http.get('http://localhost:7777/events')
   .success(function (d) {
   data = d;
   deffered.resolve();
});
return deffered.promise;
};
Dates.data = function() { return data; };

return Dates;
});

日期列表需要更多的预处理,所以我有一个函数可以将唯一具有 3 个或更多条目的日期放在范围变量中:

The list of dates needs a bit more preprocessing so I have a function that puts the only dates that have 3 or more entries in a scope-variable:

$scope.occurences = ['2014-07-21','2014-07-28'];

现在终于这是我修改后的默认 UI Bootstrap 日期选择器日期禁用功能:

Now finally this is my modified default UI Bootstrap date picker date disable function:

// Disable weekend selection
$scope.disabled = function(date, mode) {

return ( mode === 'day' && ( date.getDay() === 0 || date.getDay() === 6 || 
$scope.date_occurences.indexOf( $filter('date')(date, 'yyyy-MM-dd') ) !== -1 ));
};

它按预期工作,除了一个小怪癖,当日期选择器调用禁用"函数时,数组为空,等待我假设的异步回调.这就是为什么当我的日期被禁用时,我首先在日期选择器中选择一个日期.

It works as expected except for one little quirk, when the "disabled" function is called by the date picker, the array is empty, waiting for the async callback I presume. Which is why it's first as I select a date in the date picker as my dates gets disabled.

那么如何在调用日期选择器禁用函数之前获得回调,或者如何让它等待?一种替代方法可能是在回调到达后刷新日期选择器,但我不确定日期选择器上是否存在该函数.

So how to get the callback before the date picker disable function is called, or how do I make it wait ? One alternative might be to refresh the Datepicker after the callback has arrived, but I'm not sure if that function exists on the date picker.

推荐答案

我没有完全按照上面所说的那样解决这个问题,但有一点解决方法:

I didn't solve this exactly as stated above but a bit of a workaround:

1.使用了我在堆栈溢出评论中找到的一个小代码 http://plnkr.co/edit/Xwq7YtAD6qNHQw1aES3H?p=preview .这使您可以使用按钮或其他类型的操作调用 Angular-UI Bootstrap DatepickerrefreshView".基本上设置一个新指令

1. Used a small code that I found in a stack overflow comment http://plnkr.co/edit/Xwq7YtAD6qNHQw1aES3H?p=preview . Which lets you call the Angular-UI Bootstrap Datepicker "refreshView" using a button or other type of action. Basically setting up a new directive

`app.directive('jmDpRefreshView',function() {
   var noop = function(){};
   var refreshDpOnNotify = function (dpCtrl) {
     return function() {
       dpCtrl.refreshView();
     };
   };
return {
  require: 'datepicker',
  link: function(scope,elem,attrs,dpCtrl) {
    var refreshPromise = scope[attrs.jmDpRefreshView];
    refreshPromise.then(noop,noop,refreshDpOnNotify(dpCtrl));
   } 
 };

});`

调用refreshView功能

To call the refreshView functionality

$scope.toggleDisableMode = function() {dateDisableDeferred.notify(new Date().getTime());};

可以使用任何类型的操作调用函数 toggleDisableMode,例如使用按钮禁用来自服务器的日期:ng-click='toggleDisableMode()'"

The function toggleDisableMode can be called using any type of action, for instance using a button to disable dates from the server: "ng-click='toggleDisableMode()'"

另一件可能对您有帮助的事情是您可以从服务器预加载日期

Another thing that might help you is either you could preload your Dates from the server

//preload
$scope.dates = disable_dates();

function disable_dates() {
    console.log("disable dates function")
    Dates.async().then(function() {
        $scope.data = Dates.data();
        //do whatever you like with your data
    });
}

或者您可以在从服务器获取数据时调用.notify()"作为延迟,并在完成后禁用.

Or you could call the ".notify()" for the deferred when the data has been fetched from the server and it will disable when it is done.

    function disable_dates() {
    console.log("disable dates function")
    Dates.async().then(function() {
        $scope.data = Dates.data();
        //console.log($scope.data )
        //do whatever you like with your server data.

        //notice this line, calls the disable function
        dateDisableDeferred.notify(new Date().getTime());
    });
}

这个解决方案归因于这个问题和那里的评论:angular ui datepicker 刷新禁用日期

This solution is attributed to this question and the comments in there: angular ui datepicker refresh disabled dates

这篇关于使用工厂响应 UI Bootstrap Datepicker 禁用日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆