[jQuery] Re: Autocomplete problems with multiple fields

2009-04-17 Thread Tom Worster

On 4/16/09 2:50 PM, Lance A. Brown la...@bearcircle.net wrote:

 I'm working on adding autocomplete to a form which describes a
 discussion paper.  Each paper can have multiple authors and I want to
 provide autocomplete on the author names.  I'm new to jQuery so this
 code is kind of rough.
 
 My java script is:
 
 // Begin
 function parse_search(data) {
   var result = [];
   for (var i = 0; i  data.names.length; i++) {
 result[i] = { data: data.names[i].name,
   value: data.names[i].id,
   result: data.names[i].name
 };
   }
   return result;
 }
 
 function  format_item(row, i, max) {
   return row;
 }
 
 function set_author_autocomplete()
 {
   var searchurl = $(#authsearch_url).attr(href);
 
   $(.author_name).each(function (i) {
$(this).autocomplete(searchurl, {
 dataType: 'json',
 parse: parse_search,

what autocomplete plugin are you using? this one does not have a documented
parse option: 
http://docs.jquery.com/Plugins/Autocomplete/autocomplete#url_or_dataoptions


 formatItem: format_item
 }
 )
  }
  );
 }
 
 
 
 $(document).ready(set_author_autocomplete);
 //End

two more points, though i don't know if they make any odds:

it seems to be more conventional jq style (see
http://docs.jquery.com/Tutorials:Getting_Started_with_jQuery) to put all
your jq script inside  a block like this:

 $(document).ready(function() {  });

and is there a reason to prefer:
  $(.author_name).each(function (i) { $(this).autocomplete(); });
over
  $(.author_name).autocomplete();
?




[jQuery] Re: Autocomplete problems with multiple fields

2009-04-17 Thread Lance A. Brown

Tom Worster wrote:
 
 what autocomplete plugin are you using? this one does not have a documented
 parse option: 
 http://docs.jquery.com/Plugins/Autocomplete/autocomplete#url_or_dataoptions

Its the standard autocomplete plugin available at
http://docs.jquery.com/Plugins/Autocomplete

I found the syntax for handling json data in it on another web page.

 
 two more points, though i don't know if they make any odds:
 
 it seems to be more conventional jq style (see
 http://docs.jquery.com/Tutorials:Getting_Started_with_jQuery) to put all
 your jq script inside  a block like this:
 
  $(document).ready(function() {  });

I'm going to need to be able to call the autocomplete setup function
again later so I pulled it into a separate function.

 and is there a reason to prefer:
   $(.author_name).each(function (i) { $(this).autocomplete(); });
 over
   $(.author_name).autocomplete();
 ?

This was me struggling to figure out what was wrong with my code, which
I did figure out this morning.  I've removed the call to 'each'.

And now for the solution to my problem.  This bit of js:

  var searchurl = $(#authsearch_url).attr(href);

extracts the url I'm giving to autocomplete from the web page, allowing
me to use my templating system to set that URL.  Idiot me failed to
include that html fragment in the 2nd page, where I edit a paper,
therefor causing autocomplete to fail because it had a bad url.

Once I realized my error it was the work of a moment to correct things
and get autocomplete working properly.

--[Lance]


-- 
 GPG Fingerprint: 409B A409 A38D 92BF 15D9 6EEE 9A82 F2AC 69AC 07B9
 CACert.org Assurer


[jQuery] Re: Autocomplete problems with multiple fields

2009-04-17 Thread Tom Worster

On 4/17/09 12:49 PM, Lance A. Brown la...@bearcircle.net wrote:

 
 Tom Worster wrote:
 
 what autocomplete plugin are you using? this one does not have a documented
 parse option: 
 http://docs.jquery.com/Plugins/Autocomplete/autocomplete#url_or_dataoptions
 
 Its the standard autocomplete plugin available at
 http://docs.jquery.com/Plugins/Autocomplete
 
 I found the syntax for handling json data in it on another web page.

standard warnings on using undocumented api features apply.




[jQuery] Re: Autocomplete problems with multiple fields

2009-04-17 Thread Lance A. Brown

Tom Worster wrote:
 On 4/17/09 12:49 PM, Lance A. Brown la...@bearcircle.net wrote:
 I found the syntax for handling json data in it on another web page.
 
 standard warnings on using undocumented api features apply.

It's undocumented in the autocomplete API but is a clearly documented
option in the jQuery.ajax options.  autocomplete simply passes it through.

--[Lance]

-- 
 GPG Fingerprint: 409B A409 A38D 92BF 15D9 6EEE 9A82 F2AC 69AC 07B9
 CACert.org Assurer