Created
August 23, 2013 14:55
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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