æ¥ä»ãé¸æãããéãä»ã¾ã§jQueryUIã®Datepickerã使ã£ã¦ãã¾ãããä½æ°ã«NuGetã§æ¤ç´¢ãã¦ãã¨Datepicker for Bootstrapã¨ããã®ãè¦ã¤ããã®ã§ä½¿ã£ã¦ã¿ã¾ããã
1.ããã±ã¼ã¸ãã¤ã³ã¹ãã¼ã«
GitHub*1ãããã¦ã³ãã¼ããã¦æåã§çµã¿è¾¼ãã§ãããã®ã§ããããã£ãããªã®ã§NuGet使ãã¾ãããã
ããã±ã¼ã¸ããã¼ã¸ã£ã®ã³ã³ã½ã¼ã«ã§ä»¥ä¸ã®ã³ãã³ããå©ãã¾ãã
> Install-Package Bootstrap.Datepicker
NuGetã使ãã¨ä¾åé¢ä¿ã¨ã解決ãã¦ãããã®ã§æ¥½ã¡ãã§ããã
2.jsãèªã¿è¾¼ãããè¨å®
æ¬ä½ã®bootstrap-datepicker.jsãèªã¿è¾¼ãããã«ãã¾ãã
BundleConfig.csã以ä¸ã®æ§ã«å¤æ´ãã¾ã*2
bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include( "~/Scripts/bootstrap.js", "~/Scripts/bootstrap-datepicker.js", "~/Scripts/respond.js"));
3.ã¯ã©ã¹ãè¨å®
æ¥ä»é¸æããããå ¥åé ç®ã«é©å½ãªã¯ã©ã¹ãããã¯IDãè¨å®ãã¾ãã
ä»åã¯datepickerã¨ããã¯ã©ã¹åã«ãã¾ããã
<div class="form-group"> @Html.LabelFor(model => model.Date, new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Date, new { htmlAttributes = new { @class = "datepicker" } }) @Html.ValidationMessageFor(model => model.Date) </div> </div>
4.åæåã®JavaScriptãè¨è¿°
DatePickerã使ããããã«ä»¥ä¸ã®æ§ãªè¨è¿°ã追å ãã¾ãã
@section Scripts { <script type="text/javascript"> $(function () { $('.datepicker').datepicker(); }) </script> }
以ä¸ã§DatePickerã使ããããã«ãªãã¾ãã
ãã¾ã
æ¥ä»ã®å½¢å¼ãå¤ãã
$('.datepicker').datepicker({ format: 'yyyy/mm/dd' });
ææ¥ãæ¥æ¬èªè¡¨è¨ã«ãã
NuGetã§ã¤ã³ã¹ãã¼ã«ããã¨Scripts/lopcalesã«åè¨èªå¥ã®jsãæ ¼ç´ããã¾ãã®ã§ãæ¥æ¬èªç¨ã®js*3ãèªã¿è¾¼ã¾ããããã«BundleConfig.csã以ä¸ã®æ§ã«å¤æ´ãã¾ãã
bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include( "~/Scripts/bootstrap.js", "~/Scripts/bootstrap-datepicker.js", "~/Scripts/locales/bootstrap-datepicker.ja.js", "~/Scripts/respond.js"));
å¾ã¯åæåã®éã«è¨èªãæå®ããã ãã§ãã
$('.datepicker').datepicker({ language: 'ja' });
æ¥ä»é¸æå¾ã«èªåã§éãã
$('.datepicker').datepicker({ autoclose: 'true' });
*1:eternicode/bootstrap-datepicker
*2:å¥ã®ScriptBundleãAddãã¦ããããã©ãããã¯å¥½ã¿ã§
*3:bootstrap-datepicker.ja.js