dojodojox.grid.datagriddijit.formdojox.calendardijit.calendar

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


I'm trying to load a dijit calendar on click of dijit icon button with specific dates disabled,

for this I tried two ways

first one: loading calendar in js function
getting error as trying to register "id==mycal" but that id is already registered ."

    <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);

        },

Second method: loading calendar in postcreate of js function with var calendar = registry.byId("mycal"); if I load with below html and passing isdisableDate arguemnts in postcreate function , the disable dates are not applying on startup but they are applying only after the selection of some date I need this to be applied on startup of calendar

<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() ;
          }
          },

how can I get disabled dates on calendar startup with these any one of the methods.


Solution

  • the error is because you are creating a widget ( new ) with id mycal that was already instantiated ( defined in dojo registry ) ,

    all you need is put the instantiation in postCreate and in the button just toggle display using the "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");
    },
    

    then your button will have only show or hide calendar ,

    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");
    
    }
    

    add also css to hide calendar on startup

    #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>