| 1 | It's heavily based on [wiki:AJAXWidgetComboBox], but i've modified it to make it work with YUI autocomplete widget. |
| 2 | |
| 3 | == Requirements == |
| 4 | |
| 5 | * [http://developer.yahoo.com/yui/] - used for AJAX and widget implementation. |
| 6 | |
| 7 | == Installation == |
| 8 | |
| 9 | * Install Yahoo! UI in media/yui or media/js |
| 10 | |
| 11 | == Example Use == |
| 12 | |
| 13 | I'll use the same models that in [wiki:AJAXWidgetComboBox], except that we use "query=" instead of "q=" in search, so you can refer it. The urls.py looks exactly tike this last, but in view.py we have this: |
| 14 | |
| 15 | {{{ |
| 16 | def json_lookup(request, queryset, field, limit=5, login_required=False): |
| 17 | if login_required and not request.user.is_authenticated(): |
| 18 | return redirect_to_login(request.path) |
| 19 | obj_list = [] |
| 20 | lookup = { '%s__icontains' % field: request.GET['query'],} |
| 21 | for obj in queryset.filter(**lookup)[:limit]: |
| 22 | obj_list.append({"id": obj.id, "name": getattr(obj, field)}) |
| 23 | object = {"ResultSet": { "total": str(limit), "Result": obj_list } } |
| 24 | return HttpResponse(simplejson.dumps(object), mimetype='application/javascript') |
| 25 | }}} |
| 26 | |
| 27 | === Template === |
| 28 | |
| 29 | In template we must put the javascript. I've done it in header section with a event to fire it. So you can put the following code in your content, or in your header if you fire it with an event: |
| 30 | |
| 31 | {{{ |
| 32 | <input id="field" type="text"> |
| 33 | <div id="container"></div> |
| 34 | |
| 35 | <!-- Dependencies --> |
| 36 | <script type="text/javascript" src="media/js/yahoo.js"></script> |
| 37 | <script type="text/javascript" src="media/js/dom.js"></script> |
| 38 | <script type="text/javascript" src="media/js/event.js"></script> |
| 39 | <script type="text/javascript" src="media/js/connection.js"></script> |
| 40 | <script type="text/javascript" src="media/js/animation.js"></script> |
| 41 | <script type="text/javascript" src="media/js/json.js"></script> |
| 42 | <script type="text/javascript" src="media/js/autocomplete.js"></script> |
| 43 | |
| 44 | <script type="text/javascript"> |
| 45 | DataSource = new YAHOO.widget.DS_XHR("/myapp/reporter_lookup/", ["ResultSet.Result","name","id"]); |
| 46 | AutoComp = new YAHOO.widget.AutoComplete("field","container",DataSource); |
| 47 | </script> |
| 48 | }}} |
| 49 | |
| 50 | It's all!. You can customize [http://developer.yahoo.com/yui/docs/autocomplete/YAHOO.widget.AutoComplete.html AutoComp] and [http://developer.yahoo.com/yui/docs/autocomplete/YAHOO.widget.DataSource.html DataSource] all you need with [http://developer.yahoo.com/yui/docs/autocomplete/ extra params]. |