Wiki source code of Live Table component

Version 101.1 by ElenaOanaTabaranu on 2009/09/14

Hide last authors
ElenaOanaTabaranu 58.1 1 = Live Table component =
fkorteby 27.1 2
ElenaOanaTabaranu 96.1 3 {{velocity filter="indent"}}
4 {{html clean="false" wiki="true"}}
ElenaOanaTabaranu 82.1 5 #warning("This document is a draft.")
6
fkorteby 27.1 7 #warning("This section documents a feature that is only available starting with XWiki Enterprise 1.9M2.")
8
ElenaOanaTabaranu 68.1 9 {{toc start=2 depth=6 numbered=false scope=page /}}
fkorteby 27.1 10
ElenaOanaTabaranu 58.1 11 == Summary ==
fkorteby 27.1 12
ElenaOanaTabaranu 72.1 13 The **"Live Table"** component is a dynamic table loading data lazily using ajax requests as the user browse the table, in order to scale easily the display of very large amounts of data. Users can browse the table thanks to a pagination system. Filters on columns are available to search for specific entries. Columns can be made sortable in both direction. For a demonstration of the main capabilities of the live table component check out this [[video>>http://vimeo.com/4474332]].
ElenaOanaTabaranu 65.1 14
ElenaOanaTabaranu 58.1 15 == How to use ==
fkorteby 27.1 16
ElenaOanaTabaranu 58.1 17 The Live Table component is made available in several ways to [[Applications>>code:Applications.WebHome]] developers and developers of the [[XWiki platform and products>>dev:Main.WebHome]], depending on their specific needs. The simplest, and most of the times preferred, way to embed a Live Table component is to use the <tt>livetable</tt> velocity macro, available in all pages of your wiki. With only one call to this macro, you will be able to display a live table of the data of your choice! For more specific needs, in the cases you need more control on the table behavior, or to build another component on top of the live table, it is possible to instantiate the live table from JavaScript. In this case, you, as opposed to the velocity macro, you will need to construct the HTML elements the JavaScript component expects yourself, either from JavaScript, either writing the [[HTML directly in your wiki page>>platform:Main.XWikiSyntax]]
fkorteby 27.1 18
ElenaOanaTabaranu 72.1 19 === Using the Velocity livetable macro ===
fkorteby 27.1 20
ElenaOanaTabaranu 58.1 21 Please refer [[Live Table Macro>>code:Macros.LiveTableMacro]] reference.
fkorteby 27.1 22
ElenaOanaTabaranu 58.1 23 === HTML + JavaScript ===
fkorteby 27.1 24
ElenaOanaTabaranu 58.1 25 This is a dynamic component which must me used carefully because of performance issues. The HTML content of the Live Table is automatically updated using Javascript and [[JSON>>http://www.json.org]] code generated with the [[Live Table Macro>>code:Macros.LiveTableMacro]].
fkorteby 27.1 26
ElenaOanaTabaranu 58.1 27 === Example ===
ElenaOanaTabaranu 30.1 28
29 The Live Table component could be extended with a special column containing the ratings of some documents from the wiki.
ElenaOanaTabaranu 58.1 30 The first step would be to generate the JSON code of the column we want to add. In order to achieve this goal we need to modify the result page of the velocity live table macro with our custom page **TutorialsCode.LiveTableRatings**:
ElenaOanaTabaranu 32.1 31
ElenaOanaTabaranu 61.1 32 {{code language=html}}
ElenaOanaTabaranu 95.1 33
ElenaOanaTabaranu 55.1 34 #set($collist = ["doc.name", "_ratings", "doc.date", "doc.author", "_actions"])
ElenaOanaTabaranu 52.1 35
ElenaOanaTabaranu 57.1 36 #set($colprops = {
ElenaOanaTabaranu 32.1 37 "doc.name" : { "type" : "text" , "size" : 30, "sortable":true, "filterable":true},
38 "_ratings" : { "sortable":false},
39 "doc.date" : { "type" : "date" },
40 "doc.author" : { "type" : "text", "link" : "author"},
ElenaOanaTabaranu 51.1 41 "_actions" : { "actions": ["copy","delete","rename","rights"]}
ElenaOanaTabaranu 57.1 42 })
43 #set($options = { "resultPage":"TutorialsCode.LiveTableRatings",
44 "tagCloud" : true,
45 "translationPrefix" : "xe.index.",
46 "rowCount": 10 })
47 #set($ok = $xwiki.ssx.use("TutorialsCode.LiveTableRatings"))
48 #set($ok = $xwiki.jsx.use("TutorialsCode.LiveTableRatings"))
ElenaOanaTabaranu 95.1 49 #livetable("alldocs" $collist $colprops $options)
ElenaOanaTabaranu 58.1 50 {{/code}}
ElenaOanaTabaranu 32.1 51
ElenaOanaTabaranu 85.1 52 If we want to display the rated documents from a certain space, e.g. **Tutorials**, the last parameter of the <tt>#gridresultwithfilter</tt> must contain the query we need.
ElenaOanaTabaranu 34.1 53
ElenaOanaTabaranu 97.1 54 {{code}}
ElenaOanaTabaranu 34.1 55 #includeMacros("TutorialsCode.LiveTableResultsMacros")
ElenaOanaTabaranu 59.1 56
ElenaOanaTabaranu 34.1 57 #gridresultwithfilter("" $request.collist.split(",") "" "doc.space='Tutorials' and doc.name<>'WebHome' and doc.name<>'WebPreferences'")
ElenaOanaTabaranu 59.1 58
ElenaOanaTabaranu 60.1 59 {{/code}}
ElenaOanaTabaranu 73.1 60
ElenaOanaTabaranu 85.1 61 The page at **TutorialsCode.LiveTableResultsMacros** should contain a copy of the default code for the live table result page. The next step implies extracting the rating information from the wiki pages located the **Tutorials** space. We add a new velocity macro which will test the presence of the rating object with class **XWiki.AverageRatingsClass** and extract the rating value.
ElenaOanaTabaranu 90.1 62
ElenaOanaTabaranu 97.1 63 {{code}}
ElenaOanaTabaranu 81.1 64 ##
65 ## list ratings
66 ##
ElenaOanaTabaranu 73.1 67 #macro(grid_ratings $udoc)
68 #set($ratings = "")
69 #set($ratingObj = "")
70 #set($ratingObj = $udoc.getObject("XWiki.AverageRatingsClass"))
71 #if("$!ratingObj" != "")
72 #set($ratings = $ratingObj.get("averagevote"))
73 #end
74 #end
ElenaOanaTabaranu 74.1 75 {{/code}}
ElenaOanaTabaranu 90.1 76
ElenaOanaTabaranu 78.1 77 The continue our extension of this component, we need to add a test for the presence of our custom column to call the <tt>grid_ratings</tt> macro when generating the JSON:
ElenaOanaTabaranu 96.1 78 {{/html}}
79 {{/velocity}}
ElenaOanaTabaranu 93.1 80
ElenaOanaTabaranu 97.1 81 {{code}}
ElenaOanaTabaranu 75.1 82 ...
ElenaOanaTabaranu 79.1 83 #elseif($colname=="_images") ,
84 #grid_photolist($udoc)
85 "${colname}" : "${photolist}"
ElenaOanaTabaranu 75.1 86 #elseif($colname=="_ratings"),
87 #grid_ratings($udoc)
88 "${colname}" : "${ratings}"
ElenaOanaTabaranu 79.1 89 #else ,
ElenaOanaTabaranu 75.1 90 ...
91 {{/code}}
ElenaOanaTabaranu 87.1 92
ElenaOanaTabaranu 80.1 93 #info("Firebug can be a useful tool when testing the JSON code generated.")
ElenaOanaTabaranu 87.1 94
ElenaOanaTabaranu 98.1 95 Transforming the rating information to a nice star display requires using Javascript to replace the content of that column with the proper HTML. Add an on-demand Javascript Extension with a content which can be parsed and use the code below to enrich your component:
ElenaOanaTabaranu 75.1 96
ElenaOanaTabaranu 98.1 97 {{code}}
98 /** Display ratings in the live table on row event */
99 document.observe("xwiki:livetable:newrow", function(ev) {
100 $$('._ratings').each(function (el) {
101 // update content
102 var avgvote = 0.0;
103 if (el.innerHTML !== "") {
104 avgvote = parseFloat(el.innerHTML);
105 }
106 var wstyle = 0;
107 if (avgvote > 0) {
108 wstyle = avgvote * 20;
109 el.innerHTML="<div class='avg-rating'><div class='rating-stars'><ul class='small-star-rating'><li style='width: " + wstyle +"%;' class='current-rating'/><li><a class='one-star' href='#' onclick='return false;'>1</a></li><li><a class='two-stars' href='#' onclick='return false;'>2</a></li><li><a class='three-stars' href='#' onclick='return false;'>3</a></li><li><a class='four-stars' href='#' onclick='return false;'>4</a></li><li><a class='five-stars' href='#' onclick='return false;'>5</a></li></ul></div></div>";
110 } else if(el.innerHTML === ""){
111 el.innerHTML="<div class='avg-rating'><div class='rating-stars'><ul class='small-star-rating'><li style='width: 0%;' class='current-rating'/><li><a class='one-star' href='#' onclick='return false;'>1</a></li> <li><a class='two-stars' href='#' onclick='return false;'>2</a></li><li><a class='three-stars' href='#' onclick='return false;'>3</a></li><li><a class='four-stars' href='#' onclick='return false;'>4</a></li><li><a class='five-stars' href='#' onclick='return false;'>5</a></li></ul></div></div>";
112 }
113 });
114 document.fire("xwiki:livetable:ready", {
115 });
116 });
117 /** Update ratings images on hover event aka mouse over and mouse out.*/
118 document.observe("xwiki:livetable:ready", function(levent) {
119 $$('.xwiki-livetable-display-body tr').each(function (elem) {
120 Event.observe(elem, 'mouseover', function (ev) {
121 var ratings = elem.down('._ratings');
122 if(ratings) {
123 // update default white stars background
124 var elt = ratings.down(2);
125 elt.setStyle({
126 backgroundImage: 'url($xwiki.getDocument("TutorialsCode.LiveTableRatings").getAttachmentURL("star-table.gif"))',
127 backgroundPosition: '0% 0%'
128 });
129 // update votes background
130 elt = ratings.down(3);
131 elt.setStyle({
132 backgroundImage: 'url($xwiki.getDocument("TutorialsCode.LiveTableRatings").getAttachmentURL("star-table.gif"))',
133 backgroundPosition: '0% 50%'
134 });
135 }
136 });
137 Event.observe(elem, 'mouseout', function (ev) {
138 var ratings = elem.down('._ratings');
139 if(ratings) {
140 // restore default white stars background
141 var elt = ratings.down(2);
142 elt.setStyle({
143 backgroundImage: 'url($xwiki.getDocument("XWiki.Ratings").getAttachmentURL("star.gif"))',
144 backgroundPosition: '0% 0%'
145 });
146 // restore votes background
147 elt = elem.down('._ratings').down(3);
148 elt.setStyle({
149 backgroundImage: 'url($xwiki.getDocument("XWiki.Ratings").getAttachmentURL("star.gif"))',
150 backgroundPosition: '0% 50%'
151 });
152 }
153 });
ElenaOanaTabaranu 75.1 154
ElenaOanaTabaranu 98.1 155 });
156 });
157 {{/code}}
158
ElenaOanaTabaranu 99.1 159 Also, we should add some CSS in an on-demand Stylesheet extension with a content which can be parsed to polish the custom column display:
ElenaOanaTabaranu 98.1 160 {{code}}
161 /* ratings for live table */
162 .small-star-rating,
163 .small-star-rating .current-rating {
164 background: url($xwiki.getDocument("XWiki.Ratings").getAttachmentURL("star.gif")) left -1000px repeat-x;
165 }
166 .small-star-rating{
167 position:relative;
168 width:125px;
169 height:25px;
170 overflow:hidden;
171 list-style:none;
172 margin:0px !important;
173 padding:0px !important;
174 background-position: left top;
175 }
176 .small-star-rating li{
177 display: inline;
178 }
179 .small-star-rating a,
180 .small-star-rating .current-rating{
181 position:absolute;
182 top:0;
183 left:0;
184 text-indent:-1000em;
185 height:25px;
186 line-height:25px;
187 outline:none;
188 overflow:hidden;
189 border: none;
190 }
191 .small-star-rating .current-rating{
192 z-index:1;
193 background-position: left center;
194 }
195 .small-star-rating:hover,
196 .small-star-rating:active,
197 .small-star-rating:focus {
198 background-image: url($xwiki.getDocument("TutorialsCode.LiveTableRatings").getAttachmentURL("star-table.gif"));
199 background-repeat: repeat-x;
200 background-position: top left;
201 }
202 .small-star-rating a.one-star{
203 width:20%;
204 z-index:6;
205 }
206 .small-star-rating a.two-stars{
207 width:40%;
208 z-index:5;
209 }
210 .small-star-rating a.three-stars{
211 width:60%;
212 z-index:4;
213 }
214 .small-star-rating a.four-stars{
215 width:80%;
216 z-index:3;
217 }
218 .small-star-rating a.five-stars{
219 width:100%;
220 z-index:2;
221 }
222 .rating-stars {
223 display: inline;
224 float: left;
225 clear: none;
226 }
227
228 {{/code}}
229

Get Connected