5

I want to populate select tag with data from api get request. Here is my html code

<div id="app">
    <label for="country" class="control-label">Country</label>
    <select v-model="selectedCountry" @change="onChangeCountry" name="country" id="country" class="form-control" tabindex="11">
        <option selected disabled value="">Please select one</option>
        @foreach($countries as $country)
            <option value="{{ $country->id }}">{{ $country->name }}</option>
        @endforeach
    </select>
    <label for="city" class="control-label">City</label>
    <select v-model="cities" name="city" id="city" class="form-control" tabindex="12">
        <option v-bind:value="city.id">@{{ city.name }}</option>
    </select>
</div>

And now my JavaScript code:

<script type="text/javascript">
    new Vue({
      el: '#app',
      data: {
        selectedCountry: '',
        cities: []
      },
      methods: {
          onChangeCountry: function (event) {
            axios.get('http://localhost:8000/api/cities/country/' + this.selectedCountry)
            .then(function (
                this.cities = response.data
            }).catch(function(error) {
                console.log('an error occured ' + error);
            });
          }
        }
    });
</script>

I'm pretty sure that the data is received because i did a lot of console.log but I don't know how to append the received data to my second select tag nor how to proceed.

3
  • Please read Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers? - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. Commented Mar 2, 2018 at 10:50
  • In your comments below, you have responded several times with the report that "it doesn't work". Please note that this is the least helpful response you can give a contributor. You are the eyes and ears on the problem, and it is your job to be as clear and verbose as you can. Consider these things: how did it not work? What did you observe? Were there any logs or errors, or did it partly work? What information can you send to the helpful person trying to help you, without any prompting? Commented Mar 2, 2018 at 10:53
  • i don't know what's happening, the select tag that i want to fill is still empty after so many attempts, and there is no errors in console Commented Mar 2, 2018 at 12:56

4 Answers 4

5

Try this in the select

<select v-model="selectedCity" name="city" id="city" class="form-control" tabindex="12">

   <option v-for="(city,cityIndex) in cities" :key="city.id" :value="city.id">{{ city.name }}</option>

</select>

Add 'selectedCity' to the data object and then access its value through this.selectedCity

This is in the vue docs

https://v2.vuejs.org/v2/guide/list.html

https://v2.vuejs.org/v2/guide/forms.html#Select

Sign up to request clarification or add additional context in comments.

7 Comments

still doesn't do anything
what does the axios request object returns?
an array of objects(the city object)
i don't know what's happening, the select tag that i want to fill is still empty after so many attempts, and there is no errors in console
One thing that is confusing me is that you're using Vue and trying to use v-model, but on the same page you have a blade foreach. Please elaborate on the structure that you're using
|
2

I finally got it working
i just needed to create a city variable in data function
and in select i don't need to bind it to array of cities[], the city variable is fine v-model="city"

2 Comments

Can't see how that is actually different than what i answered
At the first i was biding to the array of cities using v-model but then i bind it to a single city object. I don't really get it because i'm a newbie to vuejs
1
<select v-model="country" class="form-control">
   <option disabled value="" selected="selected">Please select one</option>
   <option v-for="country in countries" :key="country.id" v-bind:value="country.id">
           {{country.name}}
     </option>

</select>
<script>
export default {
    data() {
        return {
            countries: {},
            country:''
    } 
</script>

Comments

0

You need to use v-for to loop the cities and create the option elements:

<select name="city" id="city" class="form-control" tabindex="12">
    <option v-for="(city, index) in cities" 
            :key="index" 
            :value="city.id">@{{ city.name }}
    </option>
</select>

Also, change your data property into a function so it's reactive:

 data () {
    return {
      selectedCountry: '',
      cities: []
    }
 }

6 Comments

Updated the answer
still doesn't do anything
do you want any other details i believe everything is clear but why is this happening to me ?
What does console.log(response.data) show? Can you include the output in your answer?
(2) [{…}, {…}]0: {id: 4, country_id: 4, name: "West Cortney", created_at: "2018-02-28 13:21:41", updated_at: "2018-02-28 13:21:41"}1: {id: 6, country_id: 4, name: "Ceceliaton", created_at: "2018-02-28 13:21:41", updated_at: "2018-02-28 13:21:41"}length: 2__proto__: Array(0) 11:23:17.800
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.