佳木斯湛栽影视文化发展公司

主頁(yè) > 知識(shí)庫(kù) > Laravel 簡(jiǎn)單實(shí)現(xiàn)Ajax滾動(dòng)加載示例

Laravel 簡(jiǎn)單實(shí)現(xiàn)Ajax滾動(dòng)加載示例

熱門(mén)標(biāo)簽:阿里云 科大訊飛語(yǔ)音識(shí)別系統(tǒng) 電子圍欄 Mysql連接數(shù)設(shè)置 團(tuán)購(gòu)網(wǎng)站 銀行業(yè)務(wù) 服務(wù)器配置 Linux服務(wù)器

開(kāi)發(fā)H5項(xiàng)目的時(shí)候我們總是需要用到下拉滾動(dòng)刷新的方式加載頁(yè)面。這里用 Laravel 實(shí)現(xiàn)一下,直接上代碼:

創(chuàng)建模型

這里我們不妨創(chuàng)建一個(gè) 文章(Post)模型, 并且生成測(cè)試數(shù)據(jù) 50 條吧。

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");
 }
}

測(cè)試數(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 分頁(yè)滾動(dòng)加載/title>
 script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js">/script>
 link  rel="external nofollow" rel="stylesheet">
 style type="text/css">
  .ajax-load{
   background: #e1e1e1;
   padding: 10px 0px;
   width: 100%;
  }
 /style>
/head>
body>

div class="container">
 h2 class="text-center">Laravel 分頁(yè)滾動(dòng)加載/h2>
 br/>
 div class="col-md-12" id="post-data">
  @include('data')
 /div>
/div>

div class="ajax-load text-center" style="display:none">
 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("沒(méi)有數(shù)據(jù)了……");
     return;
    }
    $('.ajax-load').hide();
    $("#post-data").append(data.html);
   })
   .fail(function(jqXHR, ajaxOptions, thrownError)
   {
    alert('服務(wù)未響應(yīng)……');
   });
 }
/script>

/body>
/html>

resources/view/data.php

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

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

 hr style="margin-top:5px;">
/div>
@endforeach

效果:

以上這篇Laravel 簡(jiǎn)單實(shí)現(xiàn)Ajax滾動(dòng)加載示例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • 在Laravel中實(shí)現(xiàn)使用AJAX動(dòng)態(tài)刷新部分頁(yè)面
  • laravel 解決ajax異步提交數(shù)據(jù),并還回填充表格的問(wèn)題

標(biāo)簽:廣元 衢州 大理 萍鄉(xiāng) 棗莊 江蘇 蚌埠 衡水

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Laravel 簡(jiǎn)單實(shí)現(xiàn)Ajax滾動(dòng)加載示例》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話(huà)咨詢(xún)

    • 400-1100-266
    邵阳县| 珠海市| 汾西县| 治县。| 长垣县| 马山县| 盘锦市| 大安市| 印江| 双柏县| 无棣县| 阿拉善盟| 井陉县| 青岛市| 萨嘎县| 巴林右旗| 肇庆市| 九台市| 台山市| 蒙山县| 新营市| 阳泉市| 岢岚县| 朔州市| 哈巴河县| 富顺县| 汽车| 西乌珠穆沁旗| 麟游县| 宜阳县| 鸡泽县| 获嘉县| 凤凰县| 新巴尔虎左旗| 清原| 蒲城县| 渝北区| 湖北省| 历史| 溧水县| 永新县|