Row background color for livetable

Last modified by Mircea Staicu on 2026/08/05 18:25

cogA tutorial to change the background of the livetable rows based on certain criterias
TypeOther
Category
Developed by

Mircea Staicu

Rating
0 Votes
LicenseGNU Lesser General Public License 2.1

Description

Changing the background color of a livetable row

We start by observing the creation of the rows. As a use case, we have a livetable with 5 columns, the title of the document (the event name), the start date of the event, the end date of the event, the start date for the event preparation and the end date of formation.  

According to the XWiki javascript API, the xwiki:livetable:newrow event holds every row. So, referring to our function we access the row like this: event.memo.row.

The JavaScriptExtension object:

  document.observe("xwiki:livetable:newrow", function(event){
    var row = $(event.memo.row); //we store the row element

    /*We access the DOM element which holds the date elements of the livetable and we use the split method to return an array of strings which we pass to the processDateFormat function to get the valid Date objects that we need
    */
    var startDate = processDateFormat(event.memo.row.childNodes[1].textContent.split("/"));
    var endDate = processDateFormat(event.memo.row.childNodes[2].textContent.split("/"));
    var startPreparation = processDateFormat(event.memo.row.childNodes[3].textContent.split("/"));
    var endFormation = processDateFormat(event.memo.row.childNodes[4].textContent.split("/"));

    var today = new Date(); // we store the present date
 
    //we add the CSS classes to the row element according to our needs.

    if(today > startDate && today < endDate){
      row.addClassName('startEndDate');
    }
    else if((today > startPreparation && today < startDate) || (today > endDate && today < endFormation)){
      row.addClassName('startEndFormation');
    }
  });

  //This function returns a valid javascript Date object based on an input array
  processDateFormat = function(input){
    return new Date(input[2].substring(0, 4)+ "/" +input[1]+ "/" +input[0]);
  }

If we select to use this extension on demand we have to remember to include it in the document where we want to use it, i.e. where the definition of the livetable is.

{{velocity}}
#set($discard = $xwiki.jsx.use('Space.DocumentWithTheJS', {'minify' : false}))
{{/velocity}}

The StyleSheetExtension object:

  .startEndDate{
    background-color: #eb9f9f;
  }

  .startEndFormation{
    background-color: #ebda9f;
  }

Same for the style, if we select on demand

{{velocity}}
#set($discard = $xwiki.ssx.use('Space.DocumentWithTheJS'))
{{/velocity}}

Get Connected