jQuery ui - datepicker prevent refresh onSelect

前端 未结 5 1290
一整个雨季
一整个雨季 2020-12-17 20:09

I\'m using jQuery ui Datepicker to display a yearly inline calendar full of \"special dates\" (with colors). \"enter

5条回答
  •  攒了一身酷
    2020-12-17 20:26

    I was having a similar problem. I was adding custom buttons to the bottom of the datepicker (using $(id).append), but when I would select a date the datepicker would refresh and destroy them.

    This is the date selection function for the datepicker in the jquery-ui library:

    _selectDate: function(id, dateStr) {
      ...
        if (onSelect)
            onSelect.apply((inst.input ? inst.input[0] : null), [dateStr, inst]);
      ...
        if (inst.inline)
            this._updateDatepicker(inst);
      ...
    },
    

    As you can see, the function first calls the onSelect event, and then calls _updateDatepicker (which is what redraws the form) if inst.inline is true.

    This is my workaround to prevent the form from refreshing while maintaining the selection functionality:

    $("#cal_id").datepicker({
      onSelect: function(date, inst){
    
        //This is the important line.
        //Setting this to false prevents the redraw.
        inst.inline = false;
    
        //The remainder of the function simply preserves the 
        //highlighting functionality without completely redrawing.
    
        //This removes any existing selection styling.
        $(".ui-datepicker-calendar .ui-datepicker-current-day").removeClass("ui-datepicker-current-day").children().removeClass("ui-state-active");
    
        //This finds the selected link and styles it accordingly.
        //You can probably change the selectors, depending on your layout.
        $(".ui-datepicker-calendar TBODY A").each(function(){
          if ($(this).text() == inst.selectedDay) {
            $(this).addClass("ui-state-active");
            $(this).parent().addClass("ui-datepicker-current-day");
          }
        });
      }
    });
    

提交回复
热议问题