溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何在Laravel 中使用Ajax實現(xiàn)滾動加載

發(fā)布時間:2021-02-17 08:00:57 來源:億速云 閱讀:168 作者:Leah 欄目:開發(fā)技術

這期內(nèi)容當中小編將會給大家?guī)碛嘘P如何在Laravel 中使用Ajax實現(xiàn)滾動加載,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

創(chuàng)建模型

php artisan make:model -m

模型Post.php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{

 public $fillable = ['title','description'];

 
}

遷移文件

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePostTable extends Migration
{
 /**
  * Run the migrations.
  *
  * @return void
  */
 public function up()
 {
  Schema::create('posts', function (Blueprint $table) {
   $table->increments('id');
   $table->string('title');
   $table->text('description');
   $table->timestamps();
  });
 }

 /**
  * Reverse the migrations.
  *
  * @return void
  */
 public function down()
 {
  Schema::drop("posts");
 }
}

測試數(shù)據(jù) ModelFactory.php

$factory->define(App\Post::class, function (Faker\Generator $faker) {
 return [
  'title' => $faker->sentence,
  'description' => $faker->paragraph,
 ];
});

填充

<?php

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
 /**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
  // $this->call(UsersTableSeeder::class);
  factory(App\Post::class, 50)->create();
 }
}

路由

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

控制器

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Post;

class PostController extends Controller
{

 public function myPost(Request $request)
 {
  $posts = Post::paginate(6); 

  if ($request->ajax()) {
   $view = view('data',compact('posts'))->render();
   return response()->json(['html'=>$view]);
  }

  return view('my-post',compact('posts'));
 }

}

視圖文件 resources/view/my-post.php

<!DOCTYPE html>
<html>
<head>
 <title>Laravel 分頁滾動加載</title>
 <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
 <link href="http://libs.baidu.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="external nofollow" rel="stylesheet">
 <style type="text/css">
  .ajax-load{
   background: #e1e1e1;
   padding: 10px 0px;
   width: 100%;
  }
 </style>
</head>
<body>

<div class="container">
 <h3 class="text-center">Laravel 分頁滾動加載</h3>
 <br/>
 <div class="col-md-12" id="post-data">
  @include('data')
 </div>
</div>

<div class="ajax-load text-center" >
 <p>![](./loader.gif)加載更多……</p>
</div>

<script type="text/javascript">
 var page = 1;
 $(window).scroll(function() {
  if($(window).scrollTop() + $(window).height() + 1>= $(document).height()) {
   page++;
   loadMoreData(page);
  }
 });

 function loadMoreData(page){
  $.ajax(
   {
    url: '?page=' + page,
    type: "get",
    beforeSend: function()
    {
     $('.ajax-load').show();
    }
   })
   .done(function(data)
   {
    //console.log(data.html);
    if(data.html == " "){
     $('.ajax-load').html("沒有數(shù)據(jù)了……");
     return;
    }
    $('.ajax-load').hide();
    $("#post-data").append(data.html);
   })
   .fail(function(jqXHR, ajaxOptions, thrownError)
   {
    alert('服務未響應……');
   });
 }
</script>

</body>
</html>

resources/view/data.php

@foreach($posts as $post)
<div>
 <h4><a href="">{{ $post->title }}</a></h4>
 <p>{{ str_limit($post->description, 400) }}</p>

 <div class="text-right">
  <button class="btn btn-success">Read More</button>
 </div>

 <hr >
</div>
@endforeach

上述就是小編為大家分享的如何在Laravel 中使用Ajax實現(xiàn)滾動加載了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI