1

currently i am trying to do a reset to initial data that is being loaded. Any help is appreciated.

html:

<div id="app">
    <input v-model="name" type="text">
    <input v-model="year" type="text">
    <div v-for="group in groups">
        <input v-model="group.group_name" type="text">
        <input v-model="group.remarks" type="text">
    </div>
    <button @click="reset">Click to reset</button>
</div>

vue js:

const groups = [
    {
        group_name: 'group1 name',
        remarks: 'group1 remarks'
    },
    {
        group_name: 'group2 name',
        remarks: 'group2 remarks'
    }
];

const Example = Vue.extend({
    data() {
        return{
            name: 'Creator',
            year: 0,
            groups: groups,
            group: {
                group_name: '',
                remarks: ''
            }
        }
    },
    methods: {
        reset () {
            Object.assign(this.$data, this.$options.data.call(this));
        }
    }
});

new Example({
      el: '#app',
    mounted () {
        setTimeout(() => this.year = 2001, 1000);
    }
});

when the button triggers the reset function, only the name and year gets reset. how do i get the "group in groups" input to reset as well?

1 Answer 1

1

change your reset function add group object

const Example = Vue.extend({
                data() {
                    return{
                        name: 'Creator',
                        year: 0,
                        groups: groups

                    }
                },
                methods: {
                    reset () {
                        this.groups =  [
                            {
                                group_name: '',
                                remarks: ''
                            }
                        ]
                    }
                }
            });
Sign up to request clarification or add additional context in comments.

2 Comments

Do u mean to add the groups array data back into vue groups? like a "this.groups = groups"?
if call rest button i just empty groups data .

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.