如何使用 AJAX 渲染局部? Laravel 5.2
How to render partial using AJAX? Laravel 5.2
我想列出存储在 MYSQL 数据库中的有关停车位的信息。我正在使用 AJAX 调用 API 端点 (/api/spots) 和 return 点列表。我使用 blade 语法为信息布局创建了一个局部视图 (partials/Spot.blade.php).
我想知道是否有一种方法可以创建一个控制器方法,该方法将 return 局部视图并将其呈现到页面的一部分,而无需返回服务器。这可能使用我的 partials/Spot.blade.php 吗?也许我应该创建一个方法来 return 将其中的所有 HTML 数据作为字符串,然后获取 JS 将其添加到 DOM?
我目前的做法是在最初加载页面时呈现 partials/Spot.blade.php,然后使用 CSS 将其从屏幕上移除。然后在 AJAX 调用服务器之后,我在这个隐藏的部分中获取 HTML 并使用 REGEX 将数据放置在布局中。虽然这看起来有点脏。只是想知道其他人是如何解决这个问题的。
非常感谢您的反馈,
马特
很简单(:
查看此示例并进行自己的修改:
控制器:
class StatisticsController extends Controller
{
public function index()
{
return view('statistics.index');
}
public function filter(Request $request, $fields) {
// some calculation here...
return view('statistics.response', compact('stats')); // will render statistics/response.blade.php file with passing results of filter
}
}
观看次数:
带日期过滤的页面
statistics/index.blade.php
@extends('layouts.billing')
@section('title') Statistics @stop
@section('js_footer')
<script>
function doGet(url, params) {
params = params || {};
$.get(url, params, function(response) { // requesting url which in form
$('#response').html(response); // getting response and pushing to element with id #response
});
}
$(function() {
doGet('/statistics/filter'); // show all
$('form').submit(function(e) { // catching form submit
e.preventDefault(); // preventing usual submit
doGet('/statistics/filter', $(this).serializeArray()); // calling function above with passing inputs from form
});
});
</script>
@stop
@section('content')
<div class="row">
<div class="col-sm-12 col-md-6 col-lg-4">
<div class="panel panel-default">
<div class="panel-heading">
<h3>Statistics</h3>
</div>
<div class="panel-body">
<form>
<label>Time</label>
<div class="input-group">
<input type="text" name="time_from" value="{{ date('Y-m').'-01 00:00:00' }}" class="form-control" autocomplete="off">
<input type="text" name="time_to" value="{{ date('Y-m-d H:i:s', time()) }}" class="form-control" autocomplete="off">
</div>
<button type="submit" class="btn btn-sm btn-primary pull-right">
<i class="fa fa-search"></i> Show
</button>
</form>
</div>
</div>
</div>
<div class="col-xs-12">
<div id="response"> HERE WILL BE INJECTED RESULT OF FILTERING </div>
</div>
</div>
@stop
过滤渲染结果部分:
statistics/response.blade.php
<div class="table-responsive">
<table class="table table-striped table-borderless text-center">
<thead>
<th>ID</th>
<th>Partner</th>
<th>Tariffs</th>
<th>Total</th>
</thead>
<tbody>
@foreach($stats AS $stat)
some data here
@endforeach
</tbody>
</table>
</div>
我想列出存储在 MYSQL 数据库中的有关停车位的信息。我正在使用 AJAX 调用 API 端点 (/api/spots) 和 return 点列表。我使用 blade 语法为信息布局创建了一个局部视图 (partials/Spot.blade.php).
我想知道是否有一种方法可以创建一个控制器方法,该方法将 return 局部视图并将其呈现到页面的一部分,而无需返回服务器。这可能使用我的 partials/Spot.blade.php 吗?也许我应该创建一个方法来 return 将其中的所有 HTML 数据作为字符串,然后获取 JS 将其添加到 DOM?
我目前的做法是在最初加载页面时呈现 partials/Spot.blade.php,然后使用 CSS 将其从屏幕上移除。然后在 AJAX 调用服务器之后,我在这个隐藏的部分中获取 HTML 并使用 REGEX 将数据放置在布局中。虽然这看起来有点脏。只是想知道其他人是如何解决这个问题的。
非常感谢您的反馈,
马特
很简单(:
查看此示例并进行自己的修改:
控制器:
class StatisticsController extends Controller
{
public function index()
{
return view('statistics.index');
}
public function filter(Request $request, $fields) {
// some calculation here...
return view('statistics.response', compact('stats')); // will render statistics/response.blade.php file with passing results of filter
}
}
观看次数:
带日期过滤的页面 statistics/index.blade.php
@extends('layouts.billing')
@section('title') Statistics @stop
@section('js_footer')
<script>
function doGet(url, params) {
params = params || {};
$.get(url, params, function(response) { // requesting url which in form
$('#response').html(response); // getting response and pushing to element with id #response
});
}
$(function() {
doGet('/statistics/filter'); // show all
$('form').submit(function(e) { // catching form submit
e.preventDefault(); // preventing usual submit
doGet('/statistics/filter', $(this).serializeArray()); // calling function above with passing inputs from form
});
});
</script>
@stop
@section('content')
<div class="row">
<div class="col-sm-12 col-md-6 col-lg-4">
<div class="panel panel-default">
<div class="panel-heading">
<h3>Statistics</h3>
</div>
<div class="panel-body">
<form>
<label>Time</label>
<div class="input-group">
<input type="text" name="time_from" value="{{ date('Y-m').'-01 00:00:00' }}" class="form-control" autocomplete="off">
<input type="text" name="time_to" value="{{ date('Y-m-d H:i:s', time()) }}" class="form-control" autocomplete="off">
</div>
<button type="submit" class="btn btn-sm btn-primary pull-right">
<i class="fa fa-search"></i> Show
</button>
</form>
</div>
</div>
</div>
<div class="col-xs-12">
<div id="response"> HERE WILL BE INJECTED RESULT OF FILTERING </div>
</div>
</div>
@stop
过滤渲染结果部分:
statistics/response.blade.php
<div class="table-responsive">
<table class="table table-striped table-borderless text-center">
<thead>
<th>ID</th>
<th>Partner</th>
<th>Tariffs</th>
<th>Total</th>
</thead>
<tbody>
@foreach($stats AS $stat)
some data here
@endforeach
</tbody>
</table>
</div>