Datepicker dynamic min / max dates

怎甘沉沦 提交于 2019-12-01 00:43:15

If you want to do this, you can use the "beforeShow" attribute of datepicker to help you out. Here is a pseudo-code example.

// Your start and end datepickers.
$('#dateStartMainChart, #dateEndMainChartSelect').datetimepicker({
    beforeShow: customRange
});

// I can't take credit for this...website to tutorial is below.
// From: http://test.thecodecentral.com/cms/jqueryui/datepicker/
function customRange(input) {
    return { minDate: (input.id == 'dateEndMainChartSelect' ? $('#dateStartMainChart').datepicker('getDate') : null),
        maxDate: (input.id == 'dateStartMainChart' ? $('#dateEndMainChartSelect').datepicker('getDate') : null)
    };
}

You don't even need to look at the database directly (although, you can - adjust the fields appropriately in the customRange function). In any case, this should do what you need.

OK, I finally figured this out. I initially define and use the datepicker as normal. When I need to dynamically update it, I use:

$.getScript('url.php', function() {updateDate(); });

In my case, I use this on change of a select box.

The php file that is called queries a mysql database outputs the following (using echo):

function updateDate() {
        $( "#dateStartMainChartSelect" ).datepicker('change',{ 
            minDate: new Date(2011,01,01),
            maxDate: new Date(2011,02,01)
        });
    }

You can add any of the datepicker options here that you want to change. Thanks to JasCav, I ended up looking in the right places for the solution.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!