Kerrie, don't feel alone. I've noticed this in a few other jQuery plugins. The author will give you an example of the JSON they want, but they don't describe the JSON in pure data forms. So for example, if the JSON string is an array of strings, they don't say that. They just show it and assume you know that is how arrays are represented in JSON. JSON may be easy, but I definitely can't parse it in my head quite yet. Lets take a look at what the plugin wants:A couple of weeks ago, I read a post you wrote on jQuery and form validation... really peaked my interest so I've been taking a look at not only the validation plugin, but many of the other great jQuery plugins... I found this one last night, and its perfect for an app I'm working on, but I cannot figure out how to return the output of a query to populate the list. In the demo they are returning the results of tvshows.php. I noticed a number of other folks were having the same problem but no solution. Might you have a few spare moments to take a look??
Ok, so what do you do here? Obviously this is a pattern where every result is {"id":"X", "name":"Y"}, wrapped in [ and ]. So I took a guess here and thought maybe they wanted an array of structs. There is an easy way to test this - just write a quick test script:[{"id":"856","name":"House"}, {"id":"1035","name":"Desperate Housewives"}, {"id":"1048","name":"Dollhouse"}, {"id":"1113","name":"Full House"} ]
a, just an array: #serializeJSON(a)#
c, an array with a struct: #serializeJSON(c)#
Perfect! So now I know - I need to convert a query into an array of structs. Easy enough, right? Here is the first CFC method I came up with:s, just a struct: {}
a, just an array: []
c, an array with a struct: [{}]
2 <cfargument name="q" type="string" required="true">
3 <cfset var entrylookup = "">
4 <cfset var r = []>
5 <cfset var s = {}>
6
7 <cfquery name="entrylookup" datasource="blogdev">
8 select id, title
9 from tblblogentries
10 where title like <cfqueryparam cfsqltype="cf_sql_varchar" value="%#arguments.q#%">
11 </cfquery>
12
13 <cfloop query="entrylookup">
14 <cfset s = {id=id, name=title}>
15 <cfset r[arrayLen(r)+1] = s>
16 </cfloop>
17
18 <cfreturn r>
19
20 </cffunction>
2
3 <head>
4 <script src="/jquery/jquery.js"></script>
5 <script src="/jquery/jquery.tokeninput.js"></script>
6 <link rel="stylesheet" href="/jquery/tokeninput/token-input.css" type="text/css" />
7 <script>
8 $(document).ready(function() {
9
10 $("#name").tokenInput("data.cfc?method=getNames&returnFormat=json", {
11 hintText: "Type in the name of a blog entry.",
12 noResultsText: "No results",
13 searchingText: "Searching..."
14 })
15
16 })
17
18 </script>
19 </head>
20
21 <body>
22
23 <form>
24 <input type="text" name="name" id="name">
25 </form>
26
27 </body>
28 </html>
See the case of the ID and NAME values? They are both upper case. I changed my code to use the more verbose struct creation and set the keys so that my case was maintained:
2 <cfargument name="q" type="string" required="true">
3 <cfset var entrylookup = "">
4 <cfset var r = []>
5 <cfset var s = {}>
6
7 <cfquery name="entrylookup" datasource="blogdev">
8 select id, title
9 from tblblogentries
10 where title like <cfqueryparam cfsqltype="cf_sql_varchar" value="%#arguments.q#%">
11 </cfquery>
12
13 <cfloop query="entrylookup">
14 <cfset s["id"] = id>
15 <cfset s["name"] = title>
16 <cfset arrayAppend(r, s)>
17 </cfloop>
18
19 <cfreturn r>
20
21 </cffunction>

Comment 1 written by Phillip Senn on 3 July 2009, at 8:01 AM
Comment 2 written by Raymond Camden on 3 July 2009, at 8:06 AM
Comment 3 written by Kerrie Whelan on 6 July 2009, at 7:22 AM
Comment 4 written by Kerrie Whelan on 6 July 2009, at 3:45 PM
<cfloop query="entrylookup">
<cfset s = structNew()>
<cfset s["id"] = id>
<cfset s["name"] = title>
<cfset arrayAppend(r, s)>
</cfloop>
Thanks again Ray :-)
Comment 5 written by Raymond Camden on 6 July 2009, at 3:46 PM
Comment 6 written by Kerrie Whelan on 7 July 2009, at 9:00 AM
array
1 struct
id 343618
name Repeated
2 struct
id 343618
name Kelly Repeated
3 struct
id 343618
name Kelly Repeated
Comment 7 written by Raymond Camden on 7 July 2009, at 2:15 PM
Comment 8 written by Chris Hampton on 23 July 2009, at 4:33 PM
Comment 9 written by Matt Drake on 5 August 2009, at 10:24 PM
I do have to agree with Kerrie above though - if I didn't put structNew() inside of the loop then the one data point was just repeated for each row in the query as opposed to each row's values. I am using CF8 as well. (I was only using the last piece of code you had so I don't have the <cfset r[arrayLen(r)+1] = s> in there).
So the code that worked for me is:
<cfloop query="GetStates">
<cfset gsPoint = structNew()>
<cfset gsPoint["id"] = StateID>
<cfset gsPoint["abbrev"] = StateAbbrev>
<cfset gsPoint["name"] = StateName>
<cfset arrayAppend(gsQry, gsPoint)>
</cfloop>
Thanks!
[Add Comment] [Subscribe to Comments]