Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic dropdowns using select2, json request and Laravel

I am trying to get dynamic drop downs working with Laravel and Select2. There are two drop downs; one for companies i.e. "company2" and one for locations that belong to that company i.e. "location2".

For the life of me, I cannot figure out how to make the "company2" drop down fire a event to read that companies locations, if it is changed! What am I doing wrong in the javascript section of this code! (everything else works)

Route

Route::controller('api', 'ApiController');

Controller (ApiController)

public function getLocations($companyId)
{
    return Location::where('company_id', $companyId)->lists('id', 'name');
}

Example output from address "api/locations/7"

{"Yellowstone":"8"}

View (form open/close section omitted)

{!! Form::select('company_id', $companies, null, ['class' => 'company2 form-control']) !!}
{!! Form::select('location_id', $locations, null, ['class' => 'location2 form-control']) !!}

View (Javascript)

<script type="text/javascript">
    $(document).ready(function() {
        $(".company2").select2();
        $(".location2").select2();
    });

$(".company2").select2().on('change', function() {
    var $company2 = $('.company2');
    $.ajax({
        url:"../api/locations/" + $company2.val(),
        type:'GET',
        success:function(data) {
            var $location2 = $(".location2");
            $location2.empty();
            $.each(data, function(value, key) {
                $location2.append($("<option></option>").attr("value", value).text(key));
            }); 
            $location2.select2();
        }
    });
}).trigger('change');
</script>

The view is passed a list of active companies when initialized i.e.

$companies = Company::lists('trading_name', 'id');
like image 604
Matthew Malone Avatar asked May 16 '15 07:05

Matthew Malone


2 Answers

Replace your javascript with the following, you may need to tweak some of it. Please make sure you look through the comments.

var $company2 = $('.company2');
var $location2 = $(".location2");

$company2.select2().on('change', function() {
    $.ajax({
        url:"../api/locations/" + $company2.val(), // if you say $(this) here it will refer to the ajax call not $('.company2')
        type:'GET',
        success:function(data) {
            $location2.empty();
            $.each(data, function(value, key) {
                $location2.append($("<option></option>").attr("value", value).text(key)); // name refers to the objects value when you do you ->lists('name', 'id') in laravel
            });
            $location2.select2(); //reload the list and select the first option
        }
    });
}).trigger('change');

Change the following when you grab the location data from the controller

public function getLocations($companyId)
{
    return Location::where('company_id', $companyId)->lists('name', 'id');
}
like image 167
haakym Avatar answered Nov 03 '22 01:11

haakym


you can try this :

$.getJSON("getOptions.php", function (json) {
      $("#inputs").select2({
         data: json,
         width: "180px"
      });
 });

example json output :

    {id:0,text:"Text 1"},
    {id:1,text:"Text 2"},
    {id:2,text:"Text 3"},
    {id:3,text:"Text 4"},
    {id:4,text:"Text 5"}
like image 1
Bruno Ribeiro Avatar answered Nov 02 '22 23:11

Bruno Ribeiro