Skip to content

Instantly share code, notes, and snippets.

@pjsvis
Created August 23, 2013 14:55
Show Gist options
  • Select an option

  • Save pjsvis/6320268 to your computer and use it in GitHub Desktop.

Select an option

Save pjsvis/6320268 to your computer and use it in GitHub Desktop.
An angular directive to wrap Dan Grossman's excellent date range picker ref https://github.com/dangrossman/bootstrap-daterangepicker
angular.module('daterangepicker', []);
angular.module('daterangepicker').directive('dgDaterangepicker', [function () {
'use strict';
return{
restrict: 'A',
link: function (scope, elem, attrs) {
var options = attrs.options;
var parseOptions = function (options) {
if (!options) return;
var arrOpts = options.split(',');
var len = arrOpts.length;
var opts = {};
for (var i = 0; i < len; i++) {
var arrItem = arrOpts[i].split(':');
// Only handle paired options
arrItem.length > 1 ? opts[arrItem[0].trim()] = arrItem[1].trim() : angular.noop();
}
return opts;
};
var opts = parseOptions(attrs.options) || {};
opts.ranges = {
'Today': [moment(), moment()],
'Yesterday': [moment().subtract('days', 1), moment().subtract('days', 1)],
'Last 7 Days': [moment().subtract('days', 6), moment()],
'Last 30 Days': [moment().subtract('days', 29), moment()],
'This Month': [moment().startOf('month'), moment().endOf('month')],
'Last Month': [moment().subtract('month', 1).startOf('month'), moment().subtract('month', 1).endOf('month')]
};
console.log(opts);
scope.$watch(attrs.startDate, function () {
render();
});
scope.$watch(attrs.endDate, function () {
render();
});
var render = function () {
elem.daterangepicker(opts);
};
}
}
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment