3

When i am selecting the city in first select box show the city relented data

how to populate the the json when selecting city in first drop down and how to show the relented data when select the different city?

this is html

$(document).ready(function() {
  var cityData = [{
      cityName: 'Bengaluru',
      value: "Bengaluru",
      data: [{
          movieName: 'ABC',
          theaterName: 'Tulsi Theatre'
        },
        {
          movieName: 'DEF',
          theaterName: 'PVR'
        },
        {
          movieName: 'GHI',
          theaterName: 'Srinivasa Theatre'
        }
      ]
    },


    {
      cityName: 'Hyderabad',
      value: "Hyderabad",
      data: [{
          movieName: '123',
          theaterName: 'Theatre1'
        },
        {
          movieName: '456',
          theaterName: 'PVR2'
        },
        {
          movieName: '789',
          theaterName: 'Theatre3'
        }
      ]
    },

    {
      cityName: 'Guntur',
      value: "Guntur",
      data: [{
          movieName: 'ABC1',
          theaterName: 'Theatre4'
        },
        {
          movieName: 'DEF2',
          theaterName: 'PVR3'
        },
        {
          movieName: 'GHI3',
          theaterName: 'Theatre5'
        }
      ]
    },

    {
      cityName: 'Ongole',
      value: "Ongole",
      data: 'currently not available'
    }
  ];
  $("#selectCity").on('change', function() {
    var locations = cityData[$(this).val()];
    var locationString = 'locations';
    console.log(locations)
    $.each(locations, function(i, item) {

      console.log(JSON.stringify(item));

      // OUCH!!! 

      locationString += '<option value="' + item.id + '">' + item.name + '</option>';
    });
    //console.log(locationString)
    $('#secondselectbox').html(locationString);
  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="UserData">
  <h1>MyMovie-Ticket-Booking</h1>
  <select class="selectCity" id="selectCity">
        <option value="City">Select City</option>
        <option value="Bengaluru">Bengaluru</option>
        <option value="Hyderabad">Hyderabad</option>
        <option value="Guntur">Guntur</option>
        <option value="Ongole">Ongole</option>
       </select>
  <span id="welcome"> </span>
</div>
<div>
  <select id="secondselectbox"></select>
  <select id="secondselectbox"></select>
</div>

when i am selecting the city Bangalore show the Bangalore theater list and movie list but it is showing the undefined ....

Pedram
  • 15,766
  • 10
  • 44
  • 73
Babu
  • 137
  • 1
  • 1
  • 17

3 Answers3

2

You were not retrieving your 'locations' data correctly, per your json structure.

Also, the options do not have an 'id', 'item' in the json.

Do change the last select's id to something unique (say 'thirdselectbox'...).

Modify your select's change listener to:

$("#selectCity").on('change', function() {
    var locations = cityData.filter(c => c.cityName === $(this).val())[0].data;
    var locationString = '';
    var locationString2 = '';
    console.log(locations)
    $.each(locations, function(i, item) {

        console.log(JSON.stringify(item));
        locationString += '<option value="' + item.theaterName + '">' + item.theaterName + '</option>';
        locationString2 += '<option value="' + item.movieName + '">' + item.movieName + '</option>';
    });
    $('#secondselectbox').html(locationString);
    $('#thirdselectbox').html(locationString2);
});

See working fiddle.

Hope this helps.

dev8080
  • 3,950
  • 1
  • 12
  • 18
0

Make sure you update your select element with necessary data. Just placing the JSON and including it won't solve the issue. You can see a tutorial here on how to update a select element dynamically.

And don't use two elements with one ID. It can cause more problems. You can use different ID's for the two select elements.

buddhiv
  • 692
  • 1
  • 7
  • 24
0
   $(document).ready(function() {
  var cityData = [{
      cityName: 'Bengaluru',
      value: "Bengaluru",
      data: [{
          movieName: 'ABC',
          theaterName: 'Tulsi Theatre'
        },
        {
          movieName: 'DEF',
          theaterName: 'PVR'
        },
        {
          movieName: 'GHI',
          theaterName: 'Srinivasa Theatre'
        }
      ]
    },


    {
      cityName: 'Hyderabad',
      value: "Hyderabad",
      data: [{
          movieName: '123',
          theaterName: 'Theatre1'
        },
        {
          movieName: '456',
          theaterName: 'PVR2'
        },
        {
          movieName: '789',
          theaterName: 'Theatre3'
        }
      ]
    },

    {
      cityName: 'Guntur',
      value: "Guntur",
      data: [{
          movieName: 'ABC1',
          theaterName: 'Theatre4'
        },
        {
          movieName: 'DEF2',
          theaterName: 'PVR3'
        },
        {
          movieName: 'GHI3',
          theaterName: 'Theatre5'
        }
      ]
    },

    {
      cityName: 'Ongole',
      value: "Ongole",
      data: 'currently not available'
    }
  ];
  $("#selectCity").on('change', function() 
  {
$('#secondselectbox').html("");
var selectedlocations = $("#selectCity").val();
    var locationString = 'locations';
    var locationlist=cityData
    $.each(locationlist, function(i, item) {

    var EachLocationdetail=JSON.stringify(item);
        var _json=JSON.parse(EachLocationdetail);
    if(_json.cityName==selectedlocations)
     {

      $.each(_json.data, function() 
      {

        locationString += '<option value="' +this.movieName + '">' + this.movieName  + '</option>'; 
    });

         }

    });
    //console.log(locationString)
    $('#secondselectbox').html(locationString);
  });

});

.

Try this fiddle

Rohit Kumar
  • 776
  • 3
  • 21