当前位置:首页 > CMS教程 > 其它CMS > 列表

Laravel 简单实现Ajax滚动加载示例

发布:smiling 来源: PHP粉丝网  添加日期:2022-01-15 19:07:58 浏览: 评论:0 

今天小编就为大家分享一篇Laravel 简单实现Ajax滚动加载示例,具有很好的参考价值,希望对大家有所帮助,一起跟随小编过来看看吧。

开发H5项目的时候我们总是需要用到下拉滚动刷新的方式加载页面,这里用 Laravel 实现一下,直接上代码:

创建模型

这里我们不妨创建一个 文章(Post)模型, 并且生成测试数据 50 条吧。

php artisan make:model -m

模型Post.php

  1. namespace App; 
  2.  
  3. use Illuminate\Database\Eloquent\Model; 
  4.  
  5. class Post extends Model 
  6.  
  7.  public $fillable = ['title','description']; 
  8.  
  9.    

迁移文件

  1. use Illuminate\Database\Schema\Blueprint; 
  2. use Illuminate\Database\Migrations\Migration; 
  3.  
  4. class CreatePostTable extends Migration 
  5.  /** 
  6.   * Run the migrations. 
  7.   * 
  8.   * @return void 
  9.   */ 
  10.  public function up() 
  11.  { 
  12.   Schema::create('posts'function (Blueprint $table) { 
  13.    $table->increments('id'); 
  14.    $table->string('title'); 
  15.    $table->text('description'); 
  16.    $table->timestamps(); 
  17.   }); 
  18.  } 
  19.  
  20.  /** 
  21.   * Reverse the migrations. 
  22.   * 
  23.   * @return void 
  24.   */ 
  25.  public function down() 
  26.  { 
  27.   Schema::drop("posts"); 
  28.  } 

测试数据 ModelFactory.php

  1. $factory->define(App\Post::classfunction (Faker\Generator $faker) { 
  2.  return [ 
  3.   'title' => $faker->sentence, 
  4.   'description' => $faker->paragraph, 
  5.  ]; 
  6. }); 

填充

  1. <?php 
  2.  
  3. use Illuminate\Database\Seeder; 
  4.  
  5. class DatabaseSeeder extends Seeder 
  6.  /** 
  7.   * Run the database seeds. 
  8.   * 
  9.   * @return void 
  10.   */ 
  11.  public function run() 
  12.  { 
  13.   // $this->call(UsersTableSeeder::class); 
  14.   factory(App\Post::class, 50)->create(); 
  15.  } 

路由

Route::get('my-post', 'PostController@myPost');

控制器

  1. namespace App\Http\Controllers; 
  2.  
  3. use Illuminate\Http\Request; 
  4. use App\Http\Requests; 
  5. use App\Post; 
  6.  
  7. class PostController extends Controller 
  8.  
  9.  public function myPost(Request $request
  10.  { 
  11.   $posts = Post::paginate(6);  
  12.  
  13.   if ($request->ajax()) { 
  14.    $view = view('data',compact('posts'))->render(); 
  15.    return response()->json(['html'=>$view]); 
  16.   } 
  17.  
  18.   return view('my-post',compact('posts')); 
  19.  } 
  20.  

视图文件 resources/view/my-post.php

  1. <!DOCTYPE html> 
  2. <html> 
  3. <head> 
  4.  <title>Laravel 分页滚动加载</title> 
  5.  <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> 
  6.  <link href="http://libs.baidu.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="external nofollow" rel="stylesheet"
  7.  <style type="text/css"
  8.   .ajax-load{ 
  9.    background: #e1e1e1; 
  10.    padding: 10px 0px; 
  11.    width: 100%; 
  12.   } 
  13.  </style> 
  14. </head> 
  15. <body> 
  16.  
  17. <div class="container"
  18.  <h2 class="text-center">Laravel 分页滚动加载</h2> 
  19.  <br/> 
  20.  <div class="col-md-12" id="post-data"
  21.   @include('data'
  22.  </div> 
  23. </div> 
  24.  
  25. <div class="ajax-load text-center" style="display:none"
  26.  <p>![](./loader.gif)加载更多……</p> 
  27. </div> 
  28.  
  29. <script type="text/javascript"
  30.  var page = 1; 
  31.  $(window).scroll(function() { 
  32.   if($(window).scrollTop() + $(window).height() + 1>= $(document).height()) { 
  33.    page++; 
  34.    loadMoreData(page); 
  35.   } 
  36.  }); 
  37.  
  38.  function loadMoreData(page){ 
  39.   $.ajax( 
  40.    { 
  41.     url: '?page=' + page, 
  42.     type: "get"
  43.     beforeSend: function() 
  44.     { 
  45.      $('.ajax-load').show(); 
  46.     } 
  47.    }) 
  48.    .done(function(data) 
  49.    { 
  50.     //console.log(data.html); 
  51.     if(data.html == " "){ 
  52.      $('.ajax-load').html("没有数据了……"); 
  53.      return
  54.     } 
  55.     $('.ajax-load').hide(); 
  56.     $("#post-data").append(data.html); 
  57.    }) 
  58.    .fail(function(jqXHR, ajaxOptions, thrownError) 
  59.    { 
  60.     alert('服务未响应……'); 
  61.    }); 
  62.  } 
  63. </script> 
  64.  
  65. </body> 
  66. </html> 

resources/view/data.php

  1. @foreach($posts as $post
  2. <div> 
  3.  <h3><a href="">{{ $post->title }}</a></h3> 
  4.  <p>{{ str_limit($post->description, 400) }}</p> 
  5.  
  6.  <div class="text-right"
  7.   <button class="btn btn-success">Read More</button> 
  8.  </div> 
  9.  
  10.  <hr style="margin-top:5px;"
  11. </div> 
  12. @endforeach 

效果:

Laravel滚动加载 Ajax加载

Tags: Laravel滚动加载 Ajax加载

分享到: