1

I am working on creating custom fields in my application. I am allowing clients to define custom fields for various objects (ie. Initiative) and displaying them when updating or creating that object.

In my state I define the object being modified or added:

(initiatives.js)

addEditInitiative: {
        name: '',
        description: '',
        product_name: '',
        product_id: '',
        quarter: '',
        year: '',
        custom_fields: []
    },

the custom_fields array is filled with the custom fields defined for initiatives. For example, in the json response from the database, the custom fields array will include something like this...

  "payload": [
    {
      "id": "5dc840c3d27a6e47b9baec33",
      "cid": "5d8502a2a284b46f3621f389",
      "name": "2",
      "description": "",
      "product_name": "Maps",
      "product_id": "5d86509ee24692444d84b155",
      "quarter": "Q2",
      "year": "2019",
      "custom_fields": [
        {
          "id": "5db8ec9fee8040e9b6dfad87",
          "cid": "5d8502a2a284b46f3621f389",
          "name": "Test",
          "type": "text",
          "form": "initiative",
          "value": ""
        },
        {
          "id": "5dba0bcedf9cbf185683ecca",
          "cid": "5d8502a2a284b46f3621f389",
          "name": "test2",
          "type": "text",
          "form": "initiative",
          "value": ""
        }
      ]
    }
  ]
}

I am trying to edit the value for each of those custom fields through vuex map fields, or if that doesnt work, some other way that isn't causing the error I am getting now. I am not mutating the state. I am directly using v-model on the state.

(Vue Component)

                    <v-item v-for="(field, index) in initiativeFields"
                            v-bind:index="index"
                            v-bind:key="field.id">
                        <v-text-field v-if="field.type = 'text'"
                                      :label="field.name"
                                      type="text"
                                      v-model="addEditInitiative.custom_fields[index].value">
                        </v-text-field>
                    </v-item>

I am not sure how to replace v-model="addEditInitiative.custom_fields[index].value" with something that will mutate the state. I have been using https://github.com/maoberlehner/vuex-map-fields for this for simple fields. For Example,

(Vue Component)

...mapFields('initiative', [
                'addEditInitiative.name',
                'addEditInitiative.description',
                'addEditInitiative.product_name',
                'addEditInitiative.product_id',
                'addEditInitiative.quarter',
                'addEditInitiative.year',
            ]),
4
  • What's the error you're seeing? Also, providing a codepen or a jsfiddle would help Commented Nov 20, 2019 at 1:33
  • 1
    Try using multiple row fields. github.com/maoberlehner/vuex-map-fields#multi-row-fields Commented Nov 20, 2019 at 8:45
  • Multiple row fields helped! If you add this as an answer I can give you the 50 reputation I think. Commented Nov 21, 2019 at 3:46
  • sure. I am adding an answer. Commented Nov 22, 2019 at 8:50

2 Answers 2

2
+50

Try using multiple row fields.

https://github.com/maoberlehner/vuex-map-fields#multi-row-fields

Sign up to request clarification or add additional context in comments.

Comments

0

v-model="test" is actually just a shorthand for :value="test" @input="test = arguments[0]" (or :value="test" @input="test = $event.target.value" for native components like input).

You can use this to your advantage to refactor your code a bit to something like this:

<v-item v-for="(field, index) in initiativeFields"
        v-bind:index="index"
        v-bind:key="field.id">
    <v-text-field v-if="field.type = 'text'"
                  :label="field.name"
                  type="text"
                  :value="addEditInitiative.custom_fields[index].value"
                  @input="updateValue($event, index)">
    </v-text-field>
</v-item>

And in your updateValue method you could update the store with an action/mutation.

2 Comments

My action is updateValue: function ({commit}, {value, index}) { commit('updateCustomField', value, index) }
Mutation is updateCustomField(state, value, index) { state.addEditInitiative.custom_fields[index].value = value },

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.