Summary

The "Livetable" 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, check out the XWiki livetable component video.

How to use

The Livetable component is made available in several ways to Extensions developers and developers of the XWiki platform and products, depending on their specific needs. The simplest, and most of the times preferred, way to embed a Livetable component is to use the livetable velocity macro, available in all pages of your wiki. With only one call to this macro, you will be able to display a Livetable 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 Livetable, it is possible to instantiate the Livetable 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

Using the Velocity Livetable macro

Please refer to XWiki Livetable Macro reference.

HTML + JavaScript

This is a dynamic component which must be used carefully because of performance issues. The HTML content of the Livetable is automatically updated using Javascript and JSON code generated with the Livetable Macro.
The Livetable component is build using the Prototype library as a class representing an AJAX-populated Livetable:

  • The default variable name generated in XWiki is livetable and you can use it, for example to manipulate the display of the component's rows by calling livetable.showRows() or to refresh it livetable.refresh()(older versions of the livetable use the default variable name ta).
  • For more details, check the Javascript file livetable.js on your wiki or on GitHub.

Example

You need the Ratings Application in your wiki to make the most of the example below.

The Livetable component could be extended with a special column containing the ratings of some documents from the wiki.
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 Livetable macro with our custom page TutorialsCode.LiveTableRatings:

#set($collist = ["doc.name", "_ratings", "doc.date", "doc.author", "_actions"])
#set($colprops = {
                   "doc.name" : { "type" : "text" , "size" : 30, "sortable":true, "filterable":true},
                   "_ratings" : { "sortable":false},
                   "doc.date" : { "type" : "date" },
                   "doc.author" : { "type" : "text", "link" : "author"},
                   "_actions" : { "actions": ["copy","delete","rename","rights"]}
                 })
#set($options = { "resultPage":"TutorialsCode.LiveTableRatings",
                  "tagCloud" : true,
                  "translationPrefix" : "xe.index.",
                  "rowCount": 10 })
#set($ok = $xwiki.ssx.use("TutorialsCode.LiveTableRatings"))
#set($ok = $xwiki.jsx.use("TutorialsCode.LiveTableRatings"))
#livetable("alldocs" $collist $colprops $options) 

If we want to display the rated documents from a certain space, e.g. Tutorials, the last parameter of the #gridresultwithfilter must contain the query we need.

#includeMacros("TutorialsCode.LiveTableResultsMacros")

#gridresultwithfilter("" $request.collist.split(",") "" "and doc.space='Tutorials' and doc.name<>'WebHome' and doc.name<>'WebPreferences'")

The page at TutorialsCode.LiveTableResultsMacros should contain a copy of the default code (the page is located at XWiki.LiveTableResultsMacro) for the Livetable 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.

##
## list ratings
##
#macro(grid_ratings $udoc)
  #set($ratings = "")
  #set($ratingObj = "")
  #set($ratingObj = $udoc.getObject("XWiki.AverageRatingsClass"))
  #if("$!ratingObj" != "")
     #set($ratings = $ratingObj.get("averagevote"))
  #end
#end

To continue our extension of this component, we need to add a test for the presence of our custom column to call the grid_ratings macro when generating the JSON: 

...
#elseif($colname == '_images') ,
  #livetable_getImagesList($itemDoc)
  "$escapetool.javascript(${colname})" : "$escapetool.javascript(${imagesList})"
#elseif($colname=="_ratings"),
  #grid_ratings($itemDoc)
  "${colname}" : "${ratings}"
#else ,
...
Firebug can be a useful tool when testing the JSON code generated.

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.

star-table.gif

Add an on-demand Javascript Extension with a content which can be parsed and use the code below to enrich your component:

/** Display ratings in the Livetable on row event */
document.observe("xwiki:livetable:newrow", function(ev) {
  $$('._ratings').each(function (el) {
     // update content
     var avgvote = 0.0;
     if (el.innerHTML !== "") {
        avgvote = parseFloat(el.innerHTML);
      }
     var wstyle = 0;
     if (avgvote > 0) {
        wstyle = avgvote * 20;
        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>";
      } else if(el.innerHTML === ""){
        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>";
      }
   });
  document.fire("xwiki:livetable:ready", {
   });
});
/** Update ratings images on hover event aka mouse over and mouse out.*/
document.observe("xwiki:livetable:ready", function(levent) {
  $$('.xwiki-livetable-display-body tr').each(function (elem) {
    Event.observe(elem, 'mouseover', function (ev) {
     var ratings = elem.down('._ratings');
     if(ratings) {
       // update default white stars background
       var elt = ratings.down(2);
        elt.setStyle({
          backgroundImage: 'url($xwiki.getDocument("TutorialsCode.LiveTableRatings").getAttachmentURL("star-table.gif"))',
          backgroundPosition: '0% 0%'
        });
       // update votes background     
       elt = ratings.down(3);
        elt.setStyle({
          backgroundImage: 'url($xwiki.getDocument("TutorialsCode.LiveTableRatings").getAttachmentURL("star-table.gif"))',
          backgroundPosition: '0% 50%'
        });
      }
    });
    Event.observe(elem, 'mouseout', function (ev) {
     var ratings = elem.down('._ratings');
     if(ratings) {
       // restore default white stars background
       var elt = ratings.down(2);
        elt.setStyle({
          backgroundImage: 'url($xwiki.getDocument("XWiki.Ratings").getAttachmentURL("star.gif"))',
          backgroundPosition: '0% 0%'
        });
       // restore votes background     
       elt = elem.down('._ratings').down(3);
        elt.setStyle({
          backgroundImage: 'url($xwiki.getDocument("XWiki.Ratings").getAttachmentURL("star.gif"))',
          backgroundPosition: '0% 50%'
        });
      }
    });

  });
});

Also, add some CSS in an on-demand Stylesheet extension with a content which can be parsed to polish the custom column display:

/* ratings for Livetable */
.small-star-rating,
.small-star-rating .current-rating {
  background: url($xwiki.getDocument("XWiki.Ratings").getAttachmentURL("star.gif")) left -1000px repeat-x;
}
.small-star-rating{
  position:relative;
  width:125px;
  height:25px;
  overflow:hidden;
  list-style:none;
  margin:0px !important;
  padding:0px !important;
  background-position: left top;
}
.small-star-rating li{
  display: inline;
}
.small-star-rating a,
.small-star-rating .current-rating{
  position:absolute;
  top:0;
  left:0;
  text-indent:-1000em;
  height:25px;
  line-height:25px;
  outline:none;
  overflow:hidden;
  border: none;
}
.small-star-rating .current-rating{
  z-index:1;
  background-position: left center;
}
.small-star-rating:hover,
.small-star-rating:active,
.small-star-rating:focus {
  background-image: url($xwiki.getDocument("TutorialsCode.LiveTableRatings").getAttachmentURL("star-table.gif"));
  background-repeat: repeat-x;
  background-position: top left;
}
.small-star-rating a.one-star{
  width:20%;
  z-index:6;
}
.small-star-rating a.two-stars{
  width:40%;
  z-index:5;
}
.small-star-rating a.three-stars{
  width:60%;
  z-index:4;
}
.small-star-rating a.four-stars{
  width:80%;
  z-index:3;
}
.small-star-rating a.five-stars{
  width:100%;
  z-index:2;
}
.rating-stars {
  display: inline;  
  float: left;
  clear: none;
}

The final result should look like this:

livetabel.png

Tags:
Created by Vincent Massol on 2017/09/04
   

Get Connected