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 {image:suggest.png|document=DevGuide.AutoSuggestWidget}
24 {code}
25
26 $!xwiki.jsx.use("DevGuide.AutoSuggestWidgetExample")
27 <form method="post" action="#">
28 <label for="myinput">Type the name of a tag and test the suggest list:</label>
29 <input id="myinput" size="20" type="text" value=""/>
30 </form>
31
32 {code}
33
34 The *JavascriptExtension* object from the *DevGuide.AutoSuggestWidgetExample* page contains the Javascript code to enable the widget when focusing on the text field:
35
36 {code}
37 (function(){
38 document.observe('dom:loaded', function () {
39 if($('myinput')) {
40 Event.observe($('myinput'), "focus", function() {
41 new ajaxSuggest(this, {script: '$xwiki.getURL("${doc.space}.WebHome", "view")?xpage=suggest&classname=XWiki.TagClass&fieldname=tags&secCol=-&', varname: "input", seps: " ,|", offsety: 13});
42 });
43 }
44 }); // end of doc observe
45 })();
46 {code}
47
48 1.1.1.1 Options used in the *suggest.vm* template:
49
50 {table}
51 Option|Details
52 xpage|Must use *xpage=suggest* because suggest.vm template is used to manage the request.
53 classname|The name of the class for the elements of the suggest list.
54 fieldname|The field name from the class considered for the suggest list.
55 firCol|First column of the list of results.
56 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.
57 {table}
58
59
60 1.1.1 Suggest custom information
61
62 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.
63 For example, you can build a list of suggestions that contains the wiki page names within a certain space:
64 {code}
65
66 $!xwiki.jsx.use("DevGuide.AjaxSuggestCustomExample")
67 <form method="post" action="#">
68 <label for="myinput">Type the name of an example page from the **DevGuide** space and test the suggest list:</label>
69 <input id="myinput" size="20" type="text" value=""/>
70 </form>
71
72 {code}
73
74 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.
75
76 {code}
77 (function(){
78 document.observe('dom:loaded', function () {
79 if($('myinput')) {
80 Event.observe($('myinput'), "focus", function() {
81 new ajaxSuggest(this, {script: "$xwiki.getURL("DevGuide.SuggestService", "view", "xpage=plain&spacename=Tutorials")&", varname: "input", seps: " ,|", offsety: 13});
82 });
83 }
84 }); // end of doc observe
85 })();
86 {code}
87 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.
88 {code}
89 ##
90 ## Service to generate the suggest list of files from a certain space.
91 ## @spacename
92 ## @input
93 ##
94 #set($input = $request.get("input").toLowerCase())
95 #set($spacename = $request.get("spacename"))
96 $response.setContentType("text/xml") ## generate a xml file
97 ## select pages
98 #if("$!input" == "")
99 #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")
100 #else
101 #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")
102 #end
103 #set($searchresults = $xwiki.search($query))
104 <results space="$spacename">
105 #foreach($result in $searchresults)
106 <rs id="1" info="">$result</rs>
107 #end
108 </results>
109 {code}
110
111 1.1 Javascript parameters for the *ajaxsuggest* class
112
113 {table}
114 Parameter|Details
115 minchars|The minimum number of characters after which to trigger the suggest. Default value is 1.
116 get|The HTTP method for the AJAX request.
117 varname| The name of the request parameter holding the input stub. Default value is *input*.
118 className| The CSS classname of the suggest list. Default value is *ajaxsuggest*.
119 timeout|Default value is *2500*.
120 delay|Default value is *500*.
121 offsety|Default value is *-5*.
122 shownoresults|Display a "no results" message, or simply hide the suggest box when no suggestions are available.
123 noresults|Default displayed message is *No results!*.
124 maxheight|Default value is *250*.
125 cache|Default value is *false*.
126 seps|Default value is "".
127 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.
128 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.
129 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.
130 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.
131 parentContainer|The id of the element that will hold the suggest element. Default value is *body*.
132 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.
133 {table}
134
135
136 1.1 Live Example
137
138 * Check out the example above at [Ajax Suggest Example>DevGuide.AutoSuggestWidgetExample]
139
140 1.1 Tips
141
142 * Check out the Javascript code for more details: *{pre}http://localhost:8080/xwiki/resources/js/xwiki/suggest/ajaxSuggest.js {/pre}*.

Get Connected