【问题标题】:Avoid refreshing the page at pagination避免在分页时刷新页面
【发布时间】:2020-02-12 20:27:57
【问题描述】:

当我使用 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>

这是我的 Ajax:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script type="text/javascript">
$('.pagination a').on('click', function (e) {
    e.preventDefault();
    var url = $(this).attr('href');
    $('#initiated').load(url + ' div#initiated');
});
</script>

【问题讨论】:

  • 通常的做法包括(1)使用jQuery.AJAX之类的函数来获取下一页的内容区域,然后重绘当前页面的内容区域; (2) 使用前端 javascript 框架 ReactJSVue 重写您的前端,然后使用您编写的一些 API 端点获取分页数据。
  • 你不能单独使用 php 来做到这一点。
  • 嘿,我如何在这个功能上使用 Ajax?因为我使用了一些 Ajax 但没有成功。请检查我更新的代码,我将把我的 Ajax 放在这里。
  • @Andrew,你的 ajax 不工作了吗?
  • @UdoE。不是真的,这个分页仍然令人耳目一新{{ $initiated-&gt;appends(array_merge(request()-&gt;all()))-&gt;links() }}

标签: php html database laravel


【解决方案1】:

您可能希望在您关心的实际元素上使用 jQuery 的 .load(),而不是重写整个 HTML。也许是这样的:

$('.pagination a').on('click', function (e) {
    e.preventDefault();
    var url = $(this).attr('href');
    $('#initiated').load(url + ' div#initiated');
});

如果分页链接被重写,请记住每次使用 AJAX 加载新内容后重新运行此脚本。使用.load() 函数中的complete 参数,您可以这样做:

function ajaxPaging() {
    $('.pagination a').on('click', function (e) {
        e.preventDefault();
        var url = $(this).attr('href');
        $('#initiated').load(url + ' div#initiated', null, ajaxPaging); // re-run on complete
    });
}
ajaxPaging();

更新:由于目标被视为被动,因此无法在被动事件侦听器中阻止默认。

您可能会在 Chrome 上遇到此错误。要修复它,请在调用其他脚本之前尝试此操作 (reference):

(passiveSupported && (active || el == window.document || el == window.document.body || el == window)) ? el.addEventListener(name, fn, { passive: false, capture: bubble }) : el.addEventListener(name, fn, bubble || false);

【讨论】:

  • 嗯,正在部分工作。因为现在,我的页面没有刷新,但是如果我在第 2 页,并且我选择第 1、3 或 4 页,正在加载一个空白页面。请检查这个和我更新的代码。 imgur.com/a/N4CvqVq
  • 对不起。从你的描述我不太了解情况。你的“页面没有刷新”是什么意思?加载后您是否还重新运行了点击脚本?
  • 页面没有重新加载,这就是我想说的,这就是我需要的。 re-run the on click script after load 是什么意思?
  • @Andrew:更新了我的答案。请检查并重试。
  • 不...同样的问题:(当我点击另一个页面时,它正在重新加载一个新标签,但没有内容,就像这些照片中一样:imgur.com/a/N4CvqVq
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-04
  • 1970-01-01
  • 2015-12-21
  • 1970-01-01
  • 2010-10-06
  • 1970-01-01
相关资源
最近更新 更多