And*_*erg 6 datepicker angularjs angular-ui-bootstrap
我正在尝试禁用连接到Google日历的UI Bootstrap Datepicker中的日期,如果该日期已经有3个或更多事件.
到目前为止,我使用像这样的Angular Factory获取事件数组:
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;
});
Run Code Online (Sandbox Code Playgroud)
日期列表需要更多的预处理,所以我有一个函数,只在范围变量中包含3个或更多条目的日期:
$scope.occurences = ['2014-07-21','2014-07-28'];
Run Code Online (Sandbox Code Playgroud)
现在终于这是我修改后的默认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 ));
};
Run Code Online (Sandbox Code Playgroud)
它按预期工作,除了一个小怪癖,当日期选择器调用"禁用"函数时,数组为空,等待我假设的异步回调.这就是为什么我首先选择日期选择器中的日期,因为我的日期被禁用了.
那么如何在调用日期选择器禁用函数之前获取回调,或者如何让它等待?一种替代方法可能是在回调到达后刷新Datepicker,但我不确定日期选择器上是否存在该函数.
我没有完全按照上面所述解决这个问题,但有一些解决方法:
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));
}
};
Run Code Online (Sandbox Code Playgroud)
});`
调用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
});
}
Run Code Online (Sandbox Code Playgroud)
或者,您可以在从服务器获取数据时调用“.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());
});
}
Run Code Online (Sandbox Code Playgroud)
该解决方案归因于这个问题和其中的评论: Angular ui Datepicker刷新禁用日期
| 归档时间: |
|
| 查看次数: |
4644 次 |
| 最近记录: |