Using beforeShowDay (jquery.ui.datepicker) to display different employee schedule states.

by Admin 2. December 2008 10:06

Using the JQUERY calendar has been a great experience for us here at eSchedule so we've decided to share how we've accomplished some of our basic employee scheduling calendar features utilizing great open source projects like JQUERY. If you haven't heard of JQUERY then you should definitely check it out.

Let's assume we are displaying the jquery calendar in place with 2 months displayed:

First we need a holder element:

<div id="CalendarHolder"></div>

Then we can extend the div element with a JQUERY datepicker. The first thing to note below is that we've encapsulated our code inside $(document).ready(function(){});. We always wait until the page is ready before attaching our datepickers, safety first!

<SCRIPT LANGUAGE=javascript><!--

 

var activeDate = new Date('12/2/2008');

$(document).ready(function()

{$("#CalendarHolder").datepicker(

    {

        numberOfMonths: 2,

        showStatus:true,

        yearRange: '2000:2009',

        defaultDate: activeDate,

        firstDay: 0,

        inline: true,

        showOtherMonths: true,

        beforeShowDay: setScheduledDays

    });

 });

//--></SCRIPT>

A few things to point out here are the defaultDate and beforeShowDay properties. defaultDate is fairly self explanatory, and is how we set the selected date displayed on our calendars..

What we're really interested in is the
beforeShowDay property. Here we are instructing the datepicker to call this function setScheduledDays (below) immediately before showing each day on the calendar. This function simply returns [bool, 'CSS_CLASS']. The bool signifies whether the date is selectable, and the css class will set the css of our cell accordingly.

Below we initialize two arrays, scheduledDays and holidayDays. scheduledDays holds the date and css class for each day (only scheduled days have any sort of status), while holidayDays identifies which days of the year we can't schedule.

<SCRIPT LANGUAGE=javascript><!--

var scheduledDays = [[12, 2, 2008, 'PEND'], [12, 3, 2008, 'PEND'], [12, 4, 2008, 'PEND']]

var holidayDays = [[12, 25, 2008],[4, 12, 2009],[11, 26, 2009],[12, 25, 2009]];

function setScheduledDays(date) {

    var isScheduled = false;

    var isHoliday = false;

    var scheduleStatus = "";

    // Check for scheduled day

    for (i = 0; i < scheduledDays.length; i++) {

      if (date.getMonth() == scheduledDays[i][0] - 1 && date.getDate() == scheduledDays[i][1] && date.getFullYear() == scheduledDays[i][2])

      {

        isScheduled = true;

        scheduleStatus = scheduledDays[i][3];

      }

    }

    // Check for holidays

    if (holidayDays != null)

    {

        for (i = 0; i < holidayDays.length; i++) {

          if (date.getMonth() == holidayDays[i][0] - 1 && date.getDate() == holidayDays[i][1] && date.getFullYear() == holidayDays[i][2])

          {

            isHoliday = true;

          }

        }

    }   

    if (isHoliday)

        return [false, 'CLOSED'];

    else if (isScheduled)

        return [true, scheduleStatus];

    else

      return [true, ''];

} 

//--></SCRIPT>

Currently rated 5.0 by 9 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , , ,

Technology

Comments

12/13/2008 11:20:23 PM

Brook

What is PEND and CLOSED? Are those styles for css as you mentioned? If so how do I set those in CSS? Maybe like .PEND {color: #FF0000; } If you can send me an email and let me know how you changed the colore for the scheduled days. Thanks.

Brook us

12/15/2008 3:13:59 PM

Admin

Hi Brook,

Yes PEND and CLOSED are some possible CSS name values. Your example of .PEND {color: #FF0000; } would be valid. Note in the scheduleDays declaration below:

var scheduledDays = [[12, 2, 2008, 'APPR'], [12, 3, 2008, 'APPR'], [12, 4, 2008, 'PEND']];

December 2nd and 3rd would have class name 'APPR', while December 4th would have the class name 'PEND'.

Hope this helps Smile

Admin ca

1/10/2009 4:32:40 AM

Busby SEO Test

nice post, thank you for sharing :)

Busby SEO Test us

1/18/2009 4:25:14 PM

Kai

works like a charm! thanks for sharing! greetings from germany!

Kai de

4/4/2009 11:17:32 AM

jammer

great post
love it

jammer am

4/4/2009 11:18:02 AM

movers

move it on baby
move it right now
we love it

movers sv

4/27/2009 12:31:10 PM

kampanye damai pemilu indonesia 2009

great blog, i really like it.

kampanye damai pemilu indonesia 2009 us

4/27/2009 4:44:09 PM

Denver Movers

lovely thing to do
made my day

Denver Movers by

4/27/2009 4:45:11 PM

Dallas Movers

made my day
love yours sir
so funny
so happy

Dallas Movers ge

5/4/2009 4:59:00 PM

sulumits retsambew

what a great code, this is what i am looking for, thanks.

sulumits retsambew us

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.4.0.0
Theme by Mads Kristensen