Aller au contenu

Pagination & Tri

Views offre plusieurs options de pagination :

TypeDescriptionUsage
Full pagerNuméros de page completsCatalogues, listes
Mini pagerJuste Précédent/SuivantWidgets, blocs
Load moreBouton “Charger plus”Infinite scroll
Specified numberNombre fixe, sans paginationBlocs, highlights
NoneTous les résultatsPetites listes
  1. Éditez votre vue
  2. Dans la section Pager, cliquez sur le lien actuel (ex: “Mini”)
  3. Sélectionnez le type souhaité
  1. Sélectionnez Paged output, full pager

  2. Apply and continue

  3. Configurez les options :

ParamètreValeurDescription
Items per page12Nombre d’éléments par page
Offset0Sauter les X premiers résultats
Link to moreNoLien “More” en bas
  1. Cliquez sur Pager options :
ParamètreValeur
Pager ID0
Tags → First« Premier
Tags → Previous‹ Précédent
Tags → NextSuivant ›
Tags → LastDernier »
Expose items per page☐ Non
Items per page options12, 24, 48 (si exposé)
  1. Apply

Idéal pour les blocs et widgets :

ParamètreValeur
Items per page4
Tags → Previous
Tags → Next

Pour un effet “infinite scroll” ou bouton “Charger plus” :

Fenêtre de terminal
# Installer le module Views Infinite Scroll
composer require drupal/views_infinite_scroll
drush en views_infinite_scroll -y

Configuration :

  1. Dans Pager, sélectionnez Infinite Scroll
  2. Options :
    • Button text : Charger plus de produits
    • Automatically load content : (optionnel)
  1. Dans Sort Criteria, cliquez sur Add
  2. Choisissez le champ de tri
  3. Configurez :
ParamètreOptions
OrderAscending (A-Z, 0-9) / Descending (Z-A, 9-0)
Granularity (dates)Second, Minute, Hour, Day, Month, Year
Expose☐ Non

Permet à l’utilisateur de choisir le tri :

  1. Éditez un critère de tri existant
  2. Cochez Expose this sort to visitors
  3. Configurez le label

Pour plusieurs options de tri :

  1. Ajoutez plusieurs critères de tri (tous exposés)

  2. Dans Exposed Form Settings (Advanced) :

ParamètreValeur
Exposed sorts labelTrier par
Allow people to choose the sort order ou
Sort asc labelCroissant
Sort desc labelDécroissant
CritèreLabelOrdre par défaut
Content: Authored onPlus récentDESC
Content: TitleNom (A-Z)ASC
Content: PricePrix (bas → haut)ASC
Content: PricePrix (haut → bas)DESC

Pour afficher les produits dans un ordre aléatoire (rotation) :

  1. Add sort criteria
  2. Recherchez “random”
  3. Sélectionnez Global: Random
Fenêtre de terminal
composer require drupal/views_random_seed
drush en views_random_seed -y

Ce module permet un tri “pseudo-aléatoire” qui reste cohérent pendant la session.

  1. Dans AdvancedUse Ajax : ☑ Yes

Avantages :

  • Pas de rechargement de page
  • Expérience fluide
  • Conservation du scroll

Configuration :

# Dans la vue
advanced:
use_ajax: true
// themes/custom/tailstore/js/views-ajax.js
(function ($, Drupal) {
Drupal.behaviors.viewsAjax = {
attach: function (context, settings) {
// Avant le chargement Ajax
$(document).on('views_ajax:beforeSend', function (event, xhr, settings) {
$('.view-products-catalog').addClass('loading');
});
// Après le chargement Ajax
$(document).on('views_ajax:response', function (event, response, settings) {
$('.view-products-catalog').removeClass('loading');
// Scroll vers le haut de la vue
$('html, body').animate({
scrollTop: $('.view-products-catalog').offset().top - 100
}, 300);
});
}
};
})(jQuery, Drupal);

CSS pour le loading :

.view-products-catalog.loading {
opacity: 0.5;
pointer-events: none;
}
.view-products-catalog.loading::after {
content: '';
position: absolute;
top: 50%;
left: 50%;
width: 40px;
height: 40px;
border: 3px solid #ddd;
border-top-color: #0073e6;
border-radius: 50%;
animation: spin 1s linear infinite;
}
@keyframes spin {
to { transform: rotate(360deg); }
}

