我的目标是使用ajax制作带选择选项菜单和分页的过滤器,选择选项可在每页10,15,20,25个产品之间进行选择,并同时更新laravel默认分页与新记录,例如来自10个链接的10个链接取决于选择选项结果。问题是我可以使用select选项过滤每页产品,但我无法更新laravel分页记录。
//the select option list
<label>Show</label>
<select id="selectAmount">
<option id="tweleve" value="10" selected="selected">10</option>
<option id="fifteen" value="15">15</option>
<option id="thirty" value="20">20</option>
</select>
//the view products.blade.php
<div class="products">
<ul id="prods">
@foreach($products as $product)
<li>{{ $product->title }}</li>
<li>{{ $product->image }}</li>
<li>{{ $product->price }}</li>
@endforeach
</ul>
{{ $products->links() }}
</div>
//the partial view partials.products.php
<li>{{ $product->title }}</li>
<li>{{ $product->image }}</li>
<li>{{ $product->price }}</li>
// the route
Route::get('products', 'ProductController@index');
//the controller
public function index(Request $request)
{
if($request->ajax()) {
$amount = $request->input('amount');
$products = Product::paginate($amount);
$list = [];
foreach($products as $product){
$html = view('partials.prodperpage')
->with('product', $product)
->render();
$list[] = $html;
}
return response()->json($list);
}
$products = Product::paginate(9);
return view('pages.products', compact('products'));
}
//the ajax request
$('#selectAmount').change( function() {
var selected = $('#selectAmount option:selected');
var myurl = '/products';
$('#prods').empty();
if(selected.val() <= 30) {
var amount = selected.val();
$.ajax({
type: 'GET',
url: myurl,
data: {amount:amount},
success: function(data) {
$('#prods').append(data);
}
});
}
});
答案 0 :(得分:0)
如果你想在Ajax请求之后让你的分页工作,我认为这个问题已在这里得到解答 Laravel 5: How to Paginate an Ajax Response