Wiki source code of AutoSuggestWidget

Version 14.1 by ElenaOanaTabaranu on 2009/08/04

Show last authors
1
2
3
4 1 AutoSuggest Widget
5
6 #startfloatingbox()
7 *Contents*
8 #toc ("2" "2" "")
9 #endfloatingbox()
10
11 #info("This is a Javascript widget bundled by default with the XWiki platform")
12
13
14 1.1 Usage
15
16 The suggest widget can be triggered when typing something in a text field. The suggested list can contain field values from a class defined in your wiki or any custom information you provide.
17
18 1.1.1 Suggest fields from a class defined in the wiki
19
20 Use information from a predefined class in your wiki (e.g. *XWiki.TagClass*, *XWiki.XWikiUsers*, etc.) or from a class defined by yourself.
21 For example, use *XWiki.TagClass* to suggest tags from the wiki tag cloud:
22
23 For example, it can suggest tags from the wiki tag cloud:
24 {image:suggest.png|document=DevGuide.AutoSuggestWidget}
25 {code}
26
27 $!xwiki.jsx.use("DevGuide.AutoSuggestWidgetExample")
28 <form method="post" action="#">
29 <label for="myinput">Type the name of a tag and test the suggest list:</label>
30 <input id="myinput" size="20" type="text" value=""/>
31 </form>
32
33 {code}
34
35 The *JavascriptExtension* object from the *DevGuide.AutoSuggestWidgetExample* page contains the Javascript code to enable the widget when focusing on the text field:
36
37 {code}
38 (function(){
39 document.observe('dom:loaded', function () {
40 if($('myinput')) {
41 Event.observe($('myinput'), "focus", function() {
42 new ajaxSuggest(this, {script: '$xwiki.getURL("${doc.space}.WebHome", "view")?xpage=suggest&classname=XWiki.TagClass&fieldname=tags&secCol=-&', varname: "input", seps: " ,|", offsety: 13});
43 });
44 }
45 }); // end of doc observe
46 })();
47 {code}
48
49 1.1.1 Suggest custom information
50
51 When the information you want to suggest is not available through a class field or you generate it using a custom query, you need to create a service (plain wiki page called with *xpage=plain* parameter in the url) that maps your results to the *xml* input accepted by the *autosuggest* class.
52 For example, you can build a list of suggestions that contains the wiki page names within a certain space:
53 {code}
54
55 $!xwiki.jsx.use("Tutorials.AjaxSuggestCustomExample")
56 <form method="post" action="#">
57 <label for="myinput">Type the name of an example page from the **DevGuide** space and test the suggest list:</label>
58 <input id="myinput" size="20" type="text" value=""/>
59 </form>
60
61 {code}
62
63 The *JavascriptExtension* object from the *DevGuide.AjaxSuggestCustomExample* page contains the Javascript code to enable the widget when focusing on the text field. Also, the *script* option uses the url for the results page.
64
65 {code}
66 (function(){
67 document.observe('dom:loaded', function () {
68 if($('myinput')) {
69 Event.observe($('myinput'), "focus", function() {
70 new ajaxSuggest(this, {script: "$xwiki.getURL("DevGuide.SuggestService", "view", "xpage=plain&spacename=Tutorials")&", varname: "input", seps: " ,|", offsety: 13});
71 });
72 }
73 }); // end of doc observe
74 })();
75 {code}
76 The service page uses a query to get all the pages from the space provided using *spacename* parameter in the url. The generated response must be a *xml* file that has *<results>* as a root node and *<rs>* as children.
77 {code}
78 ##
79 ## Service to generate the suggest list of files from a certain space.
80 ## @spacename
81 ## @input
82 ##
83 #set($input = $request.get("input").toLowerCase())
84 #set($spacename = $request.get("spacename"))
85 $response.setContentType("text/xml") ## generate a xml file
86 ## select pages
87 #if("$!input" == "")
88 #set($query = "select doc.name from XWikiDocument doc where doc.space='$spacename' and doc.name<>'WebHome' and doc.name<>'WebPreferences' order by doc.date desc")
89 #else
90 #set($query = "select doc.name from XWikiDocument doc where doc.space='$spacename' and doc.name<>'WebHome' and doc.name<>'WebPreferences' and lower(doc.name) like '%" + $input + "%' order by doc.date desc")
91 #end
92 #set($searchresults = $xwiki.search($query))
93 <results space="$spacename">
94 #foreach($result in $searchresults)
95 <rs id="1" info="">$result</rs>
96 #end
97 </results>
98 {code}
99 1.1 Javascript parameters for the *ajaxsuggest* class
100
101 {table}
102 Parameter|Details
103 minchars|The minimum number of characters after which to trigger the suggest. Default value is 1.
104 get|The HTTP method for the AJAX request.
105 varname| The name of the request parameter holding the input stub. Default value is *input*.
106 className| The CSS classname of the suggest list. Default value is *ajaxsuggest*.
107 timeout|Default value is *2500*.
108 delay|Default value is *500*.
109 offsety|Default value is *-5*.
110 shownoresults|Display a "no results" message, or simply hide the suggest box when no suggestions are available.
111 noresults|Default displayed message is *No results!*.
112 maxheight|Default value is *250*.
113 cache|Default value is *false*.
114 seps|Default value is "".
115 resultsParameter|The name of the JSON variable or XML element holding the results.Default value is *results* for the old suggest, *searchResults* for the REST search.
116 resultId|The name of the JSON parameter or XML attribute holding the result identifier. DEfault value is *id* for both the old suggest and the REST search.
117 resultValue|The name of the JSON parameter or XML attribute holding the result value.Default *value* for the old suggest, *pageFullName* for the REST page search.
118 resultInfo|The name of the JSON parameter or XML attribute holding the result auxiliary information. Default value is *info* for the old suggest, *pageFullName* for the REST search.
119 parentContainer|The id of the element that will hold the suggest element. Default value is *body*.
120 script|Url for the ajax request that will get the suggested list. Must end with *&* because *varname* parameter will be appended. Use *suggest.vm* to get field values from a wiki class or a custom url to generate the suggested list.
121 {table}
122
123 1.1.1 Options for the *script* parameter:
124
125 {table}
126 Option|Details
127 xpage|Must use *xpage=suggest* because suggest.vm template is used to manage the request.
128 classname|The name of the class for the elements of the suggest list.
129 fieldname|The field name from the class considered for the suggest list.
130 firCol|First column of the list of results.
131 secCol|Second column of the list of results. For a user defined query, use *-* value for one column and no hidden input. Otherwise the list of results will have two columns and a hidden input.
132 {table}
133
134
135 1.1 Live Example
136
137 * Check out the example above at [Ajax Suggest Example>DevGuide.AutoSuggestWidgetExample]
138
139 1.1 Tips
140
141 * Check out the Javascript code for more details: *{pre}http://localhost:8080/xwiki/resources/js/xwiki/suggest/ajaxSuggest.js {/pre}*.

Get Connected