datepicker minDate

datepicker minDate

montoyammontoyam Posts: 327Questions: 81Answers: 4

I need the minDate option to depend on a value in another datefield. I read that minDate won't accept a function, so what I have seen in non-DataTable projects is code like this:

$('[name=myradiogroup]').change(function () {
    var newMininumDate = /*insert logic here*/;
    $('#datepicker').datepicker('option', 'minDate', newMininumDate);
});

One way I was thinking of doing this in DataTables is to use the .clear() and .add() on open of the Editor (or select of the DataTable), like this:

var newMininumDate = /*insert logic here*/;
editor.add( {
    type:  'datetime',
    label: 'Start date:',
    name:  'start_date',
    opts:  {
        minDate :newMininumDate 
    }
} );

I just wanted to make sure this is the most efficient way to do this?

This question has accepted answers - jump to:

Answers

  • colincolin Posts: 9,822Questions: 0Answers: 1,629
    Answer ✓

    Yep, that's a good way to go with that - and the only real solution for that use case,

    Colin

  • montoyammontoyam Posts: 327Questions: 81Answers: 4

    thanks. here is my complete code in case anyone has the same type of need :)

                FundOrgDeptGroupEditor.on('open', function () {
                    var effectiveDate = FundOrgDeptGroupEditor.get("FundOrgDeptGroup_Header.effectivedate");
                    FundOrgDeptGroupEditor
                        .clear('FundOrgDeptGroup_Header.expiredate')
                        .add({
                                type: 'date',
                                label: 'Expire Date:',
                                name: 'FundOrgDeptGroup_Header.expiredate',
                                opts: {
                                    changeMonth: true,
                                    changeYear: true,
                                    showButtonPanel: true,
                                    dateFormat: 'm/d/yy',
                                    minDate: effectiveDate,
                                    onClose: function (dateText, inst) {
                                        var eom = new Date((new Date(inst.selectedYear, inst.selectedMonth + 1, 1)) - 1).getDate();
                                        $(this).datepicker('setDate', new Date(inst.selectedYear, inst.selectedMonth, eom));
                                    }
                                }
                        });
                });
    
  • montoyammontoyam Posts: 327Questions: 81Answers: 4

    a co-worker did this in what seems to be a cleaner way:

       SLAeditor.on('open', function (e, data, action) {
                        if (action == 'edit') {
                            SLAeditor.field('EarlyTerminationDate').minDate(new Date(SLAeditor.get("TermStartDate")));
                            SLAeditor.field('EarlyTerminationDate').maxDate(new Date(SLAeditor.get("TermEndDate")));
                        }
                    });
    
  • lopezmirlopezmir Posts: 1Questions: 0Answers: 1
    Answer ✓

    Thanks for posting. Here's what I tried:

       SLAeditor.on('open', function (e, data, action) {                   
                           
                SLAeditor.field('EarlyTerminationDate').minDate(new Date(SLAeditor.get("TermStartDate")));
                SLAeditor.field('EarlyTerminationDate').maxDate(new Date(SLAeditor.get("TermEndDate")));
                        }
                    });
    
  • allanallan Posts: 52,661Questions: 1Answers: 8,058 Site admin

    Agreed - if you want to dynamically set the minDate, then using the field().minDate() method for the datetime input would be the way to do it rather than removing and adding the field each time.

    Allan

Sign In or Register to comment.