简体   繁体   中英

How can i make my pagination button active?

I have a pagination navigation and I want pagination button to be active when user is on that page. Here is how I use pagination code:

<?php if ($totalPages > 1) { ?>
<ul class="pagination justify-content-center mb-4">
    <li class="page-item">
        <?php echo "<a class='page-link' href='index.php?page=1'>First page</a>"?>
    </li>
    <?php
        for ($i=2; $i < $totalPages; $i++){
            echo "<a class='page-link' href='index.php?page=".$i."'>".$i."</a>";
        }

    ?>
    <li class="page-item ">
        <?php echo "<a class='page-link' href='index.php?page=$totalPages'>Last page</a>"?>
    </li>
</ul>
<?php } else { ?>

How can I achieve that?

 for ($i=2; $i < $totalPages; $i++){
       echo "<a class='page-link ".($i == $currentPageNo ? "active"  : "" ). "' href='index.php?page=".$i."'>".$i."</a>";  }

Try this

If you are passing a collection like this:

$data = Model::paginate(10);

Then, in your view, you can use the following to create a pagination with numbers and auto active class in Laravel.

{{ $data->links() }}

OR

You can pass a variable from your controller like:

$active = true;

and in your view you can do something like this:

<a href="somelocation" class="{{ @if($active) acctive @endif }}"> #Link </a>

Before displaying pagination link check if that page no and current page no is same or not. If it is same then add class active to that.For eg:

for ($i=2; $i < $totalPages; $i++){
    if ($i == $currentPageNo) {
        echo "<a class='page-link active' href='index.php?page=".$i."'>".$i."</a>";
    } else {
        echo "<a class='page-link' href='index.php?page=".$i."'>".$i."</a>";
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM