So I was working on a project for a client and she needed a calendar for booking appointments. However, she wanted the ability to disable dates to prevent double booking. Since the site was in Flash, I thought I would use the DatePicker component, but I was having a hard time figuring out how to do it dynamically so that she could update it. With the help of nunomira on flashkit.com, I was able to implement it very easily. The site used an access database, so I wrote a quick asp page that outputs the dates in XML and then Flash loads the variables into an array and disables the dates. I thought I would share, in case anyone else can use this.
Here is the actionscript. Just load this into the frame and name your datepicker “calendar”:
var dates_xml:XML = new XML();
dates_xml.ignoreWhite = true;
dates_xml.onLoad = function(ok:Boolean):Void
{
if (ok)
{
var temp_xml:Array = this.firstChild.childNodes;
var n:Number = temp_xml.length;
var dates:Array = new Array();// local variable now
for (var i = 0; i < n; i++)
{
var year:Number = parseInt(temp_xml[i].attributes.year, 10);
var month:Number = parseInt(temp_xml[i].attributes.month, 10);
var day:Number = parseInt(temp_xml[i].attributes.day, 10);
dates.push(new Date(year, month, day));
}
disableDates(dates);// call the function
}
};
dates_xml.load("dates.xml");
function disableDates(dates:Array)
{
var datesRanges:Array = new Array();
var n = dates.length;
for (var i = 0; i < n; i++)
{
var j = i % 2;
//if it's odd
if (j == 0)
{
datesRanges.push({rangeStart:dates[i], rangeEnd:dates[i + 1]});
}
}
calendar.disabledRanges = datesRanges;
}
Here is the asp code. Note that the input month names was the actual name, so I converted them into numbers for Flash to read:
<%
' -- show.asp --
' Generates a list of uploaded files
Response.Buffer = True
' Connection String
Dim connStr
connStr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _
Server.MapPath("dates.mdb")
' Recordset Object
Dim rs
Set rs = Server.CreateObject("ADODB.Recordset")
' opening connection
rs.Open "select * from dates " & _
"order by [ID] desc", connStr, 3, 4
response.ContentType="text/xml"
response.Write("<?xml version='1.0' encoding='ISO-8859-1'?>")
response.Write("<dates>")
While Not rs.EOF
'Setting up the start year
response.Write("<date year=""" & rs("start_year") & """")
'converting month names to numbers for Flash to read
If rs("start_month") = "Jan" Then
response.Write(" month=""0""")
ElseIf rs("start_month") = "Feb" Then
response.Write(" month=""1""")
ElseIf rs("start_month") = "Mar" Then
response.Write(" month=""2""")
ElseIf rs("start_month") = "Apr" Then
response.Write(" month=""3""")
ElseIf rs("start_month") = "May" Then
response.Write(" month=""4""")
ElseIf rs("start_month") = "Jun" Then
response.Write(" month=""5""")
ElseIf rs("start_month") = "Jul" Then
response.Write(" month=""6""")
ElseIf rs("start_month") = "Aug" Then
response.Write(" month=""7""")
ElseIf rs("start_month") = "Sept" Then
response.Write(" month=""8""")
ElseIf rs("start_month") = "Oct" Then
response.Write(" month=""9""")
ElseIf rs("start_month") = "Nov" Then
response.Write(" month=""10""")
ElseIf rs("start_month") = "Dec" Then
response.Write(" month=""11""")
End If
response.Write(" day=""" & rs("start_day") & """ />")
'setting up the end year
response.Write("<date year=""" & rs("end_year") & """")
'converting month names to numbers for Flash to read
If rs("end_month") = "Jan" Then
response.Write(" month=""0""")
ElseIf rs("end_month") = "Feb" Then
response.Write(" month=""1""")
ElseIf rs("end_month") = "Mar" Then
response.Write(" month=""2""")
ElseIf rs("end_month") = "Apr" Then
response.Write(" month=""3""")
ElseIf rs("end_month") = "May" Then
response.Write(" month=""4""")
ElseIf rs("end_month") = "Jun" Then
response.Write(" month=""5""")
ElseIf rs("end_month") = "Jul" Then
response.Write(" month=""6""")
ElseIf rs("end_month") = "Aug" Then
response.Write(" month=""7""")
ElseIf rs("end_month") = "Sept" Then
response.Write(" month=""8""")
ElseIf rs("end_month") = "Oct" Then
response.Write(" month=""9""")
ElseIf rs("end_month") = "Nov" Then
response.Write(" month=""10""")
ElseIf rs("end_month") = "Dec" Then
response.Write(" month=""11""")
End If
response.Write(" day=""" & rs("end_day") & """ />")
rs.MoveNext
Wend
rs.Close
Set rs = Nothing
response.Write("</dates>")
%>
You can also
download the fla and a static XML file if you want to see how it's done.
Cheers,
Hal