简体   繁体   English

Laravel的分页渲染未在ajax请求上传递变量(无限滚动)

[英]Laravel's pagination rendering not passing variables on ajax request (infinite scrolling)

I'm trying my hand at infinite scrolling ( based on this tutorial ) of some user-uploaded images on website. 我正在尝试无限滚动( 基于本教程 )网站上一些用户上传的图像。 The infinite scrolling works just fine when I use ALL the images on my website, however when I refine the query, things start to get buggy. 当我使用网站上的所有图像时,无限滚动效果很好,但是当我优化查询时,事情开始变得麻烦了。

For testing purposes, I have 12 images. 出于测试目的,我有12张图像。 They all have a "categorie" field, titles, etc. 它们都有一个“类别”字段,标题等。

These images are titled: vector1, vector2, vector3, drawing1, drawing2, drawing3, drawing4, interface1, interface2, interface3, interface4, interface5, and they all have their appropriate categories. 这些图像的标题为:vector1,vector2,vector3,drawing1,drawing2,drawing3,drawing4,interface1,interface2,interface3,interface4,interface5,并且它们都有各自适当的类别。

So when I do this: 所以当我这样做时:

Controller 控制者

$query = $request->input('query');
$images = Status::where('is_image', 1)
->where('categorie', $query)
->where($orderByString, '>' , 0)
->whereIn('is_mature', [0, $mature])
->where('created_at', '>=', Carbon::now()->subHours($withinHours))
->orderBy($orderByString, 'desc')
->Paginate(2);

if($request->ajax()) {
    return [
        'images' => view('browse.partials.fullview_partial')->with(compact('images'))->render(),
        'next_page' => $images->nextPageUrl()
    ];
}       
return view('browse.fullview_results', ['categorie' => $categorie, 'orderBy' => $orderBy, 'orderText' => $orderText, 'orderURL' => $orderURL, 'queryString' => $queryString, 'withinText' => $withinText, 'withinURL' => $withinURL  ])->with('images', $images);

Blade

@if ($images->count())
<div class="endless-pagination" data-next-page={{ $images->nextPageUrl() }}>
    @foreach ($images as $status)
        <h4>{{ $status->title }}</h4>   
    @endforeach
    {{--{!! $images->render() !!}--}}
</div>
@endif

I get 2 images. 我得到2张图片。 In this case, drawing4 and drawing3 (since my query is "drawing"), ordered in the way that I want. 在这种情况下,drawing4和drawing3(因为我的查询是“ drawing”),以我想要的方式排序。 Great! 大!

So now I want to scroll down the page to trigger my ajax call to fetch some more images. 因此,现在我想向下滚动页面以触发我的Ajax调用以获取更多图像。

$(window).scroll(fetchImages);

function fetchImages() {

    var page = $('.endless-pagination').data('next-page');

    if(page !== null) {

        clearTimeout( $.data( this, "scrollCheck" ) );

        $.data( this, "scrollCheck", setTimeout(function() {
            var scroll_position_for_images_load = $(window).height() + $(window).scrollTop() + 900;

            if(scroll_position_for_images_load >= $(document).height()) {
                $.get(page, function(data){
                    $('.endless-pagination').append(data.images);
                    $('.endless-pagination').data('next-page', data.next_page);
                });     
            }
        }, 350))

    }
}   

this gets the partial fullview_partial.blade.php : 这将获得部分的fullview_partial.blade.php

@foreach ($images as $status)
    <h4>{{ $status->title }}</h4>   
@endforeach

and... oops. 和...哎呀。 Nothing shows up. 什么都没有出现。 That's weird. 这很奇怪。 I only have drawing4 and drawing3. 我只有drawing4和drawing3。

I go back to my controller and I switch 我回到控制器,然后切换

->where('categorie', $query)

to

->where('categorie', "drawing")

and test it again. 并再次测试。 This time, I get drawing4 and drawing3. 这次,我得到了drawing4和drawing3。 I scroll down and get drawing2 and drawing1. 我向下滚动并获得drawing2和drawing1。

From what I've concluded (and I might be wrong), it appears that on the ajax call, rather than using the same query, it goes through my controller AGAIN, but this time with no query (since the query comes from the url). 从我得出的结论(可能是错误的)来看,似乎在ajax调用中,而不是使用相同的查询,它通过了我的控制器AGAIN,但是这次没有查询(因为查询来自url) )。 This causes me major issues, as I have plenty of conditions dependent on what the query is. 这给我带来了主要问题,因为我有很多条件取决于查询的内容。

How would I solve this problem? 我该如何解决这个问题?

EDIT 编辑

Upon further investigation, it appears that on the ajax append, it does go through my controller once again, but this time without the query. 经过进一步调查,似乎在ajax附加项上,它确实再次通过了我的控制器,但是这次没有查询。 I think all I would need is a way to pass through that variable once again into the controller to make this work, but I don't know how. 我想我所需要的只是一种将变量再次传递到控制器中以使其工作的方法,但是我不知道如何做。

EDIT 2 编辑2

So essentially all I need is just to turn this: 因此,基本上我只需要打开此按钮即可:

 data-next-page={{ $images->nextPageUrl() }}

into 进入

 data-next-page={{ $images->nextPageUrl() + query }}

somehow. 不知何故。 At least that'll solve the problem for the first call. 至少可以解决第一个电话的问题。 Then I'd need to add the query to 然后我需要将查询添加到

$('.endless-pagination').data('next-page', data.next_page);

Figured it out. 弄清楚了。

Passed $query into the view along with everything else. 将$ query以及其他所有内容传递到视图中。

return view('browse.fullview_results', ['categorie' => $categorie, 'orderBy' => $orderBy, 'orderText' => $orderText, 'orderURL' => $orderURL, 'queryString' => $queryString, 'withinText' => $withinText, 'withinURL' => $withinURL  ])->with('images', $images)->with('query', $query);

and also added it to data-next-page: 并将其添加到data-next-page:

data-next-page={{ $images->nextPageUrl() }}&query={{$query}}

so the URL for the next GET request looks like: 因此下一个GET请求的网址如下所示:

fullview?page=2&query=drawing

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM