No More Posting New Topics!

If you have a question or an issue, please start a thread in our Github Discussions Forum.
This forum is closed for new threads/ topics.

Navigation

    Quasar Framework

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search

    Using associative array in v-model

    Help
    2
    2
    605
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • D
      dirkhd last edited by

      I am developing a simple survey module with Quasar. Definition of the survey is in data() and all answers I like to store in answer_object, for instance:

      data() {
          return {
            answer_object: [],
            survey: {
              surveyname: "Survey 1",
              generateNewSurveyID: true,
              questions: {
                q1: {
                  type: "open",
                  varname: "age",
                  text: "How old are you?"
                },
                q2: {
                  type: "single",
                  varname: "gender",
                  text: "Your gender?",
                  options: {
                    opt: {
                      value: 1,
                      label: "male"
                    },
                    opt: {
                      value: 2,
                      label: "female"
                    }
                  }
                }
            }
          }
        };
      }
      

      I like to store the answers in an associative array and take the “varname” as key:

      answer_object['age']
      

      So I tried to use this in the v-model but I can’t figure out how to do this - have tried quite a lot of approaches but I guess something here I do not understand:

      Just one approach as illustration that I have tried to demonstate what I wanna do:

      <q-card-section v-for="(quest,index) in survey.questions" :key="index">
          <div v-if="quest.type=='open'">
             <q-input v-model="answer_object[quest.varname]" type="text" :label="quest.text" />
          </div>
      </q-card-section>
      

      Best
      Dirk

      1 Reply Last reply Reply Quote 0
      • s.molinari
        s.molinari last edited by

        There are a few caveats to Vue’s reactivity system, when handling arrays. One is, you can’t add array items via named keys.

        https://vuejs.org/v2/guide/list.html#Caveats

        Scott

        1 Reply Last reply Reply Quote 1
        • First post
          Last post