【问题标题】:how to custom pagination in laravel?如何在 laravel 中自定义分页?
【发布时间】:2014-05-08 22:17:44
【问题描述】:

我有一个分页视图,但问题是我有这么多项目,像这样

我想要的是显示 1 2 3 ... 6 7 例如我试图这样做但我没有找到任何东西,

这是我的行动:

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'));
}

这是我的看法:

   <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();}}

所以我想尽一切办法做到这一点,如果有人有任何想法,我将非常感激

【问题讨论】:

    标签: php laravel pagination laravel-4 laravel-3


    【解决方案1】:

    您可以使用自己的视图:

    config/view.php

    'pagination' => 'my-pagination',
    

    查看/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
    }
    
    ?>
    

    【讨论】:

      【解决方案2】:

      默认情况下,Laravel 将使用“滑动”页码,但是,它需要至少 13 页才能创建“滑动”页码。如果您的页面少于 13 页,则默认为普通范围的页面。

      不幸的是,这个数字是硬编码到 Laravel 中的。

      请参阅负责构建页面的 Presenter 类中的 this comment (v4.1.24)

      【讨论】:

      • 是的,它适用于超过 13 个,对我来说已经足够了,谢谢 Jason,我真的很感谢你 2 个答案 :)
      猜你喜欢
      • 1970-01-01
      • 2016-09-21
      • 2021-08-22
      • 1970-01-01
      • 2015-06-09
      • 2017-02-28
      • 1970-01-01
      • 2016-07-01
      • 1970-01-01
      相关资源
      最近更新 更多