Skip to content

Commit

Permalink
Added ability to stop datepicker from beforeShow
Browse files Browse the repository at this point in the history
Small patch to allow stopping the datepicker from a beforeShow event handler.
Just checking to see if the return from the beforeShow handler is === false.
If it is === false then we return the same way as if the datepicker is disabled.
  • Loading branch information
Karl Kirch committed Jul 6, 2011
1 parent 96de2aa commit c18edae
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
45 changes: 45 additions & 0 deletions demos/datepicker/cancelShow.html
@@ -0,0 +1,45 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Datepicker - Default functionality</title>
<link rel="stylesheet" href="../../themes/base/jquery.ui.all.css">
<script src="../../jquery-1.5.1.js"></script>
<script src="../../ui/jquery.ui.core.js"></script>
<script src="../../ui/jquery.ui.widget.js"></script>
<script src="../../ui/jquery.ui.datepicker.js"></script>
<link rel="stylesheet" href="../demos.css">
<script>
$(function() {
var showme = false;
$( "#datepicker_canceled" ).datepicker({
beforeShow: function(input, inst) {
if(!showme) {
return false;
}
}
});
$( "#datepicker" ).datepicker({
beforeShow: function(input, inst) {
}
});
});
</script>
</head>
<body>

<div class="demo">

<p>Canceled date field: <input type="text" id="datepicker_canceled"></p>
<p>Not canceled date field: <input type="text" id="datepicker"></p>

</div><!-- End demo -->



<div class="demo-description">
<p>By returning false from a beforeShow event, you can stop the datepicker from showing.</p>
</div><!-- End demo-description -->

</body>
</html>
7 changes: 6 additions & 1 deletion ui/jquery.ui.datepicker.js
Expand Up @@ -639,7 +639,12 @@ $.extend(Datepicker.prototype, {
$.datepicker._curInst.dpDiv.stop(true, true);
}
var beforeShow = $.datepicker._get(inst, 'beforeShow');
extendRemove(inst.settings, (beforeShow ? beforeShow.apply(input, [input, inst]) : {}));
var beforeShowSettings = beforeShow ? beforeShow.apply(input, [input, inst]) : {};
if(beforeShowSettings === false){
//falsey value returned, don't show
return;
}
extendRemove(inst.settings, beforeShowSettings);
inst.lastVal = null;
$.datepicker._lastInput = input;
$.datepicker._setDateFromField(inst);
Expand Down

0 comments on commit c18edae

Please sign in to comment.