【问题标题】:Laravel Pagination with row count具有行数的 Laravel 分页
【发布时间】:2019-07-16 06:52:00
【问题描述】:

如何在表格中添加每一行的行号?我使用了分页,但发现很难显示行数。在第 2 页,它应该从 6 开始,因为我的分页是每页 5。

编辑

还有如何在表格上方正确显示showing 1 to 5 of 2095这个词?

控制器

class SchoolController extends Controller
{
    function index()
    {
        $q = Input::get('q');
        if ($q != "") {
            $schools = LibSchool::where('school_name', 'LIKE', '%' . $q . '%')
                ->orWhere('school_id', 'LIKE', '%' . $q . '%')
                ->orWhere('address', 'LIKE', '%' . $q . '%')
                ->orWhere('school_head', 'LIKE', '%' . $q . '%')
                ->orWhere('level', 'LIKE', '%' . $q . '%')
                ->orderBy('school_name', 'asc')
                ->with('libdistrict')
                ->with('libdistrict.libdivision')
                ->paginate(5)->appends('q', $q);
        } else {
            $schools = LibSchool::with('libdistrict')
                ->orderBy('school_name', 'asc')
                ->with('libdistrict.libdivision')
                ->paginate(5);
        }
        return view('schools.index')->with('data', ['schools' => $schools, 'q' => $q]);
    }
}

查看

<div class="card-body">
        Showing 1 to 15 of {{$data['schools']->total()}}
        <table class="table table-hover table-bordered table-striped">
            <tr>
                <th>#</th>
                <th>Division</th>
                <th>District</th>
                <th>School ID</th>
                <th>School NAME</th>
                <th>Address</th>
                <th>School Head</th>
                <th>Level</th>

            </tr>
            @if($data['schools']->total() > 0)
            @foreach($data['schools'] as $school)
            <tr>
                <td></td>
                <td>{{ $school->libdistrict ? $school->libdistrict->libdivision->div_name : 'N/A' }}</td>
                <td>{{ $school->libdistrict ? $school->libdistrict->district_name : 'N/A' }}</td>
                <td>{{$school->school_id}}</td>
                <td>{{$school->school_name}}</td>
                <td>{{$school->address}}</td>
                <td>{{$school->school_head}}</td>
                <td>{{$school->level}}</td>

            </tr>
            @endforeach
            @else
            <td colspan="7" class="text-danger">No records</td>
            @endif
        </table>
        {{$data['schools']->links()}}
    </div>

【问题讨论】:

    标签: laravel pagination


    【解决方案1】:

    试试这个,

    <td>{{ ($data['schools']->currentPage()-1) * $data['schools']->perPage() + $loop->index + 1 }}</td>
    

    当前页面给出页码,$loop 变量给出循环中当前项目的索引,以便您计算编号。

    您可以像这样获得 2095 年的 1 到 5 的显示:

    Showing {{($data['schools']->currentPage()-1)* $data['schools']->perPage()+($data['schools']->total() ? 1:0)}} to {{($data['schools']->currentPage()-1)*$data['schools']->perPage()+count($data['schools'])}}  of  {{$data['schools']->total()}}  Results
    

    希望你能理解。

    【讨论】:

    • 感谢这有效。我只需要添加1。但是showing 1 to 5 of 2092的显示是如何正确完成的?
    • @beginner 我已经更新了我的答案试试看。如有任何问题,请随时询问。
    • @beginner 太棒了!
    【解决方案2】:

    使用$loop变量和当前页计算行号。

    <td>{{ ($data['schools']->currentPage() - 1) * $data['schools']->perPage() + $loop->iteration }}</td>
    

    计算是当前页减一,乘以页面大小(计算以前的页数),再加上当前页的索引。

    【讨论】:

    • 我加1可以吗,因为它是从零开始的。 &lt;td&gt;{{ ($data['schools']-&gt;currentPage() - 1) * $data['schools']-&gt;perPage() + $loop-&gt;index + 1 }}&lt;/td&gt;
    • @beginner $loop->索引从零开始,所以你可以加一进行实数编号。
    • 你也可以帮助我showing 1 to 5 of 2095吗?
    • 使用$loop-&gt;iteration 而不是$loop-&gt;index。它从 1 开始。
    • 使用$loop-&gt;iteration确实会从一个开始,我已经更新了答案。
    【解决方案3】:

    要对集合的项目进行编号并在其上添加数据,就像在控制器中执行 foreach 循环一样简单,那么您在视图上始终拥有数字(顺便说一下,这些数字不会存储到数据库中)

    $i = 1;
    foreach ($schools as $item)
    {
    $item->setAttribute('number', $i);
    $i++;
    }
    

    在视图行上

    {{$item->number}}
    

    【讨论】:

    • 这比其他答案更快吗?
    • 不确定,但我认为它简单实用
    【解决方案4】:

    由于以上所有答案都缺少开箱即用的方法

    我的答案适用于 Laravel 5.0 及更高版本

    我当前的版本5.8.28

    您不需要任何模糊计算

    试试这个

    showing {{$data['schools']->firstItem()}} to {{$data['schools']->lastItem()}} of {{$data['schools']->total()}}
    

    更多https://github.com/laravel/framework/blob/5.8/src/Illuminate/Pagination/LengthAwarePaginator.php#L163

    EDIT FOR 获取行号的方法

    方法一

    @forelse ($data['schools'] as $school)
    {{-- Just Add iteration property of $loop Object --}}
        <td>{{$loop->iteration}}</td>
    {{-- All Your Fileds Goes Here --}}
    @empty
        <td colspan="7" class="text-danger">No records</td>
    @endforelse
    

    但它不会按预期工作,因为如果您更改页面,iteration 属性将从数字 1 开始

    所以

    方法二

    {{ ($data['schools']->currentpage()-1) * $data['schools'] ->perpage() + $loop->index + 1 }}
    

    如果你更喜欢短方法

    {{($data['schools']->firstItem() + $loop->index)}}
    

    它会给你实际的序列号

    如果出现问题,请发表评论

    【讨论】:

    • 除了其他答案给出的样本之外,还有其他方法可以获取行号吗?
    • 行号是什么意思??你在说序列号吗??
    • 每一行对应的数字。就像上面截图中的1、2、3、4和5一样。
    【解决方案5】:

    在您的控制器中:

    $users = User::all()->paginate(3);
    return view('/', compact('users'));
    

    在你的刀片中

    @foreach($users as $key => $user)
    <tr>
           <td>{{ $users->firstItem() + $key }}</td>
           <td>{{ $user->name }}</td>
           <td>{{ $user->email }}</td>
    </tr>
    @endforeach
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-27
      • 1970-01-01
      • 2020-02-02
      • 2011-08-05
      • 1970-01-01
      • 2015-04-14
      • 1970-01-01
      • 2016-08-29
      相关资源
      最近更新 更多