Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How do I invoke a function using a button in coldfusion?

Posted on 2009-07-12
6
Medium Priority
?
330 Views
Last Modified: 2013-12-24
I have the following function which I need to invoke using a "download" button. The download button is located just below a cfgrid. The function will output an Excel file when supplied with data.

1. How do I invoke the function when a user clicks the download button?
2. How do I supply the full dataset and not just what is available in the current page of the cfgrid?
<cffunction name="generateExcel" output="Yes" access="remote">
		<cfargument name="theQuery" type="query">
		<cfargument name="columnList" type="string" default="">
		<cfargument name="mode" type="string" default="display">
		<cfargument name="theFilename" type="string" default="clients_admin.xls">
 
		<cfset var TAB = chr(9)>
		<cfset var i = 1>
		<cfset var headerArray = ArrayNew(1)>
 
      <cfquery name="getClientsAdmin" datasource="#THIS.dsn#">
	  		SELECT *
			FROM    Clients
        </cfquery>
 
 
        <cfquery name="getClients" datasource="#THIS.dsn#">
			SELECT *
			FROM Clients WHERE owner = <cfqueryparam value="#SESSION.MM_Username#" cfsqltype="cf_sql_clob" maxlength="10"> 
        </cfquery>
		
		
		<cfsetting enablecfoutputonly="Yes">
		
		<!--- Dump all content before this fn call --->
		<cfcontent reset="yes">
		
		<!--- Use the full column list, if none is provided --->
		<cfif Len( ARGUMENTS.columnList ) IS 0>
			<cfset ARGUMENTS.columnList = ARGUMENTS.theQuery.columnList>
		</cfif>
		
		<cfswitch expression="#ARGUMENTS.mode#">
			<cfcase value="display">
				<cfif NOT isdefined("REQUEST.headerDoneFlag")>
					<cfset REQUEST.headerDoneFlag = true>
					<CFHEADER NAME="Content-Disposition" VALUE="inline; filename=""#ARGUMENTS.theFilename#""">
					<CFCONTENT TYPE="application/msexcel">
				</cfif>
			</cfcase>
			<cfcase value="save">
				<cfset guid = CreateUUID()>
				<cfset filename = GetDirectoryFromPath(GetCurrentTemplatePath()) & "temp_" & guid & ".xls">
				<cfset REQUEST.headerDoneFlag = true>
				<cfheader name="Content-type" value="application/octet-stream"> 
				<cfheader name="Content-Disposition" value="attachment;filename=""#ARGUMENTS.theFilename#""">
				<cfsavecontent variable="output">
					<cfset generateExcel( ARGUMENTS.theQuery, ARGUMENTS.columnList, "save" )>
				</cfsavecontent><cfoutput>#output#</cfoutput><cfabort>
				<cffile action="WRITE" file="#filename#" output="#output#">
				<cfcontent type="application/msexcel" file="#filename#" deleteFile="Yes">
			</cfcase>
			<cfcase value="debug">
				<cfoutput><pre></cfoutput>
			</cfcase>
			<cfdefaultcase>
				<cfthrow message="Mode must be either 'display', 'save', or 'debug'.">
			</cfdefaultcase>
		</cfswitch>
		
		<!--- Write the header row --->
		<cfloop index="i" from="1" to="#ListLen( columnList )#">
			<cfset item = Trim( ListGetAt( columnList, i ) )>
			<cfset headerArray[ ArrayLen( headerArray )+1 ] = item>
			<cfoutput>#item##TAB#</cfoutput>
		</cfloop>
		<cfoutput>#chr(13)#</cfoutput>
		
		<!--- Write all the sub rows --->
		<cfloop query="ARGUMENTS.theQuery">
		
			<cfloop index="i" from="1" to="#ArrayLen( headerArray )#">
				<cfset dbVal = Trim( ARGUMENTS.theQuery[headerArray[i]][CurrentRow] )>
				<cfif IsDate( dbVal )>
					<cfset dbVal = DateFormat( dbVal, "mm/dd/yyyy" )>
				<cfelse>
					<cfset dbVal = replaceList(dbVal,"#TAB#,#chr(10)#,#chr(13)#"," , , ")>
				</cfif>
				<cfoutput>#dbVal##TAB#</cfoutput>
			</cfloop>
			
			<cfoutput>#chr(13)#</cfoutput>
		
		</cfloop>
		
		<cfif ARGUMENTS.mode EQ "debug">
			<cfoutput></pre></cfoutput>
		</cfif>
		
		<cfsetting enablecfoutputonly="No">
	</cffunction>

Open in new window

0
Comment
Question by:futr_vision
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 13

Expert Comment

by:srikanthmadishetti
ID: 24834978
use cfjaxproxy onclick event.
0
 

Author Comment

by:futr_vision
ID: 24835010
Thanks. But my problem is that i am extremely new to ColdFusion and cfjaxproxy is greek to me.  I'll need a little more detail on how to accomplish this.
0
 
LVL 13

Expert Comment

by:srikanthmadishetti
ID: 24835017
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 

Author Comment

by:futr_vision
ID: 24835062
Hmmm. Doesn't look like what I am trying to do. All I really need is the code and the place to put it to hook this function up.
0
 

Author Comment

by:futr_vision
ID: 24835858
Looking at this a little further i don't think cfajaxproxy will work for me. My download link/button is outside of the grid and needs to return the entire recordset. To me this simple means creating a way of calling the function and passing query data to it for it to do its thing. I just don't know how to do it.
0
 

Accepted Solution

by:
futr_vision earned 0 total points
ID: 24836295
Here is where I am at. I have some code that invokes the function and a cfc with the function. The invoke is pasted below and the function is in my original message except it is surrounded by the cfcomponent tag.
<cfquery name="customerDownload" datasource="THIS.dsn">
	Select *
	From Customers
</cfquery>
 
<cfinvoke component="CFC.ExcelDownload" method="generateExcel">
	<cfinvokeargument name="theQuery" value="#customerDownload#">
</cfinvoke>

Open in new window

0

Featured Post

Optimum High-Definition Video Viewing and Control

The ATEN VM0404HA 4x4 4K HDMI Matrix Switch supports 4K resolutions of UHD (3840 x 2160) and DCI (4096 x 2160) with refresh rates of 30 Hz (4:4:4) and 60 Hz (4:2:0). It is ideal for applications where the routing of 4K digital signals is required.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
Lease-to-own eliminates the expenditure of hardware replacement and allows you to pay off the server over time. Usually, this is much cheaper than leasing servers. Think of lease-to-own as credit without interest.
The purpose of this video is to demonstrate how to insert an Iframe into WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Open Page or Post…
The purpose of this video is to demonstrate how to exclude a particular blog category from the main blog page. This is can be used when a category already has its own tab, or you simply want certain types of posts not to show up on the main blog. …

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question