I am trying to create a custom tag to simplify the repeative code that I am now
using. The problem I am facing is that to get the code to work I have to
change the names of the cfsavecontent variables every time I call on the custom
tag. This needs to be done because I will have to call the custom tag a few
times on one page. I was wondering if anybody had some ideas on how to
implement this code to work for multiple instances on one page? Any help would
be great even if you tell me it can't be done.
Thanks,
Jeff
.
Here is my custom tag file................................
<!---
Usage:
This tag creates a Datefield with a companion select box to enter dates
easier:
Startnamename: This is the name of the first datefield.
Endname: This is the name of the second datefield.
StartLabel: This is the label of the first datefield.
EndLabel: This is the label of the second datefield.
textboxsize: This determines the datefield sizing
selectsize: This determines the select box sizing
spacing: This determines the spacing between the datefield and select box
StartYear: This tells the select box which year to start displaying dates
--->
<cfparam name="attributes.Startname" type="variableName">
<cfparam name="attributes.Endname" type="variableName">
<cfparam name="attributes.RangeNum" type="variableName">
<cfparam name="attributes.StartLabel" type="string" default="After Date:">
<cfparam name="attributes.EndLabel" type="string" default="Before Date:">
<cfparam name="attributes.textboxsize" type="numeric" default="120">
<cfparam name="attributes.selectsize" type="numeric" default="80">
<cfparam name="attributes.spacing" type="numeric" default="5">
<cfparam name="attributes.StartYear" type="numeric" default="2000">
<cfparam name="form.#attributes.Startname#" default="">
<cfparam name="form.#attributes.Endname#" default="">
<cfsavecontent variable="#attributes.RangeNum#">
<cfoutput>
var dtRangeStart = #attributes.Startname#.selectedDate;
#attributes.Endname#.selectableRange = {rangeStart: dtRangeStart};
#attributes.Endname#.selectedDate = null;
</cfoutput>
</cfsavecontent>
<cfsavecontent variable="StartYear1">
<cfoutput>
#attributes.Startname#.selectedDate = null;
#attributes.Startname#.displayedMonth = Number(Date().getMonth());
#attributes.Startname#.displayedYear =
Number(#attributes.Startname#_StartYear.selectedItem.data);
</cfoutput>
</cfsavecontent>
<cfsavecontent variable="EndYear2">
<cfoutput>
#attributes.Endname#.selectedDate = null;
#attributes.Endname#.displayedMonth = Number(Date().getMonth());
#attributes.Endname#.displayedYear =
Number(#attributes.Endname#_EndYear.selectedItem.data);
</cfoutput>
</cfsavecontent>
<!---<cfformitem type="script">
<cfoutput>
function #attributes.RangeNum#_SYear(){
#attributes.Endname#.selectedDate = null;
#attributes.Endname#.displayedMonth = Number(Date().getMonth());
#attributes.Endname#.displayedYear =
Number(#attributes.Startname#_EndYear.selectedItem.data);
}
</cfoutput>
</cfformitem>--->
<cfset thisyear = dateFormat(Now(),'YYYY')>
<cfformgroup type="horizontal" visible="yes" enabled="yes"
style="marginLeft:13;" >
<cfinput type="datefield" name="#attributes.Startname#"
width="#attributes.textboxsize#" label="#attributes.StartLabel#"
bind="form.#attributes.Startname#" onchange="##attributes.RangeNum##">
<cfformitem type="spacer" height="#attributes.spacing#"/>
<cfselect name="#attributes.Startname#_StartYear" width="80"
onchange="#StartYear1#"
onMouseDown="#attributes.Startname#_StartYear.dropdownWidth = 90;">
<cfoutput>
<option value="">Pick Year</option>
<cfloop index="i" from="#thisyear#" to="#attributes.StartYear#" step="-1" >
<option value="#i#">#i#</option>
</cfloop>
</cfoutput>
</cfselect>
</cfformgroup>
..................................................................Here is the
functioning stand alone code......................................
<cfsavecontent variable="setStartYear3">
AfterDate.selectedDate = null;
AfterDate.displayedMonth = Number(Date().getMonth());
AfterDate.displayedYear = Number(selectYear3.selectedItem.data);
</cfsavecontent>
<cfsavecontent variable="setRangeStart2">
var dtRangeStart = AfterDate.selectedDate;
BeforeDate.selectableRange = {rangeStart: dtRangeStart};
BeforeDate.selectedDate = null;
</cfsavecontent>
<cfformgroup type="horizontal" visible="yes" enabled="yes"
style="marginLeft:10;" >
<cfinput type="datefield" name="AfterDate" width="120" label="After
Date:" bind="#form.AfterDate#" onChange="#setRangeStart2#">
<cfselect name="selectYear3" width="80" label=""
onchange="#setStartYear3#" onMouseDown="selectYear3.dropdownWidth = 90;">
<cfoutput>
<option value="">Pick Year</option>
<cfloop index="i" from="#thisyear#" to="2000" step="-1" >
<option value="#i#">#i#</option>
</cfloop>
</cfoutput>
</cfselect>
</cfformgroup>
<cfsavecontent variable="setStartYear4">
BeforeDate.selectedDate = null;
BeforeDate.displayedMonth = Number(Date().getMonth());
BeforeDate.displayedYear = Number(selectYear4.selectedItem.data);
</cfsavecontent>
<cfformgroup type="horizontal" visible="yes" enabled="yes"
style="marginLeft:-2;" >
<cfinput type="datefield" name="BeforeDate" width="120" label="Before
Date:" bind="#form.BeforeDate#">
<cfselect name="selectYear4" width="80" label=""
onchange="#setStartYear4#" onMouseDown="selectYear4.dropdownWidth = 90;">
<cfoutput>
<option value="">Pick Year</option>
<cfloop index="i" from="#thisyear#" to="2000" step="-1" >
<option value="#i#">#i#</option>
</cfloop>
</cfoutput>
</cfselect>
</cfformgroup>
Dan Bracuk - 26 Jul 2006 14:17 GMT
I only looked at the part of your code that is visible without horizontal
scrolling, but I didn't see anything where you are passing information back to
the calling page. In this specific custom tag, you want to pass back variable
names. One of them is Attributes.RangeNum. The syntax to do that is:
<cfset rslt=evaluate("caller.#attributes.RangeNum# = attributes.RangeNum")>
jedale - 27 Jul 2006 02:14 GMT
Dan~
Just to let you know, this is the first custom tag I have tried to do, so I
don't know all the tricks. I don't understand why I would need to pass back a
variable if the cfsavecontent is in the custom tag. The first part of my code
where I reference #attributes.RangeNum# in the cfsavecontent is called by an
onChange event for the first Datefield. The problem is that when I put this
tag on a page multiple times I have duplicate cfsavecontent tags and I don't
know how I can change the variable names each time. The first part of the code
I posted above is an attempt to do this, but I had no luck. The second part of
the code above shows you a working example of what I want to make a the custom
tag to do. Is this even possibe? Should I put the cfsavecontent on the same
page as the custom tag is called on and manipulate it that way?
Any help would be awesome!!
Thank you for your time.
Jeff
Dan Bracuk - 28 Jul 2006 04:59 GMT
The reason you have to pass information back to the calling page is that custom
tags are bits of reuseable code that are available to any cf template. These
templates don't necessarily have to be written by the same person, or have the
same variable names.
The same concept is true with User Defined Functions, either inside or outside
components. It's what makes the code so re-useable.
jedale - 28 Jul 2006 21:45 GMT
Thanks, I understand now. I haven't needed to return a variable yet and now
after looking at the documentation it looks simple.
The only other question that I have is changing the <cfsavecontent> tag's
variable name every time the custom tag is called. What I was thinking is that
I could pass the custom tag a "attributes.RangeNum" variable and this could be
used to change the cfsavecontent variable name. The only problem is that I
can't get it to work. Let me know if this is even possible.
Thanks for your help
Jeff
I want to change from a constant variable......................
<cfsavecontent variable="SetRange">
........Stuff.....
</cfsavecontent>
to a variable in the cfsavecontent tag that is dynamic............ (like this
but not)
<cfsavecontent variable="#attributes.RangeNum#">
........Stuff.....
</cfsavecontent>
It seems like coldfusion doesn't like you doing it this way. Is there a
better way of doing this?
Dan Bracuk - 29 Jul 2006 01:21 GMT
It looks ok as far as sending an attribute in to the custom tag. Of course there is no code I see that sends anything back to the calling page.
At what point do things go awry?