Coldfusion CFC Return JSON display in Jquery, how do I handle multiple records from CFC and display in Jquery?

11,171

Solution 1

You could just call the remote function and let ColdFusion serialize the query object to JSON? (notice returnFormat="JSON")

<cffunction name="getUsers" access="remote" returnType="query" returnFormat="JSON">
   <cfquery name="users" datasource="#thedb#">
       In this query I can get 1 record to 25 or even more results         
   </cfquery>
   <cfreturn users>
</cffunction>

JSON formatting will look like this:

{"COLUMNS":["NEWREQUESTER","NEWREQUESTERUSERNAME"],"DATA":[["1","JOHN DOE"],["2","JIM DOE"]]}

Solution 2

Store your users in an array and return that array.

<cfset var userArr = arrayNew(1)/>
<cfloop query="users">
    <cfset var user = structNew()/>
    <cfloop list="#users.columnlist#" index="column">
        <cfset user[column] = users[column]>
    </cfloop>
    <cfset ArrayAppend(userArr, user)/>
</cfloop>
<cfreturn userArr />

That creates an array of objects in javascript like this:

[
    {
        newrequestrr:"foobar",
        newrequestorusername:"foobar",
        ...
    },
    ...
]

Update: I made code a little more dynamic. I use a custom function in my udf library to make this conversion from query structure to array of rowstructs, I modified it to use your variables.

Share:
11,171
user1253239
Author by

user1253239

Updated on June 26, 2022

Comments

  • user1253239
    user1253239 almost 2 years

    I need a way to grab the json return from coldfusion and display it in jquery I can display the first result fine, but if more that one record comes back I am stuck Here is my cfc

    <cfquery name="users" datasource="#thedb#">
    In this query I can get 1 record to 25 or even more results         
    </cfquery>
    

    Here is my out put to jquery, not sure if this is a good way to do this, but.... this is how I handle mutliple records right now.

    <cfset var user = structNew()/>
    
    <cfset thenumber = 1>   
            <cfloop query="users"> 
            <cfset user["newrequestor#theNumber#"] = users.requestor/>
            <cfset user["newrequestorusername#theNumber#"] = users.requestor_username/>
            <cfset user["newrequestorphone#theNumber#"] = users.requestorphone/>
            <cfset user["newrequestoremail#theNumber#"] = users.requestoremail/>
            <cfset user["newthedate#theNumber#"] = users.thedate/>
            <cfset user["newapproved#theNumber#"] = users.approved/>
            <cfset user["newcomments#theNumber#"] = users.comments/>
            <cfset user["newviewed#theNumber#"] = users.viewed/>
            <cfset thenumber = thenumber + 1>
            </cfloop>
    
    <cfreturn user>
    

    End of CFC

    Here is my jquery, I have it manually set to grab first record... not sure how to loop over to get all records returned.

    Here I pass my arguments to the cfc to get my result. works great

    thedata = instance.getSearch($("#therequestor").val(), $("#fromDate").val(), $("#toDate").val(), $("#theapproved").val(), $("#theroom").val());
    

    Next I build a row in jquery to handle my first record, manual setting not dynamic.

    var new_Return = '<tr id="newReturn"><th style="text-align:left;" id="first">Request Date:</th><td>'+thedata.newthedate1+'</td><td>&nbsp&nbsp&nbsp&nbsp</td><th style="text-align:left;" id="first">Requestor:</th><td>'+thedata.newrequestor1+'</td><td>&nbsp&nbsp&nbsp&nbsp</td><th style="text-align:left;" id="first">Approved:</th><td>'+thedata.newapproved1+'</td><td>&nbsp&nbsp&nbsp&nbsp</td><th style="text-align:left;" id="first">Viewed:</th><td>'+thedata.newviewed1+'</td></tr>';
            $("#theReturnFormTable").append(new_Return)
    

    Displays first result in my div tag great, how can I loop over if I have multiple records Example thedata.newrequestor1 is my first record

    then I could have more many more

    thedata.newrequestor2 is my 2nd thedata.newrequestor3 is my 3rd etc. on and on

    How can I handle this in jquery, or do I have to start different in coldfusion cfc??

  • Mark A Kruger
    Mark A Kruger almost 12 years
    Good answer but overkill. A query already is an array. Just serialize that bad boy :)
  • Aidan Kane
    Aidan Kane almost 12 years
    Isn't it in a weird format though? A struct of arrays rather than an array of structs?
  • Kevin B
    Kevin B almost 12 years
    Yes, when you return it directly as a query, it is a structure of arrays rather than an array of structures. I prefer the array of structures.