Vue js Form Field should be empty












1















I have a parent component and I am assigning values in child component form. But after submitting the form I want form field should be empty.



I am using resetFields function for resetting value but when I am using these function it is setting last value, what I send it from parent component instead of reset value I want form fields should be empty.






<script>
export default {
props: ["empData"],
data {
return() {
empInfo: {
name: "",
id: null,
age: null
}
}
},
methods: {
getUserData() {
if (response.status == "SUCCESS") {
this.$refs['empInfo'].resetFields();
}
}
}
watch: {
empData: immediate: true,
handler() {
this.empInfo = this.empData;
}
}
}
</script>

<template>
<div>
<el-form :label-position="labelPosition" :model="empInfo" status-icon :rules="rules" ref="empInfo">
<el-form-item label="Employee Name" prop="name">
<el-input v-model="empInfo.markupName"></el-input>
</el-form-item>
<el-form-item label="EMployee Id" prop="id">
<el-input v-model="empInfo.id"></el-input>
</el-form-item>
<el-form-item label="Employee Age" prop="age">
<el-input v-model="empInfo.age"></el-input>
</el-form-item>
<el-button @click="getUserData">Submit</el-button>
</div>
</template>












share|improve this question

























  • You could just write a reset method in your component and initialize the values (with either empData or empty values) and call this method on created() and then recall this method on reset or after the data changes.

    – ssc-hrep3
    Nov 24 '18 at 8:30
















1















I have a parent component and I am assigning values in child component form. But after submitting the form I want form field should be empty.



I am using resetFields function for resetting value but when I am using these function it is setting last value, what I send it from parent component instead of reset value I want form fields should be empty.






<script>
export default {
props: ["empData"],
data {
return() {
empInfo: {
name: "",
id: null,
age: null
}
}
},
methods: {
getUserData() {
if (response.status == "SUCCESS") {
this.$refs['empInfo'].resetFields();
}
}
}
watch: {
empData: immediate: true,
handler() {
this.empInfo = this.empData;
}
}
}
</script>

<template>
<div>
<el-form :label-position="labelPosition" :model="empInfo" status-icon :rules="rules" ref="empInfo">
<el-form-item label="Employee Name" prop="name">
<el-input v-model="empInfo.markupName"></el-input>
</el-form-item>
<el-form-item label="EMployee Id" prop="id">
<el-input v-model="empInfo.id"></el-input>
</el-form-item>
<el-form-item label="Employee Age" prop="age">
<el-input v-model="empInfo.age"></el-input>
</el-form-item>
<el-button @click="getUserData">Submit</el-button>
</div>
</template>












share|improve this question

























  • You could just write a reset method in your component and initialize the values (with either empData or empty values) and call this method on created() and then recall this method on reset or after the data changes.

    – ssc-hrep3
    Nov 24 '18 at 8:30














1












1








1








I have a parent component and I am assigning values in child component form. But after submitting the form I want form field should be empty.



I am using resetFields function for resetting value but when I am using these function it is setting last value, what I send it from parent component instead of reset value I want form fields should be empty.






<script>
export default {
props: ["empData"],
data {
return() {
empInfo: {
name: "",
id: null,
age: null
}
}
},
methods: {
getUserData() {
if (response.status == "SUCCESS") {
this.$refs['empInfo'].resetFields();
}
}
}
watch: {
empData: immediate: true,
handler() {
this.empInfo = this.empData;
}
}
}
</script>

<template>
<div>
<el-form :label-position="labelPosition" :model="empInfo" status-icon :rules="rules" ref="empInfo">
<el-form-item label="Employee Name" prop="name">
<el-input v-model="empInfo.markupName"></el-input>
</el-form-item>
<el-form-item label="EMployee Id" prop="id">
<el-input v-model="empInfo.id"></el-input>
</el-form-item>
<el-form-item label="Employee Age" prop="age">
<el-input v-model="empInfo.age"></el-input>
</el-form-item>
<el-button @click="getUserData">Submit</el-button>
</div>
</template>












share|improve this question
















I have a parent component and I am assigning values in child component form. But after submitting the form I want form field should be empty.



I am using resetFields function for resetting value but when I am using these function it is setting last value, what I send it from parent component instead of reset value I want form fields should be empty.






<script>
export default {
props: ["empData"],
data {
return() {
empInfo: {
name: "",
id: null,
age: null
}
}
},
methods: {
getUserData() {
if (response.status == "SUCCESS") {
this.$refs['empInfo'].resetFields();
}
}
}
watch: {
empData: immediate: true,
handler() {
this.empInfo = this.empData;
}
}
}
</script>

