1

With the last release of Bootstrap, the .active property is set on the link class. How could we add this property to a Paginator->sort ?

An example :

$this->Paginator->templates([
    'sort' => '<a class="nav-link" href="{{url}}">{{text}}</a>',
]);

<ul class="nav">
  <li role="presentation" class="nav-item">
      <?= $this->Paginator->sort('Results.points','Per points') ?>
  </li>

  <li role="presentation" class="nav-item">
      <?= $this->Paginator->sort('Results.size','Per size') ?>
  </li>
</ul>

How could we add active to the "Per points" or "Per size" link?

WebDevBooster
  • 14,674
  • 9
  • 66
  • 70
dype
  • 500
  • 1
  • 4
  • 15

1 Answers1

0

You can retrieve the current Sort param value with the following.

<?php $active = $this->Paginator->param('sort'); ?>

With that you can take and add the class on the fly for your items.

  <li role="presentation" class="nav-item <?= $active === 'Results.points' ? 'active' : ''; ?>
      <?= $this->Paginator->sort('Results.points','Per points') ?>
  </li>

Another possible option might be to override the sortAsc and sortDesc templates and wrap them with the active and wrap the standard sort template with your base <li> template

$this->Paginator->templates([
    'sort' => '<li role="presentation" class="nav-item"><a class="nav-link" href="{{url}}">{{text}}</a></li>',
    'sortAsc' => '<li role="presentation active" class="nav-item"><a class="nav-link" href="{{url}}">{{text}}</a></li>',
]);

Edit: this example is for applying my changes to the A tag.

$this->Paginator->templates([
    'sort' => '<li role="presentation" class="nav-item"><a class="nav-link" href="{{url}}">{{text}}</a></li>',
    'sortAsc' => '<li role="presentation" class="nav-item"><a class="nav-link active" href="{{url}}">{{text}}</a></li>',
]);
KaffineAddict
  • 436
  • 2
  • 11
  • Thanks fot this answer, but it does not works with **bootstrap 4** because `active` state is set on the class of the "a" tag and not on the class of the "li" tag. – dype Feb 21 '18 at 18:07
  • You should be able to take what I am doing and apply it the same way but in the a tag. Ill update the example for you. – KaffineAddict Mar 02 '18 at 16:02