避免在Laravel分页时刷新页面

时间:2019-10-15 13:06:53

标签: javascript php html ajax laravel

当我从Laravel使用分页切换页面时,我想避免刷新页面,因为当我将页面1切换到页面2时,我将重定向到父页面。更改页面时如何避免重新加载页面?我在Invitation-ReceivedInitiated上有2个标签。在Initiated上,我有分页,例如,当我按第2页时,我被重定向到Invitation的父页面,当我进入时,我需要保留在页面Initiated上从分页切换页面。 这是我的控制器:

$initiated =  \App\Invitation::with('user')
    ->where('inviter_id', Sentinel::getUser()->id)
    ->orderBy('id', 'desc')
    ->paginate(6);

这是我的观点:

<div id="initiated" class="profile-edit tab-pane fade" style="background: none repeat scroll 0 0;">
    <dl class="dl-horizontal">
        <ul class="nav justify-content-center u-nav-v5-3 u-nav-primary  " role="tablist"
            data-target="nav-5-3-primary-hor-center" data-tabs-mobile-type="slide-up-down"
            data-btn-classes="btn btn-md btn-block u-btn-outline-primary"
            style="display: flex;justify-content: flex-end;">
            <li class="nav-item active">
                <a a data-toggle="tab" href="#grid" style="font-size: 12px"><i class="fa fa-th-large"></i> Grid</a>
            </li>
            <li class="nav-item">
                <a a data-toggle="tab" href="#list" style="font-size: 12px"><i class="fa fa-bars"></i> List</a>
            </li>
        </ul>
        <div class="tab-content">
            <div id="grid" class="tab-pane fade in active">
                <?php $count_user = 0;
         ?>
                @foreach($initiated as $record)
                @if($count_user == 0)
                <div class="row margin-bottom-20 ">
                    @endif
                    <?php $count_user++; ?>
                    <div class="col-sm-6 sm-margin-bottom-20">
                        <div class="profile-blog">
                            <img style="border-radius: 0% !important" class="rounded-x"
                                src="{{ asset('thumbnail') }}/{{ $record->user ? $record->user->profile_picture : '' }}"
                                alt="">
                            <div class="name-location" style="top: 30px;position: relative">
                                @if($record->user->role[0]->slug == 'individuals' ? $record->user->role[0]->slug : '')
                                <i style="font-size: 13px;position: relative;" class="icon-user"></i>
                                @elseif($record->user->role[0]->slug == 'organizations' ? $record->user->role[0]->slug :
                                '')
                                <i style="font-size: 13px;position: relative;"
                                    class="icon-hotel-restaurant-172 u-line-icon-pro fa- fa-lg"></i>
                                @endif
                                <a id="my-link" style="color: #555;text-decoration: none;font-size: 16px"
                                    href="{{ url('') }}/{{ $record->user ? $record->user->username : '' }}">{{ $record->user ? $record->user->username : '' }}</a>
                                </h3>
                            </div>
                            <div class="clearfix "></div>
                            <strong style="font-size: 13px"><i style="padding: 5px"
                                    class="icon-real-estate-020 u-line-icon-pro"></i>Location :
                            </strong>{{ $record->user ? $record->user->country->country : '' }} <span
                                class="{{ $record->user ? $record->user->country->flag : '' }}"></span><br>
                            <strong style="font-size: 13px"><i style="padding: 5px"
                                    class="icon-notebook fa-"></i>Industry :
                            </strong>{{ $record->user ? $record->user->industry->industry : '' }}<br>
                            @if($record->user->role[0]->slug == 'organizations' ? $record->user->role[0]->slug : '')
                            @if(isset($record->user->organization_type->organization_type))
                            <strong style="font-size: 13px"><i
                                    class="icon-hotel-restaurant-172 u-line-icon-pro fa- fa-lg"
                                    style="padding: 5px;font-size: 13px"></i>Organization :
                            </strong>{{ $record->user ? $record->user->organization_type->organization_type : '' }}@endif
                            @endif
                            @if($record->user->role[0]->slug == 'individuals' ? $record->user->role[0]->slug : '')
                            @if(!empty($record->user->career_path[0]))
                            <strong style="font-size: 13px"><i class="icon-speedometer"
                                    style="padding: 5px"></i>Function :
                            </strong>{{ $record->user ? $record->user->career_path[0]->functions->function : '' }}@endif
                            @endif
                            <hr>
                            <ul class="list-inline share-list">
                                <li>
                                    <a href="/cancel-invitation/{{$record->id}}"><i class="fa fa-times"> </i>Cancel</a>
                                </li>
                            </ul>
                        </div>
                    </div>
                    @if($count_user == 2)
                </div>
                <?php  $count_user = 0; ?>
                @endif
                @endforeach
            </div>
            <div id="list" class="tab-pane fade">
                <?php $count_user = 0;
         ?>
                @foreach($initiated as $record)
                @if($count_user == 0)
                <div class="row">
                    @endif
                    <?php $count_user++; ?>
                    <div class="col-sm-12 sm-margin-bottom-20" style="margin-bottom: 12px">
                        <div class="profile-blog" style="padding: 5px">
                            <img style="margin-right: 10px"
                                src="{{ asset('thumbnail') }}/{{ $record->user ? $record->user->profile_picture : '' }}"
                                alt="">
                            <div class="name-location">
                                <h3> @if($record->user->role[0]->slug == 'individuals' ? $record->user->role[0]->slug :
                                    '')
                                    <i style="font-size: 13px;position: relative;" class="icon-user"></i>
                                    @elseif($record->user->role[0]->slug == 'organizations' ?
                                    $record->user->role[0]->slug : '')
                                    <i style="font-size: 13px;position: relative;"
                                        class="icon-hotel-restaurant-172 u-line-icon-pro fa- fa-lg"></i>
                                    @endif <a id="my-link" style="color: #555;text-decoration: none;font-size: 16px"
                                        href="{{ url('') }}/{{ $record->user ? $record->user->username : '' }}">{{ $record->user ? $record->user->username : '' }}</a>
                                </h3>
                                <ul class="list-inline share-list d-flex pull-right"
                                    style="bottom: 25px;position: relative;margin-bottom: -20px;left: -25px">
                                    <li style="left: 25px;position: relative">
                                        <a href="/cancel-invitation/{{$record->id}}"><i
                                                class="icon-custom-me rounded fa fa-times"> </i></a>
                                    </li>
                                </ul>
                            </div>
                            <div style="display: flex;right: 2px;position: relative;bottom: 12px;margin-bottom: -10px">
                                <div style="padding-right: 15px">
                                    <strong><i class="icon-real-estate-020 u-line-icon-pro"></i> :
                                    </strong>{{ $record->user ? $record->user->country->country : '' }} <span
                                        class="{{ $record->user ? $record->user->country->flag : '' }}"></span>
                                </div>
                                <div style="padding-right: 10px">
                                    <strong><i class="icon-screen-tablet fa-"></i> :
                                    </strong>{{ $record->user ? $record->user->industry->industry : '' }}
                                </div>
                                <div style="padding-right: 10px">
                                    @if($record->user->role[0]->slug == 'organizations' ? $record->user->role[0]->slug :
                                    '')
                                    @if(isset($record->user->organization_type->organization_type))
                                    <strong><i class="icon-speedometer"></i> :
                                    </strong>{{ $record->user ? $record->user->organization_type->organization_type : '' }}
                                    @endif
                                    @endif
                                </div>
                                <div style="padding-right: 10px">
                                    @if($record->user->role[0]->slug == 'individuals' ? $record->user->role[0]->slug :
                                    '')
                                    @if(!empty($record->user->career_path[0]))
                                    <strong><i class="icon-frame fa-"></i> :
                                    </strong>{{ $record->user ? $record->user->career_path[0]->functions->function : '' }}
                                    @endif
                                    @endif
                                </div>
                            </div>
                            </ul>
                        </div>
                    </div>
                    @if($count_user == 2)
                </div>
                <?php  $count_user = 0; ?>
                @endif
                @endforeach
            </div>
        </div>
    </dl>
    {!! $initiated->render() !!}
