【问题标题】:Disable dates using factory response UI Bootstrap Datepicker使用工厂响应 UI Bootstrap Datepicker 禁用日期
【发布时间】:2014-07-27 20:32:10
【问题描述】:

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

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

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 个或更多条目的日期放入范围变量中:

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

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

// 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 ));
};

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

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

【问题讨论】:

    标签: angularjs datepicker angular-ui-bootstrap


    【解决方案1】:

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

    1。 使用了我在堆栈溢出注释 http://plnkr.co/edit/Xwq7YtAD6qNHQw1aES3H?p=preview 中找到的一个小代码。它允许您使用按钮或其他类型的操作调用 Angular-UI Bootstrap Datepicker “refreshView”。基本上是建立一个新的指令

    `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 功能

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

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

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

    //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()”,完成后它将禁用。

        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());
        });
    }
    

    这个解决方案归因于这个问题和那里的 cmets: angular ui datepicker refresh disabled dates

    【讨论】:

      猜你喜欢
      • 2015-01-07
      • 1970-01-01
      • 1970-01-01
      • 2011-07-04
      • 2012-04-02
      • 2010-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多