简体   繁体   English

$refs resetFields 不是 function AntDesign

[英]$refs resetFields not a function AntDesign

I have a problem with reseting fields of my form.我在重置表单字段时遇到问题。

I have a form.我有一个表格。 The user can adding another forms and another...用户可以添加另一个 forms 和另一个...

If everything is OK, I would like to recording data in my DB and in my store AND reset all of inputs of my form.如果一切正常,我想在我的数据库和商店中记录数据并重置表单的所有输入。 This is this last point I cannot do.这是我不能做的最后一点。

I have tried different solutions but It does not work.我尝试了不同的解决方案,但它不起作用。

This is my code:这是我的代码:

      <div v-for="(question, index) in questionsSignaletiques" :key="index" class="blockQuestion" >
            <!--form to add questions : one form per question, each form have a different name in the ref -->
            <a-form-model
              layout="inline"
              :ref="'p' + index"
            >

              <p>New question</p>

              <a-alert v-if="question.error.here" type="error" :message="question.error.message" show-icon />

              <a-form-model-item>
                <a-input v-model="question.question.type" placeholder="Title">
                </a-input>
              </a-form-model-item>

              <a-form-model-item>
                <a-input v-model="question.question.item" placeholder="Item">
                </a-input>
              </a-form-model-item>

              <br><br>
              <a-form-model-item label="Question multiple" prop="delivery">
                <a-switch v-model="question.question.multiple"  />
              </a-form-model-item>
              <a-form-model-item label="Obligatoire" prop="delivery">
                <a-switch v-model="question.question.obligatoire"  />
              </a-form-model-item>

              <br><br>

              <div class="blockChoices">
                <div v-for="subrow in question.question.choices">
                  <a-form-model-item>
                    <a-input v-model="subrow.name"  placeholder="Choix">
                    </a-input>
                  </a-form-model-item>
                </div>
              </div>
              <a-button @click="addChoice(question)" type="secondary">Add a choice</a-button>
          </a-form-model>
      </div>

      <div>
        <a-button @click="addItem" type="secondary">Add a question</a-button>
      </div>
      <br>

      <div>
        <a-button @click="submit" type="primary">Send</a-button>
      </div>

Javascript code: Javascript 代码:

  data() {
    return {
      idStudy: this.$route.params.id,
      aucuneQuestion:false,
      questionsSignaletiques: [
        {
          "study":"/api/studies/" + this.$route.params.id,
          "question":
            {
              type: "",
              item: "",
              multiple: false,
              obligatoire: false,
              inverse: false,
              barometre: false,
              originale: false,
              signaletik: true,
              choices: [{name:''}]
            },
            "error": {
            message:"",
              here:false
            }
        }
      ],

    }
  },

  mounted() {
//retreive all the questions still recorded
    axios
      .get('http://127.0.0.1:8000/api/studies/' + this.idStudy + '/question_studies?question.signaletik=true')
      .then((result)=>{
        console.log(result.data["hydra:member"])

        this.aucuneQuestion = result.data["hydra:member"].length === 0;

        //on met les données dans le store
        this.$store.commit("setListeQuestionsSignaletiques", result.data["hydra:member"])


      })
      .catch(err=>console.log(err))

  },



  methods: {
//Adding a question
    addItem () {
      this.questionsSignaletiques.push(
        {
          "study":"/api/studies/" + this.idStudy,
          "question":
            {
              type: "",
              item: "",
              multiple: false,
              obligatoire: false,
              inverse: false,
              barometre: false,
              originale: false,
              signaletik: true,
              choices: [{name:''}]
            },
          "error": {
            message:"",
            here:false
          }
        }
      )
    },

//adding a choice

    addChoice: function(choice) {
      choice.question.choices.push({
        name: ''
      })
    },

    // Sending the forms

    submit () {

     //loop table to retrieve all questions and indexes if the user adding several questions

      this.questionsSignaletiques.forEach((element,index) => 
      {

        const inputType = element.question.type
        const inputItem = element.question.item
        const inputChoice = element.question.choices


        //loop on choices to see if empty one or not 
        for (const oneChoice of inputChoice)
        {

          if ((inputChoice.length == 1) ||(inputChoice.length == 2 && oneChoice.name == ""))
          {
            element.error.here=true
            element.error.message = "You must have two choices at least"
            return false; // stop here if error
          }
          else
          {}
        }// end loop of choices

       // verify other fields

        if (inputType == "" || inputItem =="")
        {
          element.error.here=true
          element.error.message = "Title and item must not be empty"
        }
        else
        {
          // everything is ok we can record in db and store


          //reset fields == does not work

          this.$refs['p' + index][0].fields.resetField()
          //this.$refs['p' + index][0].resetFields();



          // adding questions in db one by one 
/*
            axios
              .post('http://127.0.0.1:8000/api/question_studies', element)
              .then((result)=>{
                console.log(result)


                //add in the state
                this.$store.commit("addQuestionSignaletique", element)



              })
              .catch(error => {
                console.log("ERRRR:: ",error);
              });
*/



        }




      }) //end loop foreach

    }
  }
};

