【问题标题】:Multiple pagination in a tab content选项卡内容中的多个分页
【发布时间】:2018-10-17 06:30:05
【问题描述】:

我有一个标签字段。这些包含年份。 我需要分别显示所有这些的页码。 比如我有;

全部 - 2018 - 2017 - 2016

2018 选项卡包含 30 个值。 2017 制表符 = 15 个值。 2016 制表符 = 15 个值。 所有选项卡都包含 = 60 值。

当我单击随机选项卡时,分页号必须更改。

标签:

<ul id="tab">
<li>All</li>
<li>2018</li>
<li>2017</li>
</ul>

标签内:

<div id="wrapper">
<div class="contents">
    @foreach($projects as $project)
    <div>{{$project->created_at}}</div>
    <div><img src="{{('/storage/'.$project->image)}}"/></div>
    @endforeach
</div>
<div class="contents">
    @foreach($projects2018 as $project)
        <div>{{$project->created_at}}</div>
        <div><img src="{{('/storage/'.$project->image)}}"/></div>
    @endforeach
</div>
<div class="contents">
    @foreach($projects2017 as $project)
        <div>{{$project->created_at}}</div>
        <div><img src="{{('/storage/'.$project->image)}}"/></div>
    @endforeach
</div>
<div class="contents">
    @foreach($projects2016 as $project)
    <div>{{$project->created_at}}</div>
    <div><img src="{{('/storage/'.$project->image)}}"/></div>
    @endforeach
</div>

我创建了这个.contents 字段。

使用我的脚本代码。选项卡正在发生变化,值正在到来。 这是我的控制器:

$projects = Project::latest()->paginate(50);
    $projects2018 = Project::whereYear('created_at', '=', 2018)->paginate(50);
    $projects2017 = Project::whereYear('created_at', '=', 2017)->paginate(50);
    $projects2016 = Project::whereYear('created_at', '=', 2016)->paginate(50);

在这种情况下,控制器只获得 50 值。之后 js 对这 50 个值进行分页。 10 x 10。我的意思是 5 有 5 页。但是所有值都在同一时间下载。单击时我需要下载页面 2-3-4-5 值。不要同时下载所有值!这很无聊。我不知道怎么做。

这也是我的 javascript 代码。

<script>
$(document).ready(function()
{

    $("#tab").tabpager({
        items: 10,
        contents: 'contents',
        time: 300,
        previous: 'Geri',
        next: 'İleri',
        start: 1,
        position: 'top',
        scroll: true
    });
});

仍然想知道解决方案...

【问题讨论】:

    标签: javascript laravel pagination tabs eloquent


    【解决方案1】:

    Laravel默认使用get参数page解析当前页面,即example.com/?page=3这个请求,分页器会将当前页面解析到第3页。

    这些行为可以改变,当你查看declaration of paginate method时:

    public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null)
    

    您可以创建自己的分页方法,例如,通过更改第三个参数:

    $projects2017 = Project::whereYear('created_at', '=', 2017)
        ->paginate(50, ['*'], 'p2017');
    $projects2016 = Project::whereYear('created_at', '=', 2016)
        ->paginate(50, ['*'], 'p2016');
    

    然后这个请求example.com/?p2017=2&amp;p2016=3会查询2017项目的第二页和2016项目的第三页。

    或另一种方法,输出相同,通过第四个参数显式指定页面:

    $projects2017 = Project::whereYear('created_at', '=', 2017)
        ->paginate(50, ['*'], 'page', 2);
    $projects2016 = Project::whereYear('created_at', '=', 2016)
        ->paginate(50, ['*'], 'page', 3);
    

    【讨论】:

      【解决方案2】:

      补充本的回答。在我们的控制器中添加我们自己的分页参数之后。

      $projects2017 = Project::whereYear('created_at', '=', 2017)
          ->paginate(50, ['*'], 'p2017');
      $projects2016 = Project::whereYear('created_at', '=', 2016)
          ->paginate(50, ['*'], 'p2016');
      

      在我们看来projects.blade.php而不是使用{{$projects2017-&gt;render()}}使用

      {{$projects2017->appends(["p2016" => $projects2016->currentPage()]->links())}}
      

      反之亦然,因此当您在一个选项卡上更改页面时,它会保存另一个选项卡的当前页面,并且链接将类似于 example.com/?p2017=1&amp;p2016=2

      我在 Lavarel 5.7 中尝试过,它有效!!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-12-09
        • 1970-01-01
        • 2019-02-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多