| Page 1 of 2 | [ 15 posts ] | Go to page 1, 2 Next |
|
Hi,
I've been trying to implement AutoComplete using an external datasource. I'm using the following example as the basis for my implementation http://developer.yahoo.com/yui/examples/autocomplete/ac_fn_multfields_clean.html The thing I like about this example is that I can get the person's id that I'm after. Does anyone know how to tweak this example to call an external datasource, I've tried reading the documentation but frankly I find it utterly baffling. I've written the worker page using ASP and can change it to return any format necessary at the moment it returns a string formatted like the myContacts array but can't figure out how to hook it up. This is what I have so far http://pastie.org/1885318 Any help will be greatly appreciated. Regards Al |
|
Hi Al,
I took a quick scan over you pastie and it looks like you are simply trying to make a remote Autocomplete call. I would recommend you look at some of the "remote" examples and not necessarily the FunctionDataSource example. The "Basic Remote" is a good start http://developer.yahoo.com/yui/examples/autocomplete/ac_basic_xhr.html although it only uses a delimited text response. Your code uses a somewhat old-fashioned method for Ajax calls with the "XmlHttpObject" calls. Why not use YUI's built-in and fantastic Ajax support? Maybe you were a little lost in understanding how to use YUI async connections, it is pretty straightforward -- the rest of this post will give you some pointers. It looks like your ASP server 'PeopleAutoComplete.asp' returns TEXT data at the moment. To get the most utility I recommend you can change your server to return JSON or XML data. That way you can leverage the real power of YUI. If you want to use YUI's Ajax support it is available through DataSource. To use remote requests with DataSource in addition to the 'datasource.js' library you also need to load Code: <script type="text/javascript" src="http://yui.yahooapis.com/2.9.0/build/connection/connection-min.js"></script> This will provide the YUI's asynchronous connection support.Now for your ASP server, it would be most convenient for you to change it to return JSON data. The JSON data should be in the following format to be "legal"; Code: {"Results":[{"id":0,"fname":"Alice","lname":"Abrams","email":"ace@ppl.com"},{"id":1,"fname":"Abraham","lname":"Axelrod","email":"Abe@ppl.com"}, The formatting is very important for the JSON response, you can use http://jsonlint.com/ to check it's legitimacy.... etc to last row {"id":9, "fname":"Engleberg", "lname":"Humperdink", "emai":""l} ] } Also for your server, you would probably want to do the "searching" that the "matchNames" JS function of the example used on the server-side. For example you would simply do a string search of whatever is passed into the server (call it query_string) against the "lname", "fname" and "email" fields of your data. Then return the matching rows in their entirety back in the JSON object. I haven't done ASP in awhile so you are on your own for that! If you want "php" advice I can show you that method. Assuming your ASP server is working well, then on the YUI DataSource and AutoComplete side you would do the following Code: REPLACE your oDS Instantiation as: var oDS = YAHOO.util.DataSource('http://myurl.com/mts/test/centre_people_ajax/PeopleAutoComplete.asp'); oDS.responseType = YAHOO.util.DataSource.TYPE_JSON; // if your ASP server expects POST variables, uncomment the following ... otherwise if it uses GET variables leave this commented // use oDS.connMethodPost = true; oDS.responseSchema = { resultsList : "Results", // MUST match the string the defines the return array from the JSON object ... fields: [ 'id', 'fname', 'lname', 'email' ] }; ADD the following after oAC is instantiated; oAC.generateRequest = function( sQuery ) { return 'query_string=' + encodeURIComponent(sQuery); }; The generateRequest method will send off whatever whatever is typed in the autocomplete box as sQuery on every keystroke. We simply provide a URL parameter identified in this case as "query_string". Say the user enters "da" in autocomplete, the generateRequest method fires off a GET request to your server at the following URL Code: http://myurl.com/mts/test/centre_people_ajax/PeopleAutoComplete.asp?query_string=da and return the subset that matched "da" from your server logic. DataSource will properly construct this URL for you, and if you returned mixed data you could configure DataSource to parse dates, numbers, strings, etc.. The rest of the your code with formatting and highlighting functions in your pastie should work fine. I've probably made it seem more complicated than it is. I recommend using YUI for your ajax requests via Connection and DataSource, they are invaluable tools and allow caching, parsing of the data, updating, etc... Using a browser debugger (Firebug, Chrome developer tools, etc..) is VERY helpful, especially when dealing with Ajax requests because you can set breakpoints and inspect what was sent and returned. I may be able to put this working example publicly usable from my server if you need to see the whole thing. Regards, Todd |
|
Todd,
Thanks very much for your reply very appreciated! I've applied the tweaks you've suggested to the basic example but now my call isn't getting through to the datasource I've checked the json output on http://jsonlint.com/ and it's saying that it's valid. I'd be very grateful if you could take a look at my example page http://pastie.org/1888321 and see if you can tell what's wrong. Cheers Al |
|
Todd,
Please ignore my last post I've managed to figure it out added "new" to the datasource declaration - Code: var oDS = new YAHOO.util.DataSource('http://myurl.com/mts/test/centre_people_ajax/PeopleAutoComplete1.asp'); And added a "?" to the return part of generateRequest Code: oAC.generateRequest = function( sQuery ) {return '?q=' + encodeURIComponent(sQuery);}; I've now got the basic thing working except my AC results box contains the list of Ids where I want it to display - "[lname] [fname] ([email]) Here is some sample output data - Code: {"results": [ {"id":3,"fname":"Alex", "lname":"Conba", "email":"conba@somewhere.com"}, {"id":1,"fname":"Yona", "lname":"Concha", "email":"yconcha@somewhere.com"}, {"id":4,"fname":"fawkes", "lname":"condemines", "email":"fawkes.condemines@somewhere.com"}, {"id":9,"fname":"Richie", "lname":"Conditis", "email":"conditis@somewhere.com"}, {"id":20,"fname":"Masssive", "lname":"Conesess", "email":"conesess.m@somewhere.com"}, {"id":61,"fname":"Sally", "lname":"Conzenitec", "email":"sconzenitec@somewhere.com"} ] } My current source - http://pastie.org/1888794 I'd be very grateful if you could give me a hand getting it to display. Regards Al |
|
Hi Al,
Sounds like you almost have your example working. The AC widget automatically uses the first column of the DataSource as the displayed field, and since it is "id" that is why it is using that. You can override what is displayed in the AC field using the "formatResult" method (for an example see http://developer.yahoo.com/yui/examples/autocomplete/ac_formatting_proxyless.html). Overriding .formatResult gives you any field available in the DataSource as displayable. In my earlier post I intended for you to re-use the the existing "oAC.formatResult" and "highlightMatch" functions from your original posting. If you insert them as-is they should work fine as how you want them. Good Luck, Todd |
|
Todd,
Thanks, yes thanks to you I'm very nearly there, I tried putting my existing functions in but it not liking it so I thought I must be missing a setting somewhere? Latest code here http://pastie.org/1889128 Using firebug I've put a break point on line 71, when it breaks I put the mouse over "oResultData" it displays - Code: [1, "jack", "spratt", "jack.spratt@couldeatnofat.com"] When I mouse over .fname is displays - Code: undefined So it appears to me that it doesn't recognise the object? Allan |
|
Hi Al,
From the Firebug data you posted on oResultData it sounds like the JSON parse is failing. oResultData should be an object and what you posted means it is an array - therefore it is not being parsed properly. Sounds like it may be a DataSource error somewhere, not sure if from your server or in your YUI calls/library. The code sample I wrote yesterday for this case is available at http://blunderalong.com/ac_remote.html which is publicly available, that seems to work fine for me. My system does not allow the "?" in the generateRequest return, not sure why that is different for you. Maybe you can check over my source / Firebug it and see if you can find the difference? I don't know if I'll have time available today to check this, things are pretty busy here for me today. Thanks, Todd |
|
Alan,
Quick update ... I think the problem with the .formatResult is related to a setting. If right after you instantiate the oAC you put "oAC.resultTypeList = false;" I think it will work fine. Read through the AutoComplete user page and search for resultTypeList and you will see this. Todd |
|
Todd,
Ace! One last thing I'm only getting 10 records displaying, what's the best way of removing this restriction? Al |
|
You'll have to start reading the docs yourself, okay? http://developer.yahoo.com/yui/autocomplete/#maxresults. Gotta get back to work ...
|
| Page 1 of 2 | [ 15 posts ] | Go to page 1, 2 Next |
| You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum |
© 2006-2013 Yahoo! Inc. All rights reserved.
All code on this site is licensed under the BSD License unless stated otherwise.
About This Site · Security Contact Info
Powered by phpBB® Forum Software © phpBB Group