dongou3158 2015-08-25 16:09
浏览 69

laravel 5自定义分页器无法正常工作

im trying to implement a view like sphinx documentation where you can click on next and previous to navigate between pages. not sure if im using the custom paginator on array right, i cant get page navigation links to display on a view page. this is the code:

   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]);

    });

and in the view i called {!! $paginated->render() !!}, but no navigation link was displayed.

  • 写回答

1条回答 默认 最新

  • drg5577 2015-08-25 20:35
    关注

    Here is how I handle custom Pagination in my application (Laravel 5.1). I create a partial view 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
    

    In my controller, I pass the usual $model->paginate(10) method. Then finally in my view where I want to use the Paginator, resources/views/list.blade.php I do this:

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

报告相同问题?

悬赏问题

  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同