Thanks a lot for help非常感谢您的帮助

EDIT AFTER THE FIRST ANSWER在第一个答案后编辑

Ok I didn't know.好吧,我不知道。 So I changed my mind: I added a "show" in my "data" which is true at the beginning.所以我改变了主意:我在我的“数据”中添加了一个“显示”,这在开始时是正确的。 If everything is ok, I save the question and set the show to false.如果一切正常,我会保存问题并将显示设置为 false。

The problem now is that when I have a question that is OK and the other one that is not.现在的问题是,当我有一个可以的问题而另一个不可以的问题时。 When I correct the question that was not ok and save it, BOTH questions go into the STATE.当我更正问题并保存时,两个问题 go 到 STATE。 So there is a duplicate in my state AND my DB... What can I do?所以我的 state 和我的数据库中有一个重复项......我该怎么办? This is the code:这是代码:

I just added this in the HTML:我刚刚在 HTML 中添加了这个:

<div v-for="(question, index) in questionsSignaletiques" :key="index"  >
            <a-form-model
              layout="inline"
              v-if="question.show"
              class="blockQuestion"
            >
...


  data() {
    return {
      idStudy: this.$route.params.id,
      aucuneQuestion:false,
      questionsSignaletiques: [
        {
          "study":"/api/studies/" + this.$route.params.id,
          "question":
            {
              type: "",
              item: "",
              multiple: false,
              obligatoire: false,
              inverse: false,
              barometre: false,
              originale: false,
              signaletik: true,
              choices: [{name:''}]
            },
          "error": {
              message:"",
              here:false
            },
          "show":true,
        }
      ],
    }
  },

  mounted() {

//retrieve question still recorded
    axios
      .get('http://127.0.0.1:8000/api/studies/' + this.idStudy + '/question_studies?question.signaletik=true')
      .then((result)=>{
        console.log(result.data["hydra:member"])

        this.aucuneQuestion = result.data["hydra:member"].length === 0;

        
        this.$store.commit("setListeQuestionsSignaletiques", result.data["hydra:member"])


      })
      .catch(err=>console.log(err))

  },

  methods: {

//adding a question

    addItem () {
      this.questionsSignaletiques.push(
        {
          "study":"/api/studies/" + this.idStudy,
          "question":
            {
              type: "",
              item: "",
              multiple: false,
              obligatoire: false,
              inverse: false,
              barometre: false,
              originale: false,
              signaletik: true,
              choices: [{name:''}]
            },
          "error": {
            message:"",
            here:false
          },
          "show":true
        }
      )
    },

//adding a choice

    addChoice: function(choice) {
      choice.question.choices.push({
        name: ''
      })
    },

    // submit the form
     submit () {


      this.questionsSignaletiques.forEach((element,index) =>
      {
          const inputType = element.question.type
          const inputItem = element.question.item
          const inputChoice = element.question.choices


          for (const oneChoice of inputChoice)
          {

            if ((inputChoice.length == 1) ||(inputChoice.length == 2 && oneChoice.name == ""))
            {
              element.error.here=true
              element.error.message = "You must have two choices at least"
              return false; //on s'arrête là si il y a une erreur
            }
            else
            {
              console.log("no problem")
            }
          }



          if (inputType == "" || inputItem =="")
          {
            element.error.here=true
            element.error.message = "You must fill type and item"
          }
          else
          {

            // hide the question form
            element.show =false

         //adding in db

            axios
              .post('http://127.0.0.1:8000/api/question_studies', element)
              .then((result)=>{

                //add in the state

                this.$store.commit("addQuestionSignaletique", element)


              })

              .catch(error => {
                console.log("ERRRR:: ",error);
              });



          }




      }) //end loop foreach

    }
  }
};

Thanks for help !感谢帮助 !

form.reset() does not work when using v-model.使用 v-model 时form.reset()不起作用。

Reset the reactive data instead.而是重置反应数据。

reset() {
    this.question.question.type = ""
    ...
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM