1

I have created a jQuery datepicker that has some days coloured (weekends, holidays). The fact is that the decission to colour that days depend on a ajax call that returns which days of the month have to be coloured.

When I click to the button to change the month, the ajax call is done, it returns the days to be coloured, the dateplicker flickers and then it displays the month correctly. I would like to display the new month once the ajax call is done and not earlier. Is there any way to avoid the flicker? It seems to work correctly in Internet explorer, but it doesnt work in Firefox.

Image

EDIT

If you click in change month button, the datepicker calls beforeShowDay for each day in the month to render it. If you put a breakpoint in console.log line (the jsfiddle example is below), you will see that the datepicker is closed and that it isn't rendered until every day is drawed. In my case, I want to maintain the old month until the the new month is fully drawed.

In the example that you can se below, the drawing of the calendar is really fast, and the days of the month are rendered so quickly that it seems to be instant, but if you do some ajax calls that retrieve database data to decide the colour that will have each day, the month flickers. Unlike Internet explorer, that problem happens to me in Firefox.

How can I fix it?

Fiddle example:

http://jsfiddle.net/b6V3W/370/

beforeShowDay: tratarDiasEspeciales,

function tratarDiasEspeciales(date){
   ajax call to decide the colour of the cell

   if(condition){
      //colour the day with blue
      return[true,'blue']
   }else{
     //colour the day with red
     return[true,'red']
   }
}
2
  • can you create a jsfiddle with your code? Commented Jan 3, 2017 at 9:20
  • @AminurRashid code added.
    – Jaime
    Commented Jan 3, 2017 at 11:41

2 Answers 2

1

I've created another fiddle by modifying your's. Here I've bring your business logic part(ajax call to retrieve data from database to decide color) into onChangeMonthYear callback function.

$("#dater").datepicker('option', 'onChangeMonthYear', onChangeMonthYearCustom);

Here you can populate an array named renderedDates, which will contain all data for coloring dates and the important thing is that this callback function keep current rendered calendar UI while executing and this callback function is called before "beforeShowDay" callback function. I've set debug point while executing and make sure this is the case. So, now on "beforeShowDay" callback your code doesn't have to wait for ajax call to end, thus fulfill your requirement. Please check this solution and let me know if it's working as you expected.

2
  • I have tryed it, but it still flickers if you put a breakpoint in the first line of renderCallendarCallBack function. It first deletes all the html inside the datepickers body and then, when the new html is ready it inserts it.
    – Jaime
    Commented Jan 3, 2017 at 15:28
  • If you put breakpoint in renderCallendarCallBack function, then it'll obviously halt the UI rendering as thus cause flickering. As ajax calls will no longer reside into this callback function, you've to put breakpoint into onChangeMonthYearCustom callback function. Commented Jan 3, 2017 at 15:45
0

The solution to the problem was on the library called jquery-ui.custom-1.9.1.custom.js. In that library, there is the following line of code:

inst.dpDiv.empty().append(this._generateHTML(inst));

That line first deletes the datepicker's data (header with month and year data and days) and then appends the new month's html code with generatedHTML function.

To solve the problem I have divided the code in two parts; first I get the new html code, and then I empty the datepicker´s data to append the new html code:

var quitar = this._generateHTML(inst);
inst.dpDiv.empty().append(quitar);

With this solution the ajax calls are made first and the calendar stays inmutable (it doesn´t flicker), then when the new code is ready it is inserted.

I know that it is not the best way to solve the problem (if I change the jquery version I will have to change the code there too) but is the only way I have found.

5
  • did you check my new comment on my answer? It should simulate the same thing as your answer (ajax calls are made before the rendering begins and thus prevent the flickering) Commented Jan 4, 2017 at 2:59
  • Yes, I have checked it and it makes sense. It is a right answer, but the way my application is made makes difficult to change the code from one site to another (it is a huge method and I haven´t programmed it). In order to save time, I have decided to use my own solution, but I don´t discard to use yours in a future. Thank you for answering and for your time ;)
    – Jaime
    Commented Jan 4, 2017 at 16:11
  • i'm having same problem but @Jaime, your code it's not making any effect for me and it keeps rerendering the calendar on each ajax call.
    – ohmmho
    Commented May 8, 2018 at 8:42
  • Is it the same jquery version (jquery-ui.custom-1.9.1.custom.js.)? Mine is still working and it is 1 year and a half since I made the cange of the code...
    – Jaime
    Commented May 8, 2018 at 15:42
  • @Jaime yeah! it's same jquery-ui.custom-1.9.1.js version as yours, but it seems it's not working for me. Anyway, thank you for your quick response! :)
    – ohmmho
    Commented May 8, 2018 at 17:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.