jQuery ui - datepicker prevent refresh onSelect
Asked Answered
C

5

10

I'm using jQuery ui Datepicker to display a yearly inline calendar full of "special dates" (with colors). enter image description here This is to allow users to batch special dates by selecting a range and some other details.

$('#calendar').datepicker({
  ...
  , onSelect: function (selectedDate, inst) {
      $('.date_pick').toggleClass('focused');
      if ($('.date_pick.end').hasClass('focused')) {
        $('.date_pick.end').val('');
      }
      # inst.preventDefault() ? <- not a function
      # inst.stopPropagation() ? <- not a function
      # return (false) ? <- calendar refreshes anyway
    }
  ...
});

I'm also using qtip to show the details on each date

My problem is when I click on the calendar, it reloads itself entirely, so I loose my qtips.

I'd prefer not to use live() with qtip because I don't like the behavior.

I'd also prefer that the calendar not refresh each time I click on it (but this does not seem possible anyway) but I would probably no longer be able to highlight my selection anymore.

Do you have a suggestion for my problems ?

Cohn answered 19/5, 2011 at 9:10 Comment(6)
Did you try Yozomiri's solution?Kaule
Can I see your code on fiddle? I'm trying to achieve almost same kind of functionality. Please if possible.Holub
I'm sorry it's an old project I don't access to the code anymore. Basically I used the beforeShowDay callback to apply the CSSCohn
It was combination of onSelect and beforeShowDay? Did you handled this way that onSelect each date apply a custom css or give it an array of dates and used beforeShowDay to apply a css class on them? I just need an idea. Thanks.Holub
No, the selection CSS is also conditioned in the beforeShowDay as the click was reloading the entire datepickerCohn
datepicker('refresh') - works for me, thank youLei
S
27

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");
      }
    });
  }
});
Sigismond answered 4/6, 2011 at 0:25 Comment(2)
What about 1 day... it saved many days! Thanks ! :) Does anyone know whether this will be set-able feature in future jQuery-UIs?Increasing
This answer is weight in gold! 1000 Points! Sit down! :-)Lorraine
B
1

setting inst.inline to false inside the onselect won't work. instead try something like

onSelect: function() {
    $(this).data('datepicker').inline = true;                               
},
onClose: function() {
    $(this).data('datepicker').inline = false;
}
Bayou answered 14/6, 2012 at 21:0 Comment(0)
U
0

I have almost the same problem, like some other people, I have some kind of a solution.... but it's not fair:

$('#calendar').datepicker({
...,
onSelect: function (selectedDate, inst) 
{
  myFunction(selectedDate, inst);
}
});
function myFunction(selectedDate, inst)
{

  $('.date_pick').toggleClass('focused');
  if ($('.date_pick.end').hasClass('focused')) {
    $('.date_pick.end').val('');
  }
  inst.preventDefault(); # aa; works too, but writing aa; is going too far xD
}

It is not perfect, but works... I'll try to make it works just fine, till then...

EDIT: Solved adding:

<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
Utopian answered 26/5, 2011 at 9:33 Comment(1)
inst.preventDefault() <- not a functionCohn
L
0

If you just want to select a single day then you have to specify the Month and the Year in JQuery:

$(".ui-datepicker-calendar TBODY [data-month='"+inst.selectedMonth+"'][data-year='"+inst.selectedYear+"'] A").each(function(){
Lorsung answered 21/11, 2014 at 12:52 Comment(0)
C
0

In the case of having some datepickers on the page Yozomiri example will fail. You should do:

        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.
            $(this).find(".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.
            $(this).find(".ui-datepicker-calendar TBODY td").each(function(){
              if ( $(this).find('a').text() == inst.selectedDay && $(this).data('month') == inst.selectedMonth ) {
                $(this).find('a').addClass("ui-state-active");
                $(this).addClass("ui-datepicker-current-day");
              }
            });
        }

https://jsfiddle.net/g2bgbdne/3/

Chandigarh answered 15/12, 2015 at 11:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.