How can I reset dropdown data if modal closed on v

2019-07-09 09:18发布

My case like this

I have two component, parent and child component

My parent component like this :

<template>
    ...
    <div class="row">
        ...
            <location-select level="continentList" type="1"/>
        ...
            <location-select level="countryList" type="2"/>
        ...
            <location-select level="cityList" type="3"/>
        ...
    </div>
</template>
<script>
    ...
    export default{
        ...
    }
</script>

The parent component is a modal bootstrap

My child component like this :

<template>
    <select class="form-control" v-model="selected" @change="changeLocation">
        <option value="0" disabled>Select</option>
        <template v-for="option in options">
            <option v-bind:value="option.id" >{{ option.name }}</option>
        </template>
    </select>
</template>
<script>
    ...
    export default{
        props: ['level','type'],
        data() { return { selected: '' };},
        computed:{
            ...mapGetters([
                'getContinentList', 'getCountryList','getCityList'
            ]),
            options(){
                const n = ['getContinentList', 'getCountryList','getCityList']
                return this[n[this.type-1]]
            }
        },
        methods:{
            ...mapActions([
                'getLocationList'
            ]),
            changeLocation(event){
                if(this.type==1){
                    this.getLocationList([{type:2},{level:'countryList'}])
                    this.getLocationList([{type:3},{level:'cityList'}])
                }else if(this.type==2){
                    this.getLocationList([{type:3},{level:'cityList'}])
                }
            }
        },
        created() {
            if(this.type==1)
                this.getLocationList([{type:1},{level:'continentList'}])
            if(this.type==2 && this.selected!='')
                this.getLocationList([{type:2},{level:'countryList'}])
            if(this.type==3 && this.selected!='')
                this.getLocationList([{type:3},{level:'cityList'}])
        },
        mounted() {
            $(this.$parent.$refs.modal).on('hidden.bs.modal', (e) => {
                Object.assign(this.$data, this.$options.data())
            })
        },
    };
</script>

If the modal show, I select continent, country and city. Then I close the modal

After that I show the modal again. Then I select country and city first, the data is still exist

I want to reset the data. So if I open modal again, before I choose continent, country and city data is not showing

I try :

Object.assign(this.$data, this.$options.data())

and this :

Object.assign(this.$data,this.$options.data.call(this))

and this too :

this.$forceUpdate()

when modal hidden

But, it does not work

Seems it must update data computed:{...}. But I'm still confused to do it

How can I solve this problem?

2条回答
孤傲高冷的网名
2楼-- · 2019-07-09 09:39

Just v-if the modal component and it will rerender/recalculate everything when toggled.

Parent of Parent component:

    <template>
        <div class="parent-of-parent">
            <your-modal-component v-if="isModalOpened" @close="closeModal"></your-modal-component>
        </div>
    </template>


<script>
   export default{
        data() { 
            return { isModalOpened: false };
        },
        methods: {
           openModal() {
               this.isModalOpened = true;
           },  // method to open it
           closeModal() {
              this.isModalOpened = false;
           },   // method to attach to emit event from modal on close


        },
    };

</script>

Be sure to attach click event on the icon you click for close or whatever. And attach a method to the event which emits like this.

this.$emit('closeModal');

If you have problem animating the modal use <transition>

<transition name="fadeIn">
     <your-modal-component v-if="isModalOpened" @close="closeModal"></your-modal-component>
</transition>
查看更多
ゆ 、 Hurt°
3楼-- · 2019-07-09 09:57

Have you tried reseting the selected data attribute in mounted lifecycle hook?

...    
mounted() {
    let $vm = this;

    $(this.$parent.$refs.modal).on('hidden.bs.modal', (e) => {
         $vm.selected = '';
    });
},
...
查看更多
登录 后发表回答