简体   繁体   English

如何在laravel中自定义分页?

[英]how to custom pagination in laravel?

I have a pagination in view but the problem is i have so many items that mans a big pagination like this 我有一个分页,但问题是我有太多物品,像这样大分页 在此处输入图片说明

what i want is display 1 2 3 ... 6 7 for example i tried to do it but i didn't found any thing, 例如,我想要的是显示1 2 3 ... 6 7,但是我没有发现任何东西,

This is my action : 这是我的动作:

public function index()
{
    // get all the logs
    $logs = DB::table('services')
        ->join('logs', 'services.id', '=', 'logs.service_id')
        ->paginate(20);
    // load the view and pass the logs
    return View::make('logs.index',array('logs'=> $logs,'title'=>'Service Logs'));
}

This is my view : 这是我的看法:

   <div class="container">
    @foreach($logs as $key => $value)
        <tr>
            <td>{{ $value->domain }}</td>
            <td>{{ $value->service_port }}</td>
            <td>{{ $value->checktime }}</td>
            <td class="text-center">
                @if( $value->status == 'up' ) <img src="../img/up3.png" />
                @elseif( $value->status == 'down' ) <img src="../img/down3.png" />
                @else <img width="30" height="30" src="../img/warning_icon.png" />
                @endif
            </td>
            <td>{{ $value->response_time }}</td>
        </tr>
    @endforeach
  </div>
 {{$logs->links();}}

So i tried everything to do that so please if someone has any idea i will be very appreciative 所以我尽一切努力做到这一点,所以如果有人有任何想法,我将非常感激

By default Laravel will use "sliding" page numbers, however, it requires a minimum of 13 pages before it will create the "sliding" page numbers. 默认情况下,Laravel将使用“滑动”页码,但是,在创建“滑动”页码之前,至少需要13页。 If you have less than 13 pages it will default to an ordinary range of pages. 如果您的页面少于13页,则默认为普通页面范围。

Unfortunately this number is hard coded into Laravel. 不幸的是,这个数字被硬编码到Laravel中。

See this comment (v4.1.24) in the Presenter class that is responsible for building the pages. 请参阅Presenter类中负责构建页面的注释(v4.1.24)

you can use your own view: 您可以使用自己的视图:

config/view.php config / view.php

'pagination' => 'my-pagination',

view/my-pagination.php 查看/my-pagination.php

<?php
$presenter = new Illuminate\Pagination\BootstrapPresenter($paginator);
$interval = 3;
$numberPages = $paginator->getLastPage();
$currentPage = $paginator->getCurrentPage();

if ($numberPages > 1)
{

    ?>
    <ul class="pagination pagination-sm">
        <?php
        if ($numberPages <= $interval)
        {
            for ($i = 1; $i <= $numberPages; $i++)
            {

                ?>
                <li class="<?php echo $i == $currentPage ? ' active' : ''; ?>">
                    <a href="<?php echo $paginator->getUrl($i); ?>" ><?php echo $i; ?></a>
                </li>
                <?php
            }
        }
        else
        {
            if ($currentPage < $interval)
            {
                if ($currentPage > 1)
                {

                    ?>
                    <li class="prev">
                        <a href="{{ $paginator->getUrl($paginator->getCurrentPage()-1) }}">
                            <i class="fa fa-angle-double-left"></i>
                        </a>
                    </li>
                    <?php
                }
                for ($i = 1; $i <= $interval; $i++)
                {

                    ?>
                    <li class="<?php echo $i == $currentPage ? ' active' : ''; ?>">
                        <a href="<?php echo $paginator->getUrl($i); ?>" ><?php echo $i; ?></a>
                    </li>
                    <?php
                }
            }
            elseif ($currentPage > ($numberPages - ($interval - 1)))
            {

                ?>
                <li class="prev">
                    <a href="{{ $paginator->getUrl($paginator->getCurrentPage()-1) }}">
                        <i class="fa fa-angle-double-left"></i>
                    </a>
                </li>
                <?php
                for ($i = ($numberPages - ($interval - 1)); $i <= $numberPages; $i++)
                {

                    ?>
                    <li class="<?php echo $i == $currentPage ? ' active' : ''; ?>">
                        <a href="<?php echo $paginator->getUrl($i); ?>" ><?php echo $i; ?></a>
                    </li>
                    <?php
                }
            }
            else
            {

                ?>
                <li class="prev">
                    <a href="{{ $paginator->getUrl($paginator->getCurrentPage()-1) }}">
                        <i class="fa fa-angle-double-left"></i>
                    </a>
                </li>
                <?php
                for ($i = ($currentPage - 1); $i <= ($currentPage + 1); $i++)
                {

                    ?>
                    <li class="<?php echo $i == $currentPage ? ' active' : ''; ?>">
                        <a href="<?php echo $paginator->getUrl($i); ?>" ><?php echo $i; ?></a>
                    </li>
                    <?php
                }
            }
        }
        if ($paginator->getLastPage() > $paginator->getCurrentPage())
        {

            ?>
            <li class = "next"><a href = "{{ $paginator->getUrl($paginator->getCurrentPage()+1) }}" class = "{{ ($paginator->getCurrentPage() == $paginator->getLastPage()) ? ' disabled' : '' }}">
                    <i class = "fa fa-angle-double-right"></i>
                </a></li>
            <?php
        }

        ?>
    </ul>
    <?php
}

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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