Skip to content

Instantly share code, notes, and snippets.

@talha08
Last active August 7, 2017 10:12
Show Gist options
  • Save talha08/efc09b65b682b92055aeab11c8cd26e3 to your computer and use it in GitHub Desktop.
Save talha08/efc09b65b682b92055aeab11c8cd26e3 to your computer and use it in GitHub Desktop.
Depandable dropdoen
public function create()
{
$state_id = States::pluck('name','id');
$cities = ['Please select state first'];
$venue_type = VenueType::pluck('name', 'id');
return view('venue.create', compact('venue_type','state_id','cities'))->with('title', "Create New Venue ");
}
//Api route
Route::get('api/section-dropdown', array('as' => 'apiroute', 'uses' => 'ApiController@sectionDropDownData'));
==============================================================================
/**
* 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);
}
}
==============================================================================
<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);
});
// $.each(data, function(index, subcatObj){
// // console.log();
// $('#user').append('<option value="' + subcatObj.id+'">'// this returns the value
// + subcatObj.name + '</option>'); //this returns the key
// });
});
});
//depandable dropdown end
</script>
==============================================================================
<div class="form-group">
{!! Form::label('member_type_id', 'Member Type :', array('class' => 'control-label')) !!}<br/>
{!!Form::select('member_type_id', $type, '',array('placeholder' => 'Please select a type','id'=>'type','class' => 'select2 form-control placeholder-no-fix', 'autofocus'))!!}
</div><br/>
<div class="form-group">
{!! Form::label('user_id', 'Member Name :', array('class' => 'control-label')) !!}<br/>
{!!Form::select('user_id', $user, '',array('id'=>'user','class' => 'select2 form-control placeholder-no-fix', 'autofocus'))!!}
</div><br/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment