How to check date is change in date range picker?

Issue

I am using Bootstrap Date Range Picker and i want that if user change date then i submit form for fresh data. Here is my code,

$(document).ready(function (){
    var start = moment().subtract(29, 'days');
    var end = moment();

    function cb(start, end) {
        $('#reportrange span').html(start.format('MMMM D, YYYY') + ' - ' + end.format('MMMM D, YYYY'));
        $('#start_date').val(start.format('YYYY-MM-DD'))
        $('#end_date').val(end.format('YYYY-MM-DD'))
        
    }

    $('#reportrange').daterangepicker({
        startDate: start,
        endDate: end,
        ranges: {
            'Today': [moment(), moment()],
            'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
            'Last 7 Days': [moment().subtract(6, 'days'), moment()],
            'Last 30 Days': [moment().subtract(29, 'days'), moment()],
            'This Month': [moment().startOf('month'), moment().endOf('month')],
            'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
        }
    }, cb);

    cb(start, end);
})

and my form is
enter image description here
how can i do that. Thanks

Solution

Duplicate? I guess this is your way anwser for your question.

$('#reportrange').on('apply.daterangepicker', function(ev, picker) {
    alert ('hello');
});

Either way, you can submit your form when the date changed using your callback (cb), by adding some trigger to submit form, such as: $("#someform").submit(); Make sure that you are inserting the selected date in each change to form input.

Moreover, in daterangepicker examples. I think you`ll find there your solution aswell.

I took your code and just change span to input and html to val and there you go.

<script type="text/javascript">
  $(document).ready(function () {
    var start = moment().subtract(29, "days");
    var end = moment();

    function cb(start, end) {
      $("#reportrange input").val(
        start.format("MMMM D, YYYY") + " - " + end.format("MMMM D, YYYY")
      );
      $("#start_date").val(start.format("YYYY-MM-DD"));
      $("#end_date").val(end.format("YYYY-MM-DD"));
      $("#someform").submit();
    }

    $("#reportrange").daterangepicker(
      {
        startDate: start,
        endDate: end,
        ranges: {
          Today: [moment(), moment()],
          Yesterday: [
            moment().subtract(1, "days"),
            moment().subtract(1, "days")
          ],
          "Last 7 Days": [moment().subtract(6, "days"), moment()],
          "Last 30 Days": [moment().subtract(29, "days"), moment()],
          "This Month": [
            moment().startOf("month"),
            moment().endOf("month")
          ],
          "Last Month": [
            moment().subtract(1, "month").startOf("month"),
            moment().subtract(1, "month").endOf("month")
          ]
        }
      },
      cb
    );

    cb(start, end);
  });
</script>

Answered By – Alon Albahari

Answer Checked By – David Goodson (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.