Wiki source code of AutoSuggest Widget

Version 46.1 by Oana Florea on 2011/03/03

Hide last authors
Jerome 35.1 1 ##
2 ## TODO:
3 ## - Document the REST usage of the suggest (this is the recommanded way of using the suggest now - when possible)
4 ## - Merge the example document in this one (why are there 2 documents ?)
5 ## - Migrate this page to syntax 2.0
6 ##
ElenaOanaTabaranu 1.1 7 #startfloatingbox()
8 *Contents*
9 #toc ("2" "2" "")
10 #endfloatingbox()
11
ElenaOanaTabaranu 21.1 12 #info("This is a Javascript widget bundled by default with the XWiki platform.")
ElenaOanaTabaranu 1.1 13
14
15 1.1 Usage
16
ElenaOanaTabaranu 14.1 17 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.
18
19 1.1.1 Suggest fields from a class defined in the wiki
20
21 Use information from a predefined class in your wiki (e.g. *XWiki.TagClass*, *XWiki.XWikiUsers*, etc.) or from a class defined by yourself.
22 For example, use *XWiki.TagClass* to suggest tags from the wiki tag cloud:
23
ElenaOanaTabaranu 7.1 24 {image:suggest.png|document=DevGuide.AutoSuggestWidget}
ElenaOanaTabaranu 1.1 25 {code}
26
ElenaOanaTabaranu 7.1 27 $!xwiki.jsx.use("DevGuide.AutoSuggestWidgetExample")
ElenaOanaTabaranu 1.1 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
ElenaOanaTabaranu 7.1 35 The *JavascriptExtension* object from the *DevGuide.AutoSuggestWidgetExample* page contains the Javascript code to enable the widget when focusing on the text field:
ElenaOanaTabaranu 1.1 36
37 {code}
38 (function(){
39 document.observe('dom:loaded', function () {
40 if($('myinput')) {
41 Event.observe($('myinput'), "focus", function() {
Jerome 34.1 42 new XWiki.widgets.Suggest(this, {
43 script: '$xwiki.getURL("${doc.space}.WebHome", "view")?xpage=suggest&classname=XWiki.TagClass&fieldname=tags&secCol=-&',
44 varname: "input",
45 seps: " ,|",
46 offsety: 13
47 });
ElenaOanaTabaranu 1.1 48 });
49 }
50 }); // end of doc observe
51 })();
52 {code}
53
ElenaOanaTabaranu 17.1 54 1.1.1.1 Options used in the *suggest.vm* template:
55
56 {table}
57 Option|Details
58 xpage|Must use *xpage=suggest* because suggest.vm template is used to manage the request.
59 classname|The name of the class for the elements of the suggest list.
60 fieldname|The field name from the class considered for the suggest list.
61 firCol|First column of the list of results.
62 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.
63 {table}
64
ElenaOanaTabaranu 18.1 65 1.1.1.1 Example
ElenaOanaTabaranu 17.1 66
ElenaOanaTabaranu 18.1 67 * Check out the example for class field values at [Class Field Example>DevGuide.AutoSuggestWidgetExample]
68
ElenaOanaTabaranu 14.1 69 1.1.1 Suggest custom information
ElenaOanaTabaranu 1.1 70
ElenaOanaTabaranu 14.1 71 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.
72 For example, you can build a list of suggestions that contains the wiki page names within a certain space:
ElenaOanaTabaranu 30.1 73
ElenaOanaTabaranu 28.1 74 {image:customsuggest.png|document=DevGuide.AutoSuggestWidget}
ElenaOanaTabaranu 26.1 75
ElenaOanaTabaranu 14.1 76 {code}
77
ElenaOanaTabaranu 15.1 78 $!xwiki.jsx.use("DevGuide.AjaxSuggestCustomExample")
ElenaOanaTabaranu 14.1 79 <form method="post" action="#">
ElenaOanaTabaranu 19.1 80 <label for="myinput">Type the name of an example page from the *DevGuide* space and test the suggest list:</label>
Raluca Stavro 31.1 81 <input id="myinput_suggest" size="20" type="text" value=""/>
82 <input id="myinput" type="hidden" />
83 <input id="mybutton" type="button" value="Go" /><br/>
ElenaOanaTabaranu 14.1 84 </form>
85
86 {code}
87
88 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.
89
90 {code}
91 (function(){
92 document.observe('dom:loaded', function () {
Raluca Stavro 31.1 93 if($('myinput_suggest')) {
94 Event.observe($('myinput_suggest'), "focus", function() {
Jerome 34.1 95 new XWiki.widgets.Suggest(this, {
Jerome 33.1 96 script: "$xwiki.getURL("DevGuide.SuggestService", "view", "xpage=plain&spacename=DevGuide")&",
97 varname: "input",
98 seps: " ,|",
99 offsety: 13,
100 minchars: 3
101 });
ElenaOanaTabaranu 14.1 102 });
103 }
104 }); // end of doc observe
105 })();
106 {code}
107 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.
108 {code}
109 ##
110 ## Service to generate the suggest list of files from a certain space.
111 ## @spacename
112 ## @input
113 ##
114 #set($input = $request.get("input").toLowerCase())
115 #set($spacename = $request.get("spacename"))
116 $response.setContentType("text/xml") ## generate a xml file
117 ## select pages
118 #if("$!input" == "")
Raluca Stavro 31.1 119 #set($query = "where doc.space='$spacename' and doc.name<>'WebHome' and doc.name<>'WebPreferences' order by doc.date desc")
ElenaOanaTabaranu 14.1 120 #else
Raluca Stavro 31.1 121 #set($query = "where doc.space='$spacename' and doc.name<>'WebHome' and doc.name<>'WebPreferences' and lower(doc.name) like '%" + $input + "%' order by doc.date desc")
ElenaOanaTabaranu 14.1 122 #end
Raluca Stavro 31.1 123 #set($searchresults = $xwiki.searchDocuments($query, 30, 0))
ElenaOanaTabaranu 14.1 124 <results space="$spacename">
125 #foreach($result in $searchresults)
Raluca Stavro 31.1 126 #set($resultDoc = $xwiki.getDocument($result))
127 #set($resultDocName = $resultDoc.name)
128 #set($resultDocURL = $resultDoc.getURL())
129 <rs id="1" info="$resultDocURL">$resultDocName</rs>
ElenaOanaTabaranu 14.1 130 #end
131 </results>
132 {code}
ElenaOanaTabaranu 17.1 133
MensoHeus 45.1 134 To provide autosuggest to several elements on the form, you can use JavaScript to loop through all the form elements and provide autosuggest if they meet certain conditions. In the example below, if there are form elements with id 'Supplier' in the 'inline' form, they get assigned an autosuggest that uses the Suppliers space as its source. If the element id matches 'Product' the autosuggest is told to use the 'Products' space as it's source instead.
ElenaOanaTabaranu 22.1 135
MensoHeus 45.1 136 This method can be very useful when a form contains a lot of similar elements that require autosuggest. If you make sure the naming is done consistently, you can also use javascript to assign autosuggest based on part of the element id, for example 'all elements ending with _foo' or 'all elements starting with Bar_'. You can use the velocity code from the example above with the code below.
137
138 {code}
139 (function(){
140 document.observe('dom:loaded', function () {
141 myForm = document.getElementById('inline').elements;
142 for(i=0; i<myForm.length; i++){
143 if(myForm[i].id =='Supplier'){
144 mySuggest(myForm[i], 'Suppliers');
145 }
146 if(myForm[i].id=='Product'){
147 mySuggest(myForm[i], 'Products');
148 }
149 }
150 }); // end of doc observe
151 })();
152
153 function mySuggest(element, space) {
154 if (!element.suggest) {
155 element.suggest = new XWiki.widgets.Suggest(element, {
156 script: "$xwiki.getURL("Sandbox.AutoSuggest", "view")"+ "?xpage=plain&spacename="+space+"&",
157 varname: "input",
158 seps: " ,|",
159 offsety: 13,
160 minchars: 1
161 });
162 }
163 }
164 {code}
165
ElenaOanaTabaranu 18.1 166 1.1.1.1 Example
167
168 * Check out the example for custom information at [Custom Information Example>DevGuide.AjaxSuggestCustomExample]
169
Oana Florea 38.1 170 1.1.1 Suggest Users or Groups from the wiki
171
Oana Florea 39.1 172 Local or global users and groups from the wiki can be suggested using the *uorgsuggest.vm* template.
Oana Florea 38.1 173
174 Example:
175
176 $xwiki.jsx.use("$doc.fullName")##
177
178 <input name="userInput" id="userInput" value="" type="text"/>
179
Oana Florea 44.1 180 Here is the code that made the suggestion of global users from the wiki possible:
Oana Florea 38.1 181
182 {code}
183 ...
184 <input name="userInput" id="userInput" value="" type="text"/>
185 ...
186 {code}
187
188 {code}
189 (function(){
190 document.observe('dom:loaded', function () {
191 if($('userInput')) {
192 Event.observe($('userInput'), "focus", function() {
193 new XWiki.widgets.Suggest(this, {
Oana Florea 43.1 194 script: '$xwiki.getURL("${doc.fullName}", "view")?xpage=uorgsuggest&classname=XWiki.XWikiUsers&wiki=global&uorg=user&',
Oana Florea 38.1 195 varname: "input",
196 seps: " ,|",
197 delay : 200,
198 timeout: 5000,
199 offsety: 13
200 });
201 });
202 }
203 }); // end of doc observe
204 })();
205 {code}
206
ElenaOanaTabaranu 1.1 207 1.1 Javascript parameters for the *ajaxsuggest* class
208
209 {table}
210 Parameter|Details
211 minchars|The minimum number of characters after which to trigger the suggest. Default value is 1.
212 get|The HTTP method for the AJAX request.
213 varname| The name of the request parameter holding the input stub. Default value is *input*.
214 className| The CSS classname of the suggest list. Default value is *ajaxsuggest*.
215 timeout|Default value is *2500*.
216 delay|Default value is *500*.
217 offsety|Default value is *-5*.
218 shownoresults|Display a "no results" message, or simply hide the suggest box when no suggestions are available.
219 noresults|Default displayed message is *No results!*.
220 maxheight|Default value is *250*.
221 cache|Default value is *false*.
222 seps|Default value is "".
223 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.
224 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.
225 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.
226 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.
227 parentContainer|The id of the element that will hold the suggest element. Default value is *body*.
ElenaOanaTabaranu 14.1 228 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.
ElenaOanaTabaranu 1.1 229 {table}
230
231
232 1.1 Tips
233
Oana Florea 46.1 234 * Suggest event: *xwiki:suggest:selected*
235 {code}
236 Event.observe($('myInput'), "xwiki:suggest:selected", function(event) {
237 // do something with event.memo.value or event.memo.id or event.memo.info ...
238 });
239 {code}
Oana Florea 37.1 240 * Check out the code:
241 ** for your wiki instance: *{pre}http://localhost:8080/xwiki/resources/js/xwiki/suggest/ajaxSuggest.js {/pre}*.
242 ** the suggest resources on svn(for exemple tag xwiki-web-2.2): [http://svn.xwiki.org/svnroot/xwiki/platform/web/tags/xwiki-web-2.2/standard/src/main/webapp/resources/js/xwiki/suggest/]
Oana Florea 36.1 243 1.1 Bugs we know about
244
Oana Florea 37.1 245 * The suggest feature will not work if the page called by the widget is not saved with programming rights: see details on [http://jira.xwiki.org/jira/browse/XE-539].

Get Connected