[TIP] Add a HTML calendar to your form!

Find out how to get things done with Servoy. Post how YOU get things done with Servoy

[TIP] Add a HTML calendar to your form!

Postby Karel Broer » Mon Aug 29, 2005 10:42 pm

Adding a HTML calendar to your form (or controller) difficult? No way!
It's possible with just 1 global method! No kidding? No kidding!
Here we go:

Code: Select all
var date = new Date();
         {
         //If no parameter is passed use the current date.
         if(date == null)
            date = new Date();
       
         day = date.getDate();
         month = date.getMonth();
         year = date.getFullYear();       
         months = new Array('january',
                            'february',
                            'march',
                            'april',
                            'may',
                            'june',
                            'july',
                            'august',
                            'september',
                            'october',
                            'november',
                            'december');
       
         this_month = new Date(year, month, 1);
         next_month = new Date(year, month + 1, 1);
       
         //Find out when this month starts and ends.       
         first_week_day = this_month.getDay();
         days_in_this_month = Math.floor((next_month.getTime() - this_month.getTime()) / (1000 * 60 * 60 * 24));
       
         calendar_html = '<html>' +
'<style type="text/css"><!--' +
'a { color: black; text-decoration: none }'+
'--></style>' +
                        '<body>' +
                        '<table border="0" cellspacing="1" cellpadding="0">' +
                        '<tr>' +
                        '<td bgcolor="ffffff">' +
                            '<table style="background-color:666699; color:ffffff;">';
       
         calendar_html += '<tr><td colspan="7" align="center" style="background-color:bfc7db; color:000000;">' +
                        '<font size="3" color="083767" face="Verdana"> <b>' +
                          months[month] + ' ' + year + '</b></td></tr>';
       
         calendar_html += '<tr>';
         
         //Fill the first week of the month with the appropriate number of blanks.     
         for(week_day = 0; week_day < first_week_day; week_day++)
            {
            calendar_html += '<td style="background-color:bfc7db; color:000000;">' +
            '<font size="1" face="Verdana"> </td>'; 
            }
           
         week_day = first_week_day;
         for(day_counter = 1; day_counter <= days_in_this_month; day_counter++)
            {
            week_day %= 7;
           
            if(week_day == 0)
               calendar_html += '</tr><tr>';
           
            //Do something different for the current day.
            if(day == day_counter) 
               calendar_html += '<td align="center" valign="middle" style="background-color:083767"><b>' + day_counter + '</b></td>';
            else
               calendar_html += '<td align="center" valign="middle" style="background-color:bfc7db; color:000000;"> ' +
                                day_counter + ' </td>';
           
            week_day++;
            }
           
         calendar_html += '</tr>';
         calendar_html += '</table></td></tr></table></body></html>';
       
         //Display the calendar.   
globals.gCalender = calendar_html;                 
         }


Ofcourse you can use i18n to internationalize monthnames and change the colours by using the colour editor in the Method Editor!

See? Life's sometimes easier that it looks like! :wink:
Karel Broer
ServoyCamp - http://www.servoycamp.com
User avatar
Karel Broer
 
Posts: 779
Joined: Mon May 03, 2004 12:49 am
Location: Doetinchem

Postby IT2Be » Tue Aug 30, 2005 12:31 am

GOOD work and nice tip. Thanks Karel.

BTW you can check the last date of a month with the tools plugin:

days_in_this_month = plugins.it2be_tools.getMaxDay(month, year);

Cheers
Marcel J.G. Trapman (IT2BE)
SAN partner - Freelance Java and Servoy
Servoy Components - IT2BE Plug-ins and Beans for Servoy
ServoyForge - Open Source Components for Servoy
User avatar
IT2Be
Servoy Expert
 
Posts: 4766
Joined: Tue Oct 14, 2003 7:09 pm
Location: Germany


Return to How To

Who is online

Users browsing this forum: No registered users and 9 guests