jquery - Laravel - Dynamic select menu -
i'm trying dynamically set options in select menu first selecting option in first select menu.
example: user selects company in 1 select menu. in second select menu locations belong selected company set.
here view
<div class="form-group"> <select class="form-control admin-select" name="admin-select"> <option>- select -</option> <option value="1">company 1</option> <option value="2">company 2</option> <option value="3">company 3</option> </select> </div> <div class="form-group"> <select id="location" class="form-control" name="location"> <option>- select -</option> </select> </div>
jquery:
$('.admin-select').change(function() { $.getjson("/users/dropdown/"+$(this).val(), function(data) { var location = $('#location'); location.empty(); $.each(data, function(index, element) { location.append("<option value='"+ element.id +"'>" + element.name + "</option>"); }); }); });
userscontroller:
public function dropdown() { $input = input::get('option'); $company = company::find($input); $locations = $company->locations(); return response::make($locations->get(['id','name'])); }
i think issue need set route dropdown function, i'm not sure route should like.
the dropdown function being called when select company i'm not getting data via json.
if id = $(this).val();
$.getjson("/users/dropdown/", { id : $(this).val() }, function(data) {});
route:
route::get('users/dropdown/{id}', 'usercontroller@dropdown')
controller:
class usercontroller extends basecontroller { /** * show profile given user. */ public function dropdown($id) { $company = company::find($id); $locations = $company->locations(); return $locations->get(['id','name']); } }
Comments
Post a Comment