OpenWGA 7.4 - WebTML reference

WebTML tags » foreach

<tml:foreach sortcomparison ="tmlscript-expression">

Purpose:

Determines an expression to compare iteration objects for sorting

Description:

Use this together with sortorder for sorting of iteration objects. The contents of this attribute is an TMLScript expression which is executed to compare two individual iteration objects and determine which one is to be regarded "larger" than the other.

If the iteration objects are contents (either type="content" or type="itemvalue" where the item list contains content documents) then two TMLContext objects, representing the contents to compare, are passed into the expression under variable names $CONTEXT1 and $CONTEXT2

Other iteration objects are passed into the expression under variable names $VALUE1 and $VALUE2.

If attribute currentvalue then the respective objects are also given under the name determine there, suffixed by "1" and "2". 

The expression must return a number indicating the larger object:

  • A value of 0 determines that both objects are absolutely equal
  • A value larger than 0 determines that the VALUE/CONTEXT1 is larger than VALUE/CONTEXT2
  • A value lower than 0 determines that the VALUE/CONTEXT1 is smaller than VALUE/CONTEXT2

For a simpler approach to only sort based on one sort criteria use sortexpression.

Sorting can have very bad performance on large collections, even if only a small part of that collection is displayed. The reason for this is that OpenWGA needs to load all documents in the collection before it can sort them and determine what documents are in the requested page.

On collections from queries please consider using the query languages own ordering capabilities instead if possible.

On navigators please consider using attribute order instead if possible.


Value(s):

A TMLScript expression

Examples:

Attribute "sortcomparison" will mostly be used to create more complex sorting functionalties with multiple sort criteria. It is seldomly practical to directly write all of the functionality into the attribute directly. Instead it is convenient to predefine the sort functionality as a function and call this function from the attribute.

The following example sorts contents based on they published date first. If published date is equal it sorts them based on their title. The sort functionality is defined as a JavaScript function, stored in WebTML variable "sortContents", and referenced from the "sortcomparison" attribute afterwards:

<tml:script>


sortContents = function(cx1, cx2) {


  var date1 = WGA.dateOnly(cx1.PAGEPUBLISHED);

  var date2 = WGA.dateOnly(cx2.PAGEPUBLISHED);


  var diff = date1.time - date2.time;

  if (diff != 0) {

    return diff;

  }


  if (cx1.TITLE > cx2.TITLE) {

    return 1;

  }

  else if (cx1.TITLE < cx2.TITLE) {

    return -1;

  }

  else {

    return 0;

  }


}


</tml:script>


<tml:foreach sortorder="asc" sortcomparison="sortContents($CONTEXT1, $CONTEXT2)">

  ...

</tml:foreach>