使用下拉Laravel过滤 [英] Filter with dropdown Laravel

查看:96
本文介绍了使用下拉Laravel过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个下拉列表,用于按类别过滤图像.我的第一个问题是我希望在过滤器之后选择选定的选项,我该怎么做?

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?

这是我第一次使用Laravel,我想知道我的解决方案是否朝着正确的方向发展(现在我在两个函数中具有相同的代码,我打算修复该问题),但是我做不到真正找出执行此操作的最佳方法.我可以使用带类别或null的函数吗?

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?

     <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>

路线

Route::get('/', array('uses' => 'MyController@index'));
Route::get('/{category?}', array('uses' => 'MyController@filter'));

控制器

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

}

推荐答案

在您的视图中,添加一个条件以检查循环中的当前类别是否为所选类别.

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

您可能需要使用html <select>.

You might need to use 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();

这应该可行,因为您正在使用可选的route参数.

This should work since your are using optional route parameter.

更新:

使用select如下:

Use select as follows:

@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

使用onchange属性,将调用javascript函数,然后可以使用重定向.

Using the onchange attribute a javascript function will be called, then you can use redirect.

<script>
    function filter(id)
    {
        window.location.href = {{ URL::action('Controller@filter') }} + '/' + id;
</script>

其中filter是控制器中的功能.

where filter is the function in your controller.

这篇关于使用下拉Laravel过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