views:

225

answers:

2

I would like to able to bind a series of 3 buttons to toggle 3 boolean values on a database message entry. The boolean database entries are read|unread, actioned|pending, referral|message and the message entry has the unique key "messageID". I want the buttons to display the record starting values (which I presume is bindonload="true").

I've limped towards

<cfform>
<cfinput type="hidden" name="switchName" value="read"> 
<cfinput type="button" bind="cfc:cfcs.messages.toggle({toggle@click},{switchName@none})" name="toggleRead" value="Read" bindonload="true">
</cfform>

and in the cfc

<cffunction access="remote" name="toggle" output="false" returntype="any" >
<cfargument required="true" name="toggle" type="any"/>
<cfargument required="true" name="switchName" type="any"/>
<cfif toggle eq "Read">
       <cfreturn "Unread">
<cfelseif toggle eq "Unread">
 <cfreturn "Read">
</cfif>
</cffunction>

This gets me some of the way there, in so far as it toggles the button label, but I'm foxed on how to pick up the initial db values to display the initial status.

Also is there a way to pass other variables in the bind statement without using hidden fields and control@none format, e.g. I will need to pass in the messageID so I can update the correct record. I wouldn't have put in the switchName input if I knew how to simply pass the switchName variable in a beter way.

Many thanks for any light you can shed?

+1  A: 

How about...

<cfinput type="button" bind="cfc:cfcs.messages.toggle({toggle@click}, #switchName#)" value="#initialValue#" bindonload="false">

or alternatively:

<cfajaxproxy bind="javascript:yourJSFunc({toggle@click})">

and in your yourJSFunc, use whatever JS var's you need.

Henry
A: 

That helped Henry.

I got it working, though it feels a bit kludgy...

<cfform>
<cfinput type="button" STYLE="width: 80px; height: 22px;" bind="cfc:messageProcess.togglebool('#application.dsn#', '#url.messageID#',{toggle1@click},'referral', 'Referral', 'Message')" name="toggle1" value="" bindonload="YES">
<cfinput type="button" STYLE="width: 80px; height: 22px;" bind="cfc:messageProcess.togglebool('#application.dsn#', '#url.messageID#',{toggle2@click},'viewed', 'Read', 'Unread')" name="toggle2" value="" bindonload="YES">
<cfinput type="button" STYLE="width: 80px; height: 22px;" bind="cfc:messageProcess.togglebool('#application.dsn#', '#url.messageID#',{toggle3@click},'actioned', 'Actioned', 'Pending')" name="toggle3" value="" bindonload="YES">
</cfform>

and the cfc

<cfcomponent>
 <cffunction access="remote" name="togglebool" output="true" returntype="any" displayname="Toggle boolean value in message record" hint="Toggles boolean value in message record">
  <cfargument required="true" name="dsn" type="string"/>
  <cfargument required="true" name="messageID" type="numeric"/>
  <cfargument required="true" name="buttonLabel" type="string"/>
  <cfargument required="true" name="switchName" type="string"/>
  <cfargument required="true" name="switchOnLabel" type="string"/>
  <cfargument required="true" name="switchOffLabel" type="string"/>
  <cfset var returnMessage = "" />
  <cfset var temp = "" />

  <cfquery datasource='#arguments.dsn#' name="getSwitchData">  
   SELECT #arguments.switchName#
   FROM messages
   WHERE messageID=<cfqueryparam value="#arguments.messageID#" cfsqltype="CF_SQL_INTEGER"/>
  </cfquery>

  <cfset temp="getswitchdata."&#switchName#>

  <cfif #Evaluate(temp)# is 1>
   <cfset returnMessage="#arguments.switchOnLabel#">
  <cfelse>
   <cfset returnMessage="#arguments.switchOffLabel#">
  </cfif>

   <cfif buttonLabel eq "">
    <cfreturn returnMessage>
   <cfelseif buttonLabel eq "#arguments.switchOffLabel#">
    <cfquery datasource='#arguments.dsn#'>  
    UPDATE messages
    SET #arguments.switchName#=1
    WHERE messageID=<cfqueryparam value="#arguments.messageID#" cfsqltype="CF_SQL_INTEGER"/>
    </cfquery>
    <cfreturn "#arguments.switchOnLabel#">
   <cfelseif buttonLabel eq "#arguments.switchOnLabel#">
    <cfquery datasource='#arguments.dsn#'>  
    UPDATE messages
    SET #arguments.switchName#=0
    WHERE messageID=<cfqueryparam value="#arguments.messageID#" cfsqltype="CF_SQL_INTEGER"/>
    </cfquery>
    <cfreturn "#arguments.switchOffLabel#">
   </cfif>
 </cffunction>

If there's a slicker way please let me know.

Saul