【问题标题】:increment row number with laravel pagination使用 laravel 分页增加行号
【发布时间】:2013-08-04 03:46:57
【问题描述】:

如何使用 laravel 分页增加行号?当我使用分页并转到第 2 页及以上时,它将回到开头。例如我将分页(3)

    <thead>
    <tr>
       <th>No</th>
       <th>Name</th>
    </tr>
    </thead>
    <tbody>
    <?php $i = 1; ?>
    @foreach ($telephone->results as $telp)
    <tr>
       <td>
         {{$i++}}
       </td>
       <td>{{ $telp->name }}</td>                               
    </tr>
    @endforeach
    </tbody>

当我转到第 2 页时,数字将再次从 1 开始。

当我转到第 2 页时,它会从第 4 页开始

【问题讨论】:

    标签: laravel laravel-3


    【解决方案1】:

    在 Laravel 5.3 中你可以使用firstItem():

    @foreach ($items as $key => $value)    
      {{ $items->firstItem() + $key }}
    @endforeach
    

    【讨论】:

      【解决方案2】:

      以下适用于 laravel 5.4

      <?php $i = ($telephone->currentpage()-1)* $telephone-
      >perpage() + 1;?>
      @foreach($telephone as $whatever)
      <td> {{ $i++ }}</td>
      @endforeach
      

      已编辑 以下适用于以上 laravel 5.7

      @foreach ($telephone as $key=> $whatever)
        <td>{{ $key+ $telephone->firstItem() }}</td>
      @endforeach
      

      【讨论】:

      • 这很有帮助
      【解决方案3】:

      您应该可以使用getFrom 方法获取当前页面结果的起始编号。因此,您应该能够做到这一点,而不是设置 $i = 1;

      <?php $i = $telephone->getFrom(); ?>
      

      在 Laravel 3 中没有 getFrom 方法,所以你需要手动计算。

      <?php $i = ($telephone->page - 1) * $telephone->per_page + 1; ?>
      

      【讨论】:

      • 感谢您的关注。顺便说一句,当我使用 getForm 方法时,我收到错误 Call to undefined method Laravel\Paginator::getFrom()
      • 抱歉,假设为 L4。我已更新我的答案以包含 L3 变体。
      【解决方案4】:

      Laravel 5.3

      @foreach ($products as $key=>$val)
        {{ ($products->currentpage()-1) * $products->perpage() + $key + 1 }}
      @endforeach
      

      【讨论】:

        【解决方案5】:

        对于 Laravel 6.2: $loop - 以防万一,是 Blade 中的内置实例

        @foreach($array as $item)
            <tr class="table-row">
              <td class="site-id">
                 {{($array->currentPage() - 1) * $array->perPage() + $loop->iteration}}
              </td>
            </tr>
        @endforeach
        </table>
        

        【讨论】:

          【解决方案6】:

          您可以简单地添加以下行

          $i = ($telephone->currentpage()-1)* $telephone->perpage();
          

          代替

          $i = 1;
          

          【讨论】:

            【解决方案7】:
            @php($sl = ($yourVariable->perPage() * $yourVariable->currentPage()) - ($yourVariable->perPage() - 1))
            
            @foreach($yourVariable as $item)
            
            <td>{{ $item->key_name }}</td>
            
            .....
            
            @php($sl++)
            
            @endforeach
            

            【讨论】:

            • 你应该解释答案
            【解决方案8】:

            在 Laravel 6 中

            
            @php $i = ($data->currentpage()-1)* $data->perpage() + 1;@endphp
            
            @foreach($data as $banner)
             <tr>
               <td>{{$i}}</td>
               <td><img src="{{ URL::to('/') }}/dist/img/{{$banner->name}}" height="250" width="600"></td>
             </tr>
             @php  $i += 1; @endphp
            @endforeach
            

            【讨论】:

              【解决方案9】:

              或者完全避开php标签

               @foreach ($users as $key => $user)
                  {{ (Input::get('page', 1) - 1) * $users->getPerPage() + $key + 1 }}
               @endforeach
              

              【讨论】:

                【解决方案10】:

                如果你使用 Laravel 分页。效果很好

                后端

                public function index()
                {
                    return view('your.telephone.index', [
                        'telephone' => Telephone::paginate(15)
                    ]);
                }
                

                刀片前端

                <td>{{ (($telephone->currentPage() * 10) - 10) + $loop->iteration  }}</td>
                

                别忘了嵌入页面

                {{ $telephone->links() }}
                

                对于$loop-&gt;iteration

                在此处查看文档:https://laravel.com/docs/7.x/blade#the-loop-variable

                $telephone-&gt;currentPage()

                在此处查看文档:https://laravel.com/docs/7.x/pagination#paginator-instance-methods

                【讨论】:

                  【解决方案11】:

                  您可以在控制器中使用它。下面给出的例子

                  $records = Table::paginate(20);
                  return view('yourview',compact('records')->with('i', ($request->input('page', 1) - 1) * 20);
                  

                  注意:分页值必须等于with()函数值

                  之后,您可以在刀片文件中使用 $i 变量。控制器根据页码计算值并返回刀片

                  在刀片文件中。在循环内使用

                  {{ ++$i }}
                  

                  希望对你有帮助

                  【讨论】:

                    【解决方案12】:

                    Laravel 8++

                    $loop-&gt;iteration 在循环内开箱即用。

                    {{ ($barangs->currentPage() - 1)  * $barangs->count() + $loop->iteration }}
                    

                    【讨论】:

                      【解决方案13】:

                      我在 Laravel 8 中使用它,访问 $loop 和 links()->paginator:

                             @foreach ($users as $user)
                                  <tr>
                                      <td class="text-center">{{ ($users->currentPage() - 1)  * $users->links()->paginator->perPage() + $loop->iteration }}</td>
                                      <td class="text-center">{{$user->name}}</td>
                                  </tr>
                              @endforeach
                      

                      即使最后一页仅部分填满,也能正常工作。

                      也可以使用 $users->perPage() 代替 $users->links()->paginator->perPage()

                      【讨论】:

                        猜你喜欢
                        • 2016-05-31
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 2016-07-15
                        • 1970-01-01
                        • 2014-09-01
                        • 2015-01-01
                        相关资源
                        最近更新 更多