0

Trying to get cities to select the country in laravel through vue.js in the component.

I am making dynamic dropdown lists with Vue.JS , the Countries the Cities JSON is not received and not fetched into the view and there is no error shown in the console, I couldn't find my mistake..

Component Template Code

<template>
    <div>
        <div class="row">
            <div class="col-lg-6">
                <select name="county" id="countries" v-model='country' @change='getCities()'>
                    <option value="" selected disabled>Select country</option>
                    <option v-for='data in countries' :value='data.id'>{{ data.name }}</option>
                </select>
            </div>
            <div class="col-lg-6">
                <select name="city" id="city" v-model='city'>
                <option value="" selected disabled>Select city</option>
                <option v-for='data in cities' :value='data.id'>{{ data.name }}</option>
            </select>
            </div>
        </div>
    </div>
</template>

Component Js Code

<script  type="text/javascript">
export default {
    mounted() {
        console.log('Component mounted.')
    },
    data(){
        return {
            country:0,
            countries:[],
            city:0,
            cities:[]
        }
    },
    methods:{
        getCountries: function (){
            axios.get('/get-countries')
                .then(function (response){
                    this.countries = response.data;
                }.bind(this));
        },
        getCities: function (){
            axios.get('/get-cities', {
                params: {
                    country: this.country
                }
            }).then(function (response){
                this.cities = response.data;
            }.bind(this));
        }
    },
    created: function () {
        this.getCountries()
    },
    updated: function () {
        $('#countries').trigger("chosen:updated");
        $('#city').trigger("chosen:updated");
    }
}
</script>

Trying to get city but the city array is null all time.

0 Answers0