Submitted : {tri} and #TRI in loops

**SPIP**

# Article submitted

The article "**{tri} and #TRI in loops**
(https://www.spip.net/ecrire/?exec=article&id_article=6588)" is submitted
for publication. on Wednesday 10 June 2020.

* * *

## {tri} and #TRI in loops

Wednesday 10 June 2020 , by [jack](.././?page=auteur&id_auteur=593&)

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

Criterion `{tri}` simplifies the realisation of sortable lists 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>

    #TRI

    {titre,'Trier par titre'}
    
     |
    
    #TRI

    {date,'Trier par date'}
    
     |

    #TRI

    {date_redac,'Trier par date de rédaction'}
    
    <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 sort is shown in bold and not clickable, and unused
sorts 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>

    #TRI

    {titre,'Trier par titre',ajax}
    
     |
    
    #TRI

    {date,'Trier par date',ajax}
    
     |

    #TRI

    {date_redac,'Trier par date de rédaction',ajax}
    
    <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>

    #TRI

    {titre,'Trier par titre'}
    
     |
    
    #TRI

    {date,'Trier par date'}
    
     |

    #TRI

    {date_redac,'Trier par date de rédaction'}
    
    <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>

    #TRI

    {>,'Tri croissant',ajax}
    
     |
    
    #TRI

    {<,'Tri décroissant',ajax}
    
     |
    
    #TRI

    {titre,'Trier par titre',ajax}
    
     |
    
    #TRI

    {date,'Trier par date',ajax}
    
     |

    #TRI

    {date_redac,'Trier par date de rédaction',ajax}
    
    <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>

    #TRI

    {titre,'Trier par titre',ajax}
    
     |
    
    #TRI

    {date,'Trier par date',ajax}
    
     |

    #TRI

    {date_redac,'Trier par date de rédaction',ajax}
    
    <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.

— Sent by SPIP (https://www.spip.net/)

rubon57.jpg