Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Don't understand cffunction and cfcomponent

Participant ,
Oct 19, 2010 Oct 19, 2010

I am trying to alter Forta's ajax related select tutorial to fit my needs.  I am having trouble understanding the details of this .cfc though.

<cfcomponent output="false">

    <!--- Get array of media types --->
    <cffunction name="get_states" access="remote" returnType="array">


        <cfset data="">
        <cfset result=ArrayNew(2)>
        <cfset i=0>


        <!--- Get data --->
        <cfquery name="data" datasource="mydsn">
        SELECT id,states
        FROM state
        </cfquery>


        <!--- Convert results to array --->
        <cfloop index="i" from="1" to="#data.RecordCount#">
            <cfset result[1]=data.id>
            <cfset result[2]=data.state>
        </cfloop>


        <cfreturn result>
    </cffunction>


    <!--- Get art by media type --->
    <cffunction name="get_waters" access="remote" returnType="array">

        <cfargument name="location" type="string" required="true" default="">


        <!--- Define variables --->
        <cfset var data="">
        <cfset var result=ArrayNew(2)>
        <cfset var i=0>


        <!--- Get data --->
        <cfquery name="data" datasource="mydsn">
        SELECT id,name
        FROM waters
        WHERE bow_state = '#arguments.location#'
        </cfquery>
  
        <!--- Convert results to array --->
        <cfloop index="i" from="1" to="#data.RecordCount#">
            <cfset result[1]=data.id>
            <cfset result[2]=data.name>
        </cfloop>


        <!--- And return it --->
        <cfreturn result>
    </cffunction>


</cfcomponent>

When I put '#arguments.id#' into the second query, I get results.  However, I'd like to query by the name instead, so I am trying location.  I don't fully understand so I am hacking around with this a bit but I'd like to 1 - get results by name instead of id and 2 - understand the arguments variable so I really know what is going on.

Thanks!

1.7K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Engaged , Oct 19, 2010 Oct 19, 2010

I agree with the other poster--more context (and code--like the selects to which these are bound) would be helpful.

However, you might start by using Firebug or the CF ajax debugging tools to see what's being passed into the second function.  If "arguments.id" is what was in the original example, then it's likely that the value of <cfset result[1]=data.id> from the first function is what's being populated into the first select's "value" for each option.  Then, when the option is selected, its val

...
Translate
Participant ,
Oct 19, 2010 Oct 19, 2010

Hi,

I think it'd help if you provided the context of the tutorial you're trying to modify, e.g. which one and where can people find that.

Cheers

Kai

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Oct 19, 2010 Oct 19, 2010

http://www.forta.com/blog/index.cfm/2007/5/31/ColdFusion-Ajax-Tutorial-2-Related-Selects

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Oct 19, 2010 Oct 19, 2010

Sorry, didn't see this before I posted.

But I think my suggestion was in keeping with the example.  If you need to select on "state", then make the "state" column of your first query to be the first value in the array that you return:

<!--- Get data --->
<cfquery name="data" datasource="mydsn">
     SELECT id,states
     FROM state
</cfquery>

<!--- Convert results to array --->
<cfloop index="i" from="1" to="#data.RecordCount#">
     <cfset result[1]=data.state>
     <cfset result[2]=data.state>
</cfloop>

This will make your select look something like this in the rendered HTML:

<select id="states" name="whatever...">

     <option value="Alabama">Alabama</option>

     <option value="Alaska">Alaska</option>

     ...

</select>

So then, on your second, dependent select, you'd specify the binding like so (just like you did, really):

<cfselect name="waters" bind="cfc:_ffc_cfc_select_jump.get_waters({states})" bindonload="false"></cfselect>

However, now, because the value of the "states" select options are state names, rather than ids, you should now be able to use the argument in your query based on state names:

<!--- Get art by media type --->
<cffunction name="get_waters" access="remote" returnType="array">

     <cfargument name="location" type="string" required="true" default="">

     <!--- Define variables --->

     <cfset var data="">
     <cfset var result=ArrayNew(2)>
     <cfset var i=0>

     <!--- Get data --->

     <cfquery name="data" datasource="mydsn">
        SELECT id,name
        FROM waters
        WHERE bow_state = <cfqueryparam value="#arguments.location#" cfsqltype="varchar" /> <!---Be sure to queryparam!!!--->
     </cfquery>
  
     <!--- Convert results to array --->
     <cfloop index="i" from="1" to="#data.RecordCount#">
          <cfset result[1]=data.id>
          <cfset result[2]=data.name>
     </cfloop>

     <!--- And return it --->
     <cfreturn result>
</cffunction>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Oct 19, 2010 Oct 19, 2010

That was just a missed s when I pasted. It actually is "states" in the code.

Like I said it works when I put arguments.id.

I tried to do what you suggested and put the states in the array's first position instead of the id. It didn't work.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Oct 19, 2010 Oct 19, 2010

Have you looked in Firebug or Chrome's Web Developer tools to see what values are being passed to the second remote function, and what--if any--data is being returned?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Oct 19, 2010 Oct 19, 2010

I take back what I said earlier... I put the state in both array positions and it worked.  Thank you.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Oct 19, 2010 Oct 19, 2010
LATEST

No problem--glad it worked out.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Oct 19, 2010 Oct 19, 2010

I agree with the other poster--more context (and code--like the selects to which these are bound) would be helpful.

However, you might start by using Firebug or the CF ajax debugging tools to see what's being passed into the second function.  If "arguments.id" is what was in the original example, then it's likely that the value of <cfset result[1]=data.id> from the first function is what's being populated into the first select's "value" for each option.  Then, when the option is selected, its value (in this case, data.id) is being passed as the argument to the second function.

If this is right, you could try making the "state" the first value in the array.  Without seeing the select bindings and what-not, however, it's hard to tell.

BTW, you might want to take a look at this:

<!--- Get data --->
<cfquery name="data" datasource="mydsn">
    SELECT id,states
    FROM state
</cfquery>


<!--- Convert results to array --->
<cfloop index="i" from="1" to="#data.RecordCount#">
    <cfset result[1]=data.id>
    <cfset result[2]=data.state>
</cfloop>

In your query, you select "id" and "states" (plural).  However, in your loop, you reference the "state" column (singular) from the query.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Oct 19, 2010 Oct 19, 2010

Here is the form.

Location: <cfselect  class="dropdown" multiple="no" name="states"  bind="cfc:_ffc_cfc_select_jump.get_states()" bindonload="true"/>

Water:  <cfselect class="dropdown" multiple="no" name="waters"  bind="cfc:_ffc_cfc_select_jump.get_waters({states})"  bindonload="false"></cfselect>

Here is the cfc

<cfcomponent output="false">

    <!--- Get array of media types --->
    <cffunction name="get_states" access="remote" returnType="array">


        <cfset data="">
        <cfset result=ArrayNew(2)>
        <cfset i=0>


        <!--- Get data --->
        <cfquery name="data" datasource="mydsn">
        SELECT id,states
        FROM state
        </cfquery>


        <!--- Convert results to array --->
        <cfloop index="i" from="1" to="#data.RecordCount#">
            <cfset result[1]=data.id>
            <cfset result[2]=data.state>
        </cfloop>


        <cfreturn result>
    </cffunction>


    <!--- Get art by media type --->
    <cffunction name="get_waters" access="remote" returnType="array">

        <cfargument name="location" type="string" required="true" default="">


        <!--- Define variables --->
        <cfset var data="">
        <cfset var result=ArrayNew(2)>
        <cfset var i=0>


        <!--- Get data --->
        <cfquery name="data" datasource="mydsn">
        SELECT id,name
        FROM waters
        WHERE bow_state = '#arguments.location#'
        </cfquery>
  
        <!--- Convert results to array --->
        <cfloop index="i" from="1" to="#data.RecordCount#">
            <cfset result[1]=data.id>
            <cfset result[2]=data.name>
        </cfloop>


        <!--- And return it --->
        <cfreturn result>
    </cffunction>


</cfcomponent>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources