Wiki source code of Live Table component

Version 113.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
ElenaOanaTabaranu 104.1 29 #warning("You need the Ratings plugin in your wiki to make the most of the example below.")
ElenaOanaTabaranu 103.1 30
ElenaOanaTabaranu 30.1 31 The Live Table component could be extended with a special column containing the ratings of some documents from the wiki.
ElenaOanaTabaranu 58.1 32 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 33
ElenaOanaTabaranu 61.1 34 {{code language=html}}
ElenaOanaTabaranu 95.1 35
ElenaOanaTabaranu 55.1 36 #set($collist = ["doc.name", "_ratings", "doc.date", "doc.author", "_actions"])
ElenaOanaTabaranu 52.1 37
ElenaOanaTabaranu 57.1 38 #set($colprops = {
ElenaOanaTabaranu 32.1 39 "doc.name" : { "type" : "text" , "size" : 30, "sortable":true, "filterable":true},
40 "_ratings" : { "sortable":false},
41 "doc.date" : { "type" : "date" },
42 "doc.author" : { "type" : "text", "link" : "author"},
ElenaOanaTabaranu 51.1 43 "_actions" : { "actions": ["copy","delete","rename","rights"]}
ElenaOanaTabaranu 57.1 44 })
45 #set($options = { "resultPage":"TutorialsCode.LiveTableRatings",
46 "tagCloud" : true,
47 "translationPrefix" : "xe.index.",
48 "rowCount": 10 })
49 #set($ok = $xwiki.ssx.use("TutorialsCode.LiveTableRatings"))
50 #set($ok = $xwiki.jsx.use("TutorialsCode.LiveTableRatings"))
ElenaOanaTabaranu 95.1 51 #livetable("alldocs" $collist $colprops $options)
ElenaOanaTabaranu 58.1 52 {{/code}}
ElenaOanaTabaranu 32.1 53
ElenaOanaTabaranu 85.1 54 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 55
ElenaOanaTabaranu 97.1 56 {{code}}
ElenaOanaTabaranu 34.1 57 #includeMacros("TutorialsCode.LiveTableResultsMacros")
ElenaOanaTabaranu 59.1 58
ElenaOanaTabaranu 34.1 59 #gridresultwithfilter("" $request.collist.split(",") "" "doc.space='Tutorials' and doc.name<>'WebHome' and doc.name<>'WebPreferences'")
ElenaOanaTabaranu 59.1 60
ElenaOanaTabaranu 60.1 61 {{/code}}
ElenaOanaTabaranu 73.1 62
ElenaOanaTabaranu 106.1 63 The page at **TutorialsCode.LiveTableResultsMacros** should contain a copy of the default code (the page is located at **XWiki.LiveTableResultsMacro**) 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 64
ElenaOanaTabaranu 97.1 65 {{code}}
ElenaOanaTabaranu 81.1 66 ##
67 ## list ratings
68 ##
ElenaOanaTabaranu 73.1 69 #macro(grid_ratings $udoc)
70 #set($ratings = "")
71 #set($ratingObj = "")
72 #set($ratingObj = $udoc.getObject("XWiki.AverageRatingsClass"))
73 #if("$!ratingObj" != "")
74 #set($ratings = $ratingObj.get("averagevote"))
75 #end
76 #end
ElenaOanaTabaranu 74.1 77 {{/code}}
ElenaOanaTabaranu 90.1 78
ElenaOanaTabaranu 78.1 79 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 80 {{/html}}
81 {{/velocity}}
ElenaOanaTabaranu 93.1 82
ElenaOanaTabaranu 97.1 83 {{code}}
ElenaOanaTabaranu 75.1 84 ...
ElenaOanaTabaranu 79.1 85 #elseif($colname=="_images") ,
86 #grid_photolist($udoc)
87 "${colname}" : "${photolist}"
ElenaOanaTabaranu 75.1 88 #elseif($colname=="_ratings"),
89 #grid_ratings($udoc)
90 "${colname}" : "${ratings}"
ElenaOanaTabaranu 79.1 91 #else ,
ElenaOanaTabaranu 75.1 92 ...
93 {{/code}}
ElenaOanaTabaranu 87.1 94
ElenaOanaTabaranu 80.1 95 #info("Firebug can be a useful tool when testing the JSON code generated.")
ElenaOanaTabaranu 87.1 96
ElenaOanaTabaranu 107.1 97 Transforming the rating information to a nice star display requires using Javascript to replace the content of that column with the proper HTML. Attach the image below to your **TutorialsCode.LiveTableRatings** page to change the default white background when hovering on the stars.
ElenaOanaTabaranu 75.1 98
ElenaOanaTabaranu 109.1 99 [[image:star-table.gif||style="margin-right: 1em;"]]
ElenaOanaTabaranu 108.1 100
101
ElenaOanaTabaranu 107.1 102 Add an on-demand Javascript Extension with a content which can be parsed and use the code below to enrich your component:
103
ElenaOanaTabaranu 98.1 104 {{code}}
105 /** Display ratings in the live table on row event */
106 document.observe("xwiki:livetable:newrow", function(ev) {
107 $$('._ratings').each(function (el) {
108 // update content
109 var avgvote = 0.0;
110 if (el.innerHTML !== "") {
111 avgvote = parseFloat(el.innerHTML);
112 }
113 var wstyle = 0;
114 if (avgvote > 0) {
115 wstyle = avgvote * 20;
116 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>";
117 } else if(el.innerHTML === ""){
118 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>";
119 }
120 });
121 document.fire("xwiki:livetable:ready", {
122 });
123 });
124 /** Update ratings images on hover event aka mouse over and mouse out.*/
125 document.observe("xwiki:livetable:ready", function(levent) {
126 $$('.xwiki-livetable-display-body tr').each(function (elem) {
127 Event.observe(elem, 'mouseover', function (ev) {
128 var ratings = elem.down('._ratings');
129 if(ratings) {
130 // update default white stars background
131 var elt = ratings.down(2);
132 elt.setStyle({
133 backgroundImage: 'url($xwiki.getDocument("TutorialsCode.LiveTableRatings").getAttachmentURL("star-table.gif"))',
134 backgroundPosition: '0% 0%'
135 });
136 // update votes background
137 elt = ratings.down(3);
138 elt.setStyle({
139 backgroundImage: 'url($xwiki.getDocument("TutorialsCode.LiveTableRatings").getAttachmentURL("star-table.gif"))',
140 backgroundPosition: '0% 50%'
141 });
142 }
143 });
144 Event.observe(elem, 'mouseout', function (ev) {
145 var ratings = elem.down('._ratings');
146 if(ratings) {
147 // restore default white stars background
148 var elt = ratings.down(2);
149 elt.setStyle({
150 backgroundImage: 'url($xwiki.getDocument("XWiki.Ratings").getAttachmentURL("star.gif"))',
151 backgroundPosition: '0% 0%'
152 });
153 // restore votes background
154 elt = elem.down('._ratings').down(3);
155 elt.setStyle({
156 backgroundImage: 'url($xwiki.getDocument("XWiki.Ratings").getAttachmentURL("star.gif"))',
157 backgroundPosition: '0% 50%'
158 });
159 }
160 });
ElenaOanaTabaranu 75.1 161
ElenaOanaTabaranu 98.1 162 });
163 });
164 {{/code}}
165
ElenaOanaTabaranu 110.1 166 Also, 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 167 {{code}}
168 /* ratings for live table */
169 .small-star-rating,
170 .small-star-rating .current-rating {
171 background: url($xwiki.getDocument("XWiki.Ratings").getAttachmentURL("star.gif")) left -1000px repeat-x;
172 }
173 .small-star-rating{
174 position:relative;
175 width:125px;
176 height:25px;
177 overflow:hidden;
178 list-style:none;
179 margin:0px !important;
180 padding:0px !important;
181 background-position: left top;
182 }
183 .small-star-rating li{
184 display: inline;
185 }
186 .small-star-rating a,
187 .small-star-rating .current-rating{
188 position:absolute;
189 top:0;
190 left:0;
191 text-indent:-1000em;
192 height:25px;
193 line-height:25px;
194 outline:none;
195 overflow:hidden;
196 border: none;
197 }
198 .small-star-rating .current-rating{
199 z-index:1;
200 background-position: left center;
201 }
202 .small-star-rating:hover,
203 .small-star-rating:active,
204 .small-star-rating:focus {
205 background-image: url($xwiki.getDocument("TutorialsCode.LiveTableRatings").getAttachmentURL("star-table.gif"));
206 background-repeat: repeat-x;
207 background-position: top left;
208 }
209 .small-star-rating a.one-star{
210 width:20%;
211 z-index:6;
212 }
213 .small-star-rating a.two-stars{
214 width:40%;
215 z-index:5;
216 }
217 .small-star-rating a.three-stars{
218 width:60%;
219 z-index:4;
220 }
221 .small-star-rating a.four-stars{
222 width:80%;
223 z-index:3;
224 }
225 .small-star-rating a.five-stars{
226 width:100%;
227 z-index:2;
228 }
229 .rating-stars {
230 display: inline;
231 float: left;
232 clear: none;
233 }
234
235 {{/code}}
ElenaOanaTabaranu 111.1 236
237 The final result should look like this:
ElenaOanaTabaranu 113.1 238 [[image:livetabel.png||style="margin-right: 1em;"]]
ElenaOanaTabaranu 111.1 239
240

Get Connected