La pagination utilise des paramètres GET :

/shop?page=2 # Page 3 (0-indexed)
/shop?sort_by=field_price # Tri par prix
/shop?sort_order=DESC # Ordre décroissant
/shop?items_per_page=24 # 24 items par page

Dans Pager options :

ParamètreValeur par défautPersonnalisé
Pager IDpagep
Items per page parameteritems_per_pagelimit
{#
/**
* Mini pager personnalisé
*/
#}
{% if items.previous or items.next %}
<nav class="pager pager--mini" role="navigation" aria-label="Pagination">
<ul class="pager__items">
{% if items.previous %}
<li class="pager__item pager__item--previous">
<a href="{{ items.previous.href }}" title="Page précédente" rel="prev">
<span aria-hidden="true"></span>
<span class="visually-hidden">Page précédente</span>
</a>
</li>
{% endif %}
{% if items.current %}
<li class="pager__item pager__item--current">
Page {{ items.current }}
</li>
{% endif %}
{% if items.next %}
<li class="pager__item pager__item--next">
<a href="{{ items.next.href }}" title="Page suivante" rel="next">
<span class="visually-hidden">Page suivante</span>
<span aria-hidden="true"></span>
</a>
</li>
{% endif %}
</ul>
</nav>
{% endif %}
{#
/**
* Pager complet personnalisé
*/
#}
{% if items %}
<nav class="pager" role="navigation" aria-label="Pagination">
<ul class="pager__items">
{# Premier #}
{% if items.first %}
<li class="pager__item pager__item--first">
<a href="{{ items.first.href }}" title="Première page">
{{ items.first.text|default('« Premier') }}
</a>
</li>
{% endif %}
{# Précédent #}
{% if items.previous %}
<li class="pager__item pager__item--previous">
<a href="{{ items.previous.href }}" rel="prev">
{{ items.previous.text|default('‹ Précédent') }}
</a>
</li>
{% endif %}
{# Pages numérotées #}
{% for key, item in items.pages %}
<li class="pager__item{{ current == key ? ' pager__item--active' : '' }}">
{% if current == key %}
<span class="pager__current">{{ key }}</span>
{% else %}
<a href="{{ item.href }}">{{ key }}</a>
{% endif %}
</li>
{% endfor %}
{# Suivant #}
{% if items.next %}
<li class="pager__item pager__item--next">
<a href="{{ items.next.href }}" rel="next">
{{ items.next.text|default('Suivant ›') }}
</a>
</li>
{% endif %}
{# Dernier #}
{% if items.last %}
<li class="pager__item pager__item--last">
<a href="{{ items.last.href }}" title="Dernière page">
{{ items.last.text|default('Dernier »') }}
</a>
</li>
{% endif %}
</ul>
</nav>
{% endif %}
.pager {
margin: 2rem 0;
text-align: center;
}
.pager__items {
display: inline-flex;
gap: 0.25rem;
list-style: none;
padding: 0;
margin: 0;
}
.pager__item a,
.pager__current {
display: flex;
align-items: center;
justify-content: center;
min-width: 2.5rem;
height: 2.5rem;
padding: 0 0.75rem;
border: 1px solid #ddd;
border-radius: 4px;
text-decoration: none;
color: #333;
transition: all 0.2s;
}
.pager__item a:hover {
background: #0073e6;
border-color: #0073e6;
color: white;
}
.pager__item--active .pager__current {
background: #0073e6;
border-color: #0073e6;
color: white;
font-weight: bold;
}
.pager__item--first a,
.pager__item--last a {
font-size: 0.875rem;
}
  • Pagination complète configurée avec labels français
  • Tri par date, prix, nom configuré
  • Tri exposé aux utilisateurs
  • Ajax activé pour une navigation fluide
  • Message “Aucun résultat” personnalisé
  • Templates de pagination personnalisés (optionnel)
  • Configuration exportée

Créez une vue Blog avec :

  • URL : /blog
  • Pagination : 6 articles par page, full pager
  • Tri : Plus récent d’abord
  • Filtre exposé : Catégorie blog
  • Format : Unformatted list, Teaser
  • Header : Compteur de résultats
  • Bloc sidebar : 3 derniers articles

L’Étape 4 est terminée ! 🎉 Passez aux Exercices Views pour consolider vos acquis, puis à l’Étape 5 - Navigation & Blocs.