Monday, January 23, 2012

Create a calendar item returning week numbers

When you use the built-in jQuery UI datepicker in your APEX page, you choose from a number of different format masks...but something with "week" is not one of them. And guess what I needed for recent project...
But, the good news is, you can (rather easy) use the jQuery framework to tweak the result that's returned from the datepicker.
Just create a Dynamic Action that fires on load of the page, and add these line of Javascript:
//Show the week number as the first column
$( ".datepicker" ).datepicker( "option", "showWeek", true );
//Set Monday as the first day of the week
$( ".datepicker" ).datepicker( "option", "firstDay", 1 );
//Return yyyy-ww instead of the actual date
$(".datepicker").datepicker("option", "onSelect", 
      function(value, date) 
      { var week=$.datepicker.iso8601Week ( 
              new Date(date.selectedYear,
                       date.selectedMonth,
                       date.selectedDay));
        $(this).val(date.selectedYear+'-'+(week<10?'0':'')+week);
      }
);
That's it. Of course you have to replace the jQuery selector - $(".datepicker") - with the one that matches your selection.
You can also substitute the function that generates the week number with your own one. And, yes, you could turn this into a plug-in if you like... (maybe I will when I've got the time, but no guarantee).
Post a Comment