Skip to content

Instantly share code, notes, and snippets.

@lanks
Created July 7, 2020 20:50
Show Gist options
  • Save lanks/f76bcb2dcc5803a03431db37f97bdb9a to your computer and use it in GitHub Desktop.
Save lanks/f76bcb2dcc5803a03431db37f97bdb9a to your computer and use it in GitHub Desktop.

Revisions

  1. lanks created this gist Jul 7, 2020.
    41 changes: 41 additions & 0 deletions picker-controller.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    import { Controller } from "stimulus"
    import moment from 'moment';
    export default class extends Controller {
    static targets = [ "date_range" ]
    connect() {
    if(this.date_rangeTarget) {
    let selectedRange = false
    $(this.date_rangeTarget).daterangepicker({
    showCustomRangeLabel: false,
    alwaysShowCalendars: true,
    autoApply: false,
    ranges: {
    'Today': [moment(), moment()],
    'Last 7 days': [moment().subtract(6, 'days'), moment()],
    'Last 14 days': [moment().subtract(13, 'days'), moment()],
    'Last 30 days': [moment().subtract(29, 'days'), moment()],
    'This week': [moment().startOf('week'), moment().endOf('week')],
    'This month': [moment().startOf('month'), moment().endOf('month')],
    'Last month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')],
    'All time': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
    }
    }).on('apply.daterangepicker', function(e, picker) {
    if(selectedRange) {
    picker.show();
    selectedRange = false;
    }
    });
    $(".ranges ul li").on("click", function() {
    selectedRange = $(this).text();
    });
    }
    }
    initialize() {
    }
    rangeSelected(e, picker) {
    }
    submit(e) {
    console.log("click")
    this.element.submit()
    }
    }