【问题标题】:Laravel show more buttonLaravel 显示更多按钮
【发布时间】:2018-04-28 01:15:13
【问题描述】:

我确实有两个 laravel 集合

$posts = collect($instagramService->fetchPosts('username'))->take(10); 

还有一些帖子

$morePosts =  collect($instagramService->fetchPosts('username'))->slice(10);

我正在刀片上显示第一个集合:

@foreach($posts as $post)
   {{$post->url}}
   {{$post->likes}}
@endforeach

<button type="submit">Show More</button>

我正在尝试研究 ajax 分页,但到目前为止还没有运气,所以如果有人可以在这里帮助我,将不胜感激。

问题:当我点击“显示更多”时,我想加载第二个集合?

【问题讨论】:

    标签: php ajax laravel pagination


    【解决方案1】:

    按钮

    <button type="submit" class="clickMe">Show More</button>
    

    ajax

           $(".clickMe").click(function(){
        $.ajax({
                dataType: 'json',
                type:'GET',
                url: "{{ url('morePosts') }}",
              }).done(function(data){
                 var len=data.length
    console.log(len);
        //Perform ANy action after successfuly post data
          var rows = '';
    for(i=0;i<data.length;i++){
          rows = rows + 'data[i].url'
    }      
     $("#morePosts").html(rows);  
            });
    

    路线

     Route::get('/morePosts', 'PostController@morePosts');
    

    控制器中的方法

    public function morePosts(){
       $data=  collect($instagramService->fetchPosts('username'))->slice(10);
    return response()->json($data);
    }
    

    html

    <div id="morePosts">
    </div>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-01
      • 2021-02-15
      • 2020-06-19
      相关资源
      最近更新 更多