<template>
<div>
<el-form :label-position="labelPosition" :model="empInfo" status-icon :rules="rules" ref="empInfo">
<el-form-item label="Employee Name" prop="name">
<el-input v-model="empInfo.markupName"></el-input>
</el-form-item>
<el-form-item label="EMployee Id" prop="id">
<el-input v-model="empInfo.id"></el-input>
</el-form-item>
<el-form-item label="Employee Age" prop="age">
<el-input v-model="empInfo.age"></el-input>
</el-form-item>
<el-button @click="getUserData">Submit</el-button>
</div>
</template>








<script>
export default {
props: ["empData"],
data {
return() {
empInfo: {
name: "",
id: null,
age: null
}
}
},
methods: {
getUserData() {
if (response.status == "SUCCESS") {
this.$refs['empInfo'].resetFields();
}
}
}
watch: {
empData: immediate: true,
handler() {
this.empInfo = this.empData;
}
}
}
</script>

<template>
<div>
<el-form :label-position="labelPosition" :model="empInfo" status-icon :rules="rules" ref="empInfo">
<el-form-item label="Employee Name" prop="name">
<el-input v-model="empInfo.markupName"></el-input>
</el-form-item>
<el-form-item label="EMployee Id" prop="id">
<el-input v-model="empInfo.id"></el-input>
</el-form-item>
<el-form-item label="Employee Age" prop="age">
<el-input v-model="empInfo.age"></el-input>
</el-form-item>
<el-button @click="getUserData">Submit</el-button>
</div>
</template>





<script>
export default {
props: ["empData"],
data {
return() {
empInfo: {
name: "",
id: null,
age: null
}
}
},
methods: {
getUserData() {
if (response.status == "SUCCESS") {
this.$refs['empInfo'].resetFields();
}
}
}
watch: {
empData: immediate: true,
handler() {
this.empInfo = this.empData;
}
}
}
</script>

<template>
<div>
<el-form :label-position="labelPosition" :model="empInfo" status-icon :rules="rules" ref="empInfo">
<el-form-item label="Employee Name" prop="name">
<el-input v-model="empInfo.markupName"></el-input>
</el-form-item>
<el-form-item label="EMployee Id" prop="id">
<el-input v-model="empInfo.id"></el-input>
</el-form-item>
<el-form-item label="Employee Age" prop="age">
<el-input v-model="empInfo.age"></el-input>
</el-form-item>
<el-button @click="getUserData">Submit</el-button>
</div>
</template>






vue.js vuejs2 element-ui






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 15:41









tony19

23.5k44175




23.5k44175










asked Nov 24 '18 at 8:13









user3739946user3739946

62




62













  • You could just write a reset method in your component and initialize the values (with either empData or empty values) and call this method on created() and then recall this method on reset or after the data changes.

    – ssc-hrep3
    Nov 24 '18 at 8:30



















  • You could just write a reset method in your component and initialize the values (with either empData or empty values) and call this method on created() and then recall this method on reset or after the data changes.

    – ssc-hrep3
    Nov 24 '18 at 8:30

















You could just write a reset method in your component and initialize the values (with either empData or empty values) and call this method on created() and then recall this method on reset or after the data changes.

– ssc-hrep3
Nov 24 '18 at 8:30





You could just write a reset method in your component and initialize the values (with either empData or empty values) and call this method on created() and then recall this method on reset or after the data changes.

– ssc-hrep3
Nov 24 '18 at 8:30












2 Answers
2






active

oldest

votes


















0














The Element UI form seems to latch onto the data it gets initialized with, and resetFields() reinitializes the form with that data. You could defer the watcher until the next tick so that the component's initially empty empInfo could be propagated to the Element UI form as initial data, which would then be used in resetFields().



watch: {
empData: {
immediate: true,
handler(value) {
this.$nextTick(() => {
if (value) {
this.empInfo = value;
}
});
}
}
}


demo






share|improve this answer
























  • Thanks for ur help...Its working Fine.

    – user3739946
    Nov 26 '18 at 8:38











  • @user3739946 No problem :)

    – tony19
    Nov 26 '18 at 17:02



















0














If you use Parent and Child component, it is good practise to emit data as output to parent component and then process data. This parent child pattern is good for making dynamic form. Reset method cleares your form.



getUserData() {
if (this.$refs.empInfo.validate()) {
this.$emit('get-user-data', this.empInfo);
this.$refs.empInfo.reset();
}
}





share|improve this answer





















  • 1





    Although this code might solve the question, a good answer should always explain what the problem was and how this code helps.

    – BDL
    Nov 26 '18 at 9:16











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53456387%2fvue-js-form-field-should-be-empty%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














The Element UI form seems to latch onto the data it gets initialized with, and resetFields() reinitializes the form with that data. You could defer the watcher until the next tick so that the component's initially empty empInfo could be propagated to the Element UI form as initial data, which would then be used in resetFields().



watch: {
empData: {
immediate: true,
handler(value) {
this.$nextTick(() => {
if (value) {
this.empInfo = value;
}
});
}
}
}


demo






