【问题标题】:How to insert infinite scroll to Laravel Project如何在 Laravel 项目中插入无限滚动
【发布时间】:2018-11-28 09:42:48
【问题描述】:

我对在 laravel 中使用的 Jquery 和 Ajax 非常陌生,并且一直在尝试对我的项目实现无限滚动,但我不知道从哪里开始

控制器:

$books= DB::table('books')->where('status', 'post')->orderBy('created_at', 'DESC')->paginate(15);

   if ($books->isEmpty())
   {
     $books= null;

     return view('landingpage')->withBooks($books);
   }
   return view('landingpage')->withBooks($books);

景色

   @if ($books== null)

            <center><p class="paragraph"> Be the first to share your shopping experience <a href="{{ route('testimonials.create') }}" style="color:#9e0101;"> here</a></p></center>

        @else


        <div class="row mt-0"> <div class="infinite-scroll"> @foreach ($books as $item)
            <div class="col-lg-4 mt-3">
                <p class="paragraph"> <sup><i class="fa fa-quote-left" style="font-size:5px" aria-hidden="true"></i></sup>{{$item->title}}<sup><i class="fa fa-quote-right" style="font-size:5px" aria-hidden="true"></i></sup> </p>
                <img src="../images/{{$item->rate}}.png" style="width:50%" alt="Image">
            <h2 style="font-size:18px">{{$item->firstname}} {{$item->lastname}}</h2> </div>@endforeach </div>{{$books->links()}}</div>

                @endif

JS

<script type="text/javascript">
$('ul.pagination').hide();
$(function() {
    $('.infinite-scroll').jscroll({
        autoTrigger: true,
        loadingHtml: '<img class="center-block" src="images/loading.gif" alt="Loading..." />',
        padding: 0,
        nextSelector: '.pagination li.active + li a',
        contentSelector: 'div.infinite-scroll',
        callback: function() {
            $('ul.pagination').remove();
        }
    });
});

任何帮助将不胜感激

【问题讨论】:

    标签: laravel infinite-scroll endlessscroll


    【解决方案1】:

    我使用 ajax 进行无限滚动这是示例:

    查看方法:

    public function index(){
            return view('index');
        }
    

    通过ajax获取数据的方法:

    public function ajaxData(Request $request)
        {
            $post_query = Trends::query()
                ->where('is_offer', '!=', 1)
                ->where('direct_to', null)
                ->orderBy('id', 'desc')
                ->paginate(12);
    
            $data['post_list'] = $post_query;
    
    
                if ($data['post_list']->count() > 0) {            
                    $ajax_data['post'] = true;
                    $ajax_data['next_url'] = $data['post_list']->nextPageUrl();
                    $ajax_data['next_data'] = $data['post_list'];
    
                } else {
                    $ajax_data['html'] = false;
                    $ajax_data['post'] = false;
                    $ajax_data['next_url'] = $data['post_list']->nextPageUrl();
                }
                return response()->json($ajax_data);
        }
    

    这是我在刀片中的脚本

    var loaddataUrl = '{!! route('explore-ajax-data',array_merge(request()->all())) !!}';
            var nextData = true;
            $(document.body).on('touchmove', onExploreScroll); // for mobile
            $(window).on('scroll', onExploreScroll);
    
            function onExploreScroll() {
                if ($(window).scrollTop() >= ($(document).height() - $(window).height()) * 0.9) {
                    if (nextData) {
                        nextData = false;
                        loadMoreData();
                    }
                }
            }
            function loadMoreData() {
                $.ajax({
                    type: 'GET',
                    url: loaddataUrl,        
    
                    success: function (data) {                        
                        if (data.next_url) {
                            loaddataUrl = data.next_url;
                            console.log(data.next_data);
                            nextData = true;
                        } else {
                            nextData = false;
                        }
                    },
                    error: function (data) {
                        nextData = true;
                    }
                })
            }
    

    route(explore-ajax-data) 映射到public function ajaxData(Request $request)

    我的路线是:

    // this will display home page or home view
    Route::get('home', 'HomeController@index')->name('home');
    
    // this is for ajax request.
    Route::get('ajax/explore-data', 'HomeController@ajaxData')->name('explore-ajax-data');
    

    link of tutorial

    【讨论】:

    • 我想我明白发生了什么,但我不知道如何将数据附加到视图
    • 在示例中,当 ajax 成功时,我会在控制台 console.log(data.next_data); 中获得输出
    • 您必须循环该数据并附加到任何您想要的地方
    • 我明白这一点,但有没有办法在没有你的 ajaxData 函数的情况下做到这一点,我对 ajax 毫无头绪。目前正在使用资源控制器来获取数据。
    • 我必须做 ajax。
    猜你喜欢
    • 1970-01-01
    • 2018-08-09
    • 1970-01-01
    • 1970-01-01
    • 2016-06-26
    • 1970-01-01
    • 1970-01-01
    • 2016-12-29
    • 1970-01-01
    相关资源
    最近更新 更多