Skip to content

Instantly share code, notes, and snippets.

@talha08
Last active May 10, 2017 08:00
Show Gist options
  • Select an option

  • Save talha08/fec67fa2e72e0810d9e7e759fecd12f0 to your computer and use it in GitHub Desktop.

Select an option

Save talha08/fec67fa2e72e0810d9e7e759fecd12f0 to your computer and use it in GitHub Desktop.
class ApiController extends Controller
{
/**
* Section dependant dropdown system
* This is for committee create
*
* @return \Illuminate\Http\Response
*/
public function sectionDropDownData()
{
$member_type_id = \Input::get('member_type_id');
if($member_type_id == 1 or $member_type_id == 2 ){
$teacherList =\DB::table('role_user')
->where('role_id',2)
->lists('user_id');
$users = User::where('dept_id', \Auth::user()->dept_id)->whereIn('id',$teacherList )->lists('name','id');
return \Response::json($users);
}else{
$studentList =\DB::table('role_user')
->where('role_id',3)
->lists('user_id');
$users = User::where('dept_id', \Auth::user()->dept_id)->whereIn('id',$studentList )->lists('name','id');
return \Response::json($users);
}
}
//Api route
Route::get('api/section-dropdown', array('as' => 'apiroute', 'uses' => 'ApiController@sectionDropDownData'));
{!! Form::open(array('route' => 'student.search')) !!}
<div class="form-group">
{!! Form::label('class_id', 'Select Class :', array('class' => 'control-label')) !!}
{!!Form::select('class_id', $class, '',array('class' => 'select2', 'autofocus','id'=>'class'))!!}
{!! Form::label('section_id', 'Select Section :', array('class' => 'control-label')) !!}
{!!Form::select('section_id', $section, '',array('class' => 'select2', 'autofocus', 'id'=>'section'))!!}
{!! Form::submit('Search', array('class' => ' btn btn-primary')) !!}
</div><br/>
{!! Form::close() !!}
<script type="text/javascript" charset="utf-8">
//depandable dropdown start
$('#type').on('change', function(e){
var member_type_id = e.target.value;
//ajax
var path = "<?php echo URL::route('apiroute'); ?>";
$.get(path+'?member_type_id='+member_type_id, function(data){
//success data
$('#user').empty();
$('#user').append('<option value=""> Please choose one</option>');
$.each(data, function(key, value) {
var option = $("<option></option>")
.attr("value", key)
.text(value);
$('#user').append(option);
});
});
});
//depandable dropdown end
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment