使用下拉 Laravel 过滤
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28294970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Filter with dropdown Laravel
提问by ana
I have a dropdown that I′m using to filter images by category. My first problem is that I want the selected choice to be selected after the filter, how can I do that?
我有一个下拉菜单,用于按类别过滤图像。我的第一个问题是我希望在过滤器之后选择选定的选项,我该怎么做?
This is my first time using Laravel and I wonder if I′m going in the right direction with my solution (now I have the same code in two functions, I′m planning on fixing that), but I can′t really figure out the best way of doing this. Can I have a function that takes either a category or null?
这是我第一次使用 Laravel,我想知道我的解决方案是否朝着正确的方向前进(现在我在两个函数中有相同的代码,我打算修复它),但我真的不知道这样做的最佳方式。我可以有一个接受类别或空值的函数吗?
<div class="dropdown">
<button class="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown" value="">Show all</button>
<ul class="dropdown-menu" role="menu">
@foreach ($categories as $category)
<li value="{{ $category->id }}"><a href="{{$category->id}}">{{ $category->name }}</a></li>
@endforeach
</ul>
</div>
routes
路线
Route::get('/', array('uses' => 'MyController@index'));
Route::get('/{category?}', array('uses' => 'MyController@filter'));
controller
控制器
public function index()
{
$images = Image::all();
$categories = ..\Models\Category::all();
return View::make('index', array('images' => $images, 'categories' => $categories));
}
public function filter($category){
$images = Image::where('category_id', '=', $category);
$categories = ..\Models\Category::all();
return View::make('index', array('images' => $images, 'categories' => $categories));
}
采纳答案by Mahozi
In your view, add a condition to check if the current category in the loop is the selected one.
在您的视图中,添加一个条件来检查循环中的当前类别是否是选定的类别。
@foreach ($categories as $category)
@if($category->id == Input::get('category')
// echo category as selected
@else
// echo category
@endif
@endforeach
You might need to use html <select>
.
您可能需要使用 html <select>
。
You can use the same approach to combine the two function.
您可以使用相同的方法来组合这两个功能。
if(Input::has('category'))
$images = Image::where('category_id', '=', $category);
else
$images = Image::all();
This should work since your are using optional route parameter.
这应该有效,因为您使用的是可选的路由参数。
Update:
更新:
Use select as follows:
使用 select 如下:
@foreach ($categories as $category)
<select onchange="filter(this.value)">
@if($category->id == Input::get('category')
<option selected="selected" value="{{ $category->id }}">{{ $category->name }}</option>
@else
<option value="{{ $category->id }}">{{ $category->name }}</option>
@endif
</select>
@endforeach
Using the onchange
attribute a javascript function will be called, then you can use redirect.
使用该onchange
属性将调用 javascript 函数,然后您可以使用重定向。
<script>
function filter(id)
{
window.location.href = {{ URL::action('Controller@filter') }} + '/' + id;
</script>
where filter
is the function in your controller.
filter
控制器中的功能在哪里。