</div>

2 个答案:

答案 0 :(得分:1)

服务器端分页需要更改路由参数,因此不是“页面刷新”

您可以使用Ajax对分页按钮编号单击进行手动查询

这样的事情(不推荐)

<div id="users">
    @foreach ($users as $user)
    <h2>{{ $user->name }}</h2>
    @endforeach
</div>
{{ $users->links() }}

并使用JQuery Ajax

$('.pagination a').on('click', function (e) {
    e.preventDefault();
    var url = $(this).attr('href');
    $.get(url, function (data) {
        $('html').html(data.replace(/<html>(.*)<\/html>/, "$1"));
    });
});

这是尝试做TurboLink的目的,所以我建议改用它

或者使用Vue路由器使整个应用成为SPA,例如,如果您使用的是Vue

但是最简单的解决方案是使用Turbo Links

这将拦截页面重定向并进行XHR捕获,以获取整个HTML laravel返回并将其与当前视图交换

希望这会有所帮助

答案 1 :(得分:0)

首先,我认为您应该知道分页的工作原理。

@foreach ($initiated as $record)
    ....
@endforeach

{{ $initiated->appends(array_merge(request()->all()))->links() }}

通常您只能执行{{ $initiated->links() }},但是当您需要传递特殊参数时,还应该将请求参数合并到分页中,以便分页所具有的每个链接都将具有您需要的参数

您可以在Appending To Pagination Links部分下找到here的更多文档。如果实现此分页,则将不再获得父级重定向。 我还看到您在两个选项卡中使用相同的数据执行相同的foreach循环,并且我认为这也是一个问题,因为您不能对同一数据进行2个不同的分页。也许在控制器中您可以执行类似的操作

$initiated_tab_1 = ... ;
$initiated_tab_2 = $initiated_tab_1;

这不是一个好的解决方案,但是也许您可以尝试一下,看看这是否会改变您的代码工作方式。另外,您还需要调整分页符

我希望这能对您有所帮助。