单击禁用特定日期的 dijit 图标按钮加载 dijit 日历

loading dijit calendar on click of dijit icon button with specific dates disabled

我正在尝试通过单击禁用特定日期的 dijit 图标按钮来加载 dijit 日历,

为此我尝试了两种方法

第一个: 在 js 函数中加载日历
尝试注册时出现错误 "id==mycal" 但该 ID 已注册。

    <button  data-dojo-type="dijit.form.Button" data-dojo-attach-event="onClick:oncldriconclick" data-dojo-props="iconClass:' dijitIconTable', showLabel: false" type="button"></button>
<div id="mycal"   data-dojo-attach-event="onclick: _onDayClick"  ></div>

oncldriconclick : function() {


            disable_before_days = 2;
            var calendar = new Calendar({
                value: new Date(),
                isDisabledDate:function(date, localString){
                  return dojoDate.difference(date, new Date(), "day") > disable_before_days 
                      || locale.isWeekend(date) 
                      || date > new Date() ;
                }
               }, "mycal");
            },

            onDayClick:function(evt){

            alert(evt);

        },

第二种方法:如果我使用以下 html 加载并在 postcreate 函数中传递 isdisableDate 参数,则使用 var calendar = registry.byId("mycal"); 在 js 函数的 postcreate 中加载日历,禁用日期不在启动时应用,但它们仅在选择某个日期后才应用我需要在日历启动时应用此日期

<button  data-dojo-type="dijit.form.Button" data-dojo-attach-event="onClick:oncldriconclick" data-dojo-props="iconClass:' dijitIconTable', showLabel: false" type="button"></button>
<div id="mycal" class="mycal" data-dojo-attach-point="mycal"  data-dojo-type="dijit.Calendar"   data-dojo-attach-event="onChange: _onDayClick"  ></div>



postCreate: function(){

disable_before_days = 2;
        var calendar = registry.byId("mycal");
          console.log(locale );
          calendar.isDisabledDate = function(date, localString){
              return dojoDate.difference(date, new Date(), "day") > disable_before_days || locale.isWeekend(date) || date > new Date() ;
          }
          },

如何使用其中任何一种方法在日历启动时禁用日期。

错误是因为您正在创建一个小部件(新),其 ID mycal 已经实例化(在 dojo 注册表中定义),

你所需要的只是将实例化放在 postCreate 和按钮中,只需使用 "dojo/dom-style" class :

切换显示
calendar:null,
postCreate: function(){
   calendar = new Calendar({
                value: new Date(),
                isDisabledDate:function(date, localString){
                  return dojoDate.difference(date, new Date(), "day") > disable_before_days 
                      || locale.isWeekend(date) 
                      || date > new Date() ;
                }
    }, "mycal");
},

那么你的按钮将只显示或隐藏日历,

oncldriconclick : function() {
   if(domStyle.get(this.calendar.domNode,"display") === "none")
       domStyle.set(this.calendar.domNode,"display","table");
    else 
       domStyle.set(this.calendar.domNode,"display","none");

}

还添加 css 以在启动时隐藏日历

#mycal {
  display:none;
}

require(["dojo/parser",
    "dijit/form/Button",
    "dijit/Calendar",
    "dojo/dom-style",
    "dijit/registry",
    "dojo/date",
    "dojo/date/locale",
    "dojo/ready",
    "dojo/domReady!"
], function(parser, Button, Calendar,domStyle, registry, dojoDate, locale, ready){

    disable_before_days = 52;

    ready(function(){
    
      var button = registry.byId("btn");
      
      button.on("click",function(e){
        if(domStyle.get(calendar.domNode,"display") === "none")
          domStyle.set(calendar.domNode,"display","table");
        else 
          domStyle.set(calendar.domNode,"display","none");
      });
      
      var calendar = new Calendar({
        value: new Date(),
        isDisabledDate:function(date, localString){
          return dojoDate.difference(date, new Date(), "day") > disable_before_days 
              || locale.isWeekend(date) 
              || date > new Date() ;
        }
       }, "mycal");
    });
    
});
html,
body {
  height: 100%;
  padding: 0;
  margin: 0;
  font-family: Lucida Sans, Lucida Grande, Arial !important;
  font-size: 13px !important;
  background: white;
  color: #333;
}

#mycal .dijitCalendarDisabledDate {
  background-color: #333;
  text-decoration: none;
}

#mycal .dijitCalendarContainer {
  margin: 25px auto;
}

#mycal {
  display:none;
}
<link href="https://ajax.googleapis.com/ajax/libs/dojo/1.8.3/dojox/calendar/themes/claro/Calendar.css" rel="stylesheet" />
<link href="https://ajax.googleapis.com/ajax/libs/dojo/1.8.3/dijit/themes/claro/claro.css" rel="stylesheet" />

<script type="text/javascript">
  dojoConfig = {
    isDebug: true,
    async: true,
    parseOnLoad: true
  }
</script>

<script src="https://ajax.googleapis.com/ajax/libs/dojo/1.10.4/dojo/dojo.js"></script>

<body class="claro">
  <div id="btn" data-dojo-type="dijit/form/Button">Show / Hide</div><br />
  <div id="mycal" style="display:none"></div>

</body>