problem with parsing json in jquery autocomplete -
i'm having trouble in parsing json data in jquery autocomplete. json comes code:
<cfset theq = lcase(q)> <cfquery datasource="#source#" name="qry" maxrows="20"> select top 10 lastname info lower(lastname) '#theq#%' order lastname </cfquery> <!--- before can serialize query, need convert array of structs. ---> <cfset rows = [] /> <!--- loop on query convert it. ---> <cfloop query="qry"> <!--- create row struct. ---> <cfset row = {} /> <!--- add each column our struct. ---> <cfloop index="column" list="#qry.columnlist#" delimiters=","> <cfset row[ column ] = qry[ column ][ qry.currentrow ] /> </cfloop> <!--- append row struct row array. ---> <cfset arrayappend( rows, row ) /> </cfloop> <!--- have converted our query array of structs, can serialize using serializejson() method. ---> <cfset serializedquery = serializejson( rows ) /> <cfoutput>#serializedquery#</cfoutput>
i converted query array of structures , serialized json. now, parsing json in jquery autocomplete not work. data looks this:
[{"lastname":"abc"},{"lastname":"def"},{"lastname":"ghi"}]
here code autocomplete:
$(document).ready(function() { $("#name").autocomplete("data/name.cfm",{ minchars:1, delay:10, autofill:false, matchsubset:false, matchcontains:1, cachelength:10, selectonly:1, datatype: 'json', parse: function(data) { var parsed = []; var dataparsed = $.parsejson(data); (var = 0; < dataparsed.length; i++) { parsed[parsed.length] = { data: dataparsed[i], value: dataparsed[i].lastname, result: dataparsed[i].lastname }; } return parsed; }, formatitem: function(item) { return item; } });
when type in text field, whole json string search result. i've looked other codes parsing still can't working. help? thanks.
references: simon whatley autocomplete; ben nadel query array of structs , serialize json
one problem in loop iterating data.length
when should iterating dataparsed.length
if using simon whatley autocomplete plug-in, according demo on website, plugin expected autocomplete results on individual lines, e.g:
abc def ghi
so should rid of parse
function provide in jquery, , modify coldfusion script return 1 name per line i.e. abc\ndef\n...
no need use json or else. (at least thats how demo on website works http://www.simonwhatley.co.uk/examples/autocomplete/jquery/data/country.php)
just confirm, should add alert('aaa')
inside of parse()
function, bet see never being called.
Comments
Post a Comment