share|improve this answer
























  • Thanks for ur help...Its working Fine.

    – user3739946
    Nov 26 '18 at 8:38











  • @user3739946 No problem :)

    – tony19
    Nov 26 '18 at 17:02
















0














The Element UI form seems to latch onto the data it gets initialized with, and resetFields() reinitializes the form with that data. You could defer the watcher until the next tick so that the component's initially empty empInfo could be propagated to the Element UI form as initial data, which would then be used in resetFields().



watch: {
empData: {
immediate: true,
handler(value) {
this.$nextTick(() => {
if (value) {
this.empInfo = value;
}
});
}
}
}


demo






share|improve this answer
























  • Thanks for ur help...Its working Fine.

    – user3739946
    Nov 26 '18 at 8:38











  • @user3739946 No problem :)

    – tony19
    Nov 26 '18 at 17:02














0












0








0







The Element UI form seems to latch onto the data it gets initialized with, and resetFields() reinitializes the form with that data. You could defer the watcher until the next tick so that the component's initially empty empInfo could be propagated to the Element UI form as initial data, which would then be used in resetFields().



watch: {
empData: {
immediate: true,
handler(value) {
this.$nextTick(() => {
if (value) {
this.empInfo = value;
}
});
}
}
}


demo






share|improve this answer













The Element UI form seems to latch onto the data it gets initialized with, and resetFields() reinitializes the form with that data. You could defer the watcher until the next tick so that the component's initially empty empInfo could be propagated to the Element UI form as initial data, which would then be used in resetFields().



watch: {
empData: {
immediate: true,
handler(value) {
this.$nextTick(() => {
if (value) {
this.empInfo = value;
}
});
}
}
}


demo







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 25 '18 at 15:41









tony19tony19

23.5k44175




23.5k44175













  • Thanks for ur help...Its working Fine.

    – user3739946
    Nov 26 '18 at 8:38











  • @user3739946 No problem :)

    – tony19
    Nov 26 '18 at 17:02



















  • Thanks for ur help...Its working Fine.

    – user3739946
    Nov 26 '18 at 8:38











  • @user3739946 No problem :)

    – tony19
    Nov 26 '18 at 17:02

















Thanks for ur help...Its working Fine.

– user3739946
Nov 26 '18 at 8:38





Thanks for ur help...Its working Fine.

– user3739946
Nov 26 '18 at 8:38













@user3739946 No problem :)

– tony19
Nov 26 '18 at 17:02





@user3739946 No problem :)

– tony19
Nov 26 '18 at 17:02













0














If you use Parent and Child component, it is good practise to emit data as output to parent component and then process data. This parent child pattern is good for making dynamic form. Reset method cleares your form.



getUserData() {
if (this.$refs.empInfo.validate()) {
this.$emit('get-user-data', this.empInfo);
this.$refs.empInfo.reset();
}
}





share|improve this answer





















  • 1





    Although this code might solve the question, a good answer should always explain what the problem was and how this code helps.

    – BDL
    Nov 26 '18 at 9:16
















0














If you use Parent and Child component, it is good practise to emit data as output to parent component and then process data. This parent child pattern is good for making dynamic form. Reset method cleares your form.



getUserData() {
if (this.$refs.empInfo.validate()) {
this.$emit('get-user-data', this.empInfo);
this.$refs.empInfo.reset();
}
}





share|improve this answer





















  • 1





    Although this code might solve the question, a good answer should always explain what the problem was and how this code helps.

    – BDL
    Nov 26 '18 at 9:16














0












0








0







If you use Parent and Child component, it is good practise to emit data as output to parent component and then process data. This parent child pattern is good for making dynamic form. Reset method cleares your form.



getUserData() {
if (this.$refs.empInfo.validate()) {
this.$emit('get-user-data', this.empInfo);
this.$refs.empInfo.reset();
}
}





share|improve this answer















If you use Parent and Child component, it is good practise to emit data as output to parent component and then process data. This parent child pattern is good for making dynamic form. Reset method cleares your form.



getUserData() {
if (this.$refs.empInfo.validate()) {
this.$emit('get-user-data', this.empInfo);
this.$refs.empInfo.reset();
}
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 26 '18 at 11:36

























answered Nov 25 '18 at 20:21









DaaronDaaron

149




149








  • 1





    Although this code might solve the question, a good answer should always explain what the problem was and how this code helps.

    – BDL
    Nov 26 '18 at 9:16














  • 1





    Although this code might solve the question, a good answer should always explain what the problem was and how this code helps.

    – BDL
    Nov 26 '18 at 9:16








1




1





Although this code might solve the question, a good answer should always explain what the problem was and how this code helps.

– BDL
Nov 26 '18 at 9:16





Although this code might solve the question, a good answer should always explain what the problem was and how this code helps.

– BDL
Nov 26 '18 at 9:16


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53456387%2fvue-js-form-field-should-be-empty%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)