1.router
Route::get(‘product‘, function(){ $products = App\Product::paginate(10); return view(‘product.index‘, compact(‘products‘)); }); Route::get(‘ajax/product‘, function(){ $products = App\Product::paginate(10); return view(‘product.indexAjax‘, compact(‘products‘)); });
2.indexAjax.blade.php
<div class="row"> <div class="col-md-6 col-md-offset-3"> <h2>Products list</h2> <ul> @foreach($products as $product) <li><h3>{{ $product->name }}</h3><span class=‘pull-right‘>{{ $product->id }}</span></li> @endforeach </ul> {!! $products->render() !!} </div> </div>
3.js
<script type="text/javascript"> $(document).on(‘click‘, ‘.pagination a‘, function(e){ e.preventDefault(); page = $(this).attr(‘href‘).split(‘page=‘)[1]; getProducts(page); }); function getProducts(page){ $.ajax({ url : ‘ajax/product?page=‘ + page, }).done(function(date){ $(‘.container‘).html(date); location.hash = page; }); } </script>
时间: 2024-10-12 20:03:41