3

I'm trying to filter the records from a query using dropdown menus. The idea is to aply the filter when the dropdown menu is changed, so I imagine the solution is to use ajax, but i don't know much about it and how to use it in laravel.

Here is my controller:

public function setSelectTestView()
{
    $test         = Test::orderBy('updated_at', 'des')->lists('nom', 'id');
    $cursos       = Curs::orderBy('nom', 'asc')->lists('nom', 'id');
    $assignatures = Assignatura::orderBy('nom', 'asc')->lists('nom', 'id');

    return View::make('Test.results.result_select_test')
        ->with(array('test' => $test, 'cursos' => $cursos, 'asg' => $assignatures));
}

and my blade:

<fieldset>
  {{ Form::select('curs', $cursos) }}
  {{ Form::select('assignatura', $asg) }}
</fieldset>

{{ Form::open(array('action' => array('ResultsController@setSelectTestView'))) }}
  </br>                                                
  <fieldset>
    {{ Form::select('test', $test) }}
  </fieldset>
  </br><input type="submit" value="Seleccionar"/>

{{ Form::close() }} 

What I want is to filter the variable $test by curs and assignatura, like

$test = Test::where('curs_id', $curs_id)->where('assignatura_id', $asg_id);

but doing this in a asynchronous way and applying one filter depending which dropdown menu has changed, but also if the second filter is changed keeping applying the first one (both applied)

1
  • I just build this to give a good demonstration of what you'd want to do. It's fairly and it's doing the same thing as you are wanting to do, except with contacts. It should have everything you need though and I tried to document it well... laravel.io/bin/yQzdo#4,10 Commented Apr 30, 2014 at 18:18

1 Answer 1

2

You can start by getting the the value of filter on onchange() event of your select box and then sent it to your controller via post.

    $('select[name="curs"]').onchange(){
   $cur = $(this).val();
   $asg = $('select[name="assignatura"]').val();
 $.ajax({
    url:'/result/search',
    data:{cur:$cur,asg:$asg;},
    cache:false,
    type:'POST',
    beforeSend: function (xhr) {
        var token = $('meta[name="csrf_token"]').attr('content');

        if (token) {
            return xhr.setRequestHeader('X-CSRF-TOKEN', token);
        }
    },
    success:function(data){ // this is view data returned by controller 
        console.log(data);
        $('#result_container').html(data); // use it to fill your results
    }
})}

use your route as

Route::post('/result/search','ResultController@search');

Then in controller use parameter to query result.

  public function search(){

$cur_id = Input::get('cur');
$asg_id = Input::get('asg');

    $test = Test::where('curs_id', $cur)->where('assignatura_id', $asg_id);

return view('Test.results.result_select_test',compact('test'));}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.