{tri} and #TRI in loops

SPIP 3.0 introduces a criterion and a tag which make it easy to create sortable lists.

Criterion {tri} simplifies the lists sorting in the same way as the criterion {pagination} simplifies the creation of paginated lists.
In a simple loop, it allows you to define the sort criterion by indicating as argument the field to be used by default:

<BOUCLE_art(ARTICLES){tri titre}>
</BOUCLE_art>

So far nothing new in relation to the {par} criterion. But {tri} can be associated with the #TRI tag which allows you to generate a link to change the sort criteria. The #TRI tag takes as first argument the sort field, and as second argument the label of the clickable link to use this field for the sorting:

<B_art>
<p>#TRI{titre,'Sort by title'} | #TRI{date,'Sort by date'} | 
#TRI{date_redac,'Sort by editing date'}</p>
<BOUCLE_art(ARTICLES){tri titre}>
</BOUCLE_art>

In doing so, the user is offered a menu to change the sorting of the loop. The currently used sorting is shown in bold and not clickable, and the other columns available for sorting are clickable links.

If the loop is in an ajax inclusion, it is enough to inform the ajax class as the third argument of the #TRI tag for the links to behave with a partial reloading of the page:

<B_art>
<p>#TRI{titre,'Sort by title',ajax} | #TRI{date,'Sort by date',ajax} | 
#TRI{date_redac,'Sort by editing date',ajax}</p>
<BOUCLE_art(ARTICLES){tri titre}>
</BOUCLE_art>

The criterion {tri} also allows you to specify the default sort direction as a second argument. The value direct or 1 must be specified for an ascending sort by default, and inverse or -1 for a descending sort by default:

<B_art>
<p>#TRI{titre,'Sort by title'} | #TRI{date,'Sort by date'} | 
#TRI{date_redac,'Sort by editing date'}</p>
<BOUCLE_art(ARTICLES){tri date,-1}>
</BOUCLE_art>

In addition, it is also possible to propose the choice of the sorting direction. This is done by passing > for an increasing sort and < for a decreasing sort as first argument of the #TRI tag:

<B_art>
<p>#TRI{>,'Sort ascending',ajax} | #TRI{<,'Sort descending',ajax} | #TRI{titre,'Sort by title',ajax} | #TRI{date,'Sort by date',ajax} | 
#TRI{date_redac,'Sort by editing date',ajax}</p>
<BOUCLE_art(ARTICLES){tri titre}>
</BOUCLE_art>

That said, rather than proposing buttons for systematic sorting, an alternative is possible: automatically adapt the sorting direction to the column used for sorting. This is done by passing as the second argument of the sort tag an array with the default direction for each column:

#SET{defaut_tri,#ARRAY{
titre,1,
date,-1,
date_redac,-1
}}
<B_art>
<p>#TRI{titre,'Sort by title',ajax} | #TRI{date,'Sort by date',ajax} | 
#TRI{date_redac,'Sort by editing date',ajax}</p>
<BOUCLE_art(ARTICLES){tri titre,#GET{defaut_tri}}>
</BOUCLE_art>

So when the user chooses to sort by title, it will be in ascending order. But when he chooses the date or the date of writing, the sorting will be in descending order.

Author jack Published :

Translations : English, français, Nederlands