【问题标题】:laravel 5 custom paginator not workinglaravel 5自定义分页器不起作用
【发布时间】:2015-08-25 16:09:30
【问题描述】:

我正在尝试实现一个类似于 sphinx 文档的视图,您可以在其中单击下一个和上一个以在页面之间导航。不确定我是否在数组右侧使用自定义分页器,我无法让页面导航链接显示在视图页面上。这是代码:

   public function paginate($array, $perPage, $pageStart=1) {

    $offset = ($pageStart * $perPage) - $perPage;

    return new Paginator(array_slice($array, $offset, $perPage, true), $perPage, $pageStart);
   }

   view()->composer('layouts.book', function($view)
   {
        //some other code
        $pages = [];
        foreach($book->textsection_pages as $textsection) {
            $pages[] = $textsection->alias;
        }
        foreach($book->task_pages as $task) {
            $pages[] = $task->alias;
        }
        foreach($book->tutor_pages as $tutor) {
            $pages[] = $tutor->alias;
        }
        foreach($book->eval_pages as $eval) {
            $pages[] = $eval->alias;
        }

        $chapters = $book->chapters()->orderBy('weight', 'asc')->get();
        $paginated = $this->paginate($pages, 1);
        $view->with(['chapters' => $chapters, 'book' => $book, 'paginated' => $paginated]);

    });

在视图中我调用了{!! $paginated->render() !!},但没有显示导航链接。

【问题讨论】:

  • 检查一下 laravel 5 api,Paginator 的构造函数是__construct(mixed $items, int $total, int $perPage, int|null $currentPage = null, array $options = array())
  • 是的,我做到了。它的__construct($items, $perPage, $currentPage = null, array $options = [])
  • 啊,我知道这是因为您在闭包中调用 paginate() 方法,而 $this 没有引用该类。
  • 哦,我明白了。你会如何在闭包中调用paginate() 方法?

标签: php laravel pagination laravel-5


【解决方案1】:

这是我在应用程序(Laravel 5.1)中处理自定义分页的方式。我创建了一个局部视图resources/views/partials/pagination.blade.php

@if ($paginator->lastPage() > 1)
<ul id="pagination">
    <li>
    @if ($paginator->currentPage() > 1)
        <a class="prev" href="{{ $paginator->url($paginator->currentPage()-1) }}">Previous</a>
    @else
        <span class="disabled">Previous</span>
    @endif
    </li>

    <li>
    @if ($paginator->currentPage() !== $paginator->lastPage())
        <a class="next" href="{{ $paginator->url($paginator->currentPage()+1) }}" >Next</a>
    @else
        <span class="disabled">Next</span>
    @endif
    </li>
</ul>
@endif

在我的控制器中,我通过了通常的$model-&gt;paginate(10) 方法。最后在我看来,我想在哪里使用 Paginator,resources/views/list.blade.php 我这样做:

@foreach ($objects as $object)
    // Here we list the object properties
@endforeach
@include("partials.pagination", ['paginator' => $objects])

【讨论】:

  • 嘿,您仍在使用模型的内置分页器。我想要一个适用于集合或数组的分页器。
猜你喜欢
  • 1970-01-01
  • 2015-06-09
  • 2021-08-02
  • 1970-01-01
  • 2022-11-16
  • 2015-05-12
  • 2015-05-28
  • 1970-01-01
  • 2011-08-13
相关资源
最近更新 更多