将多个输入添加到嵌套数组

huangapple go评论64阅读模式
英文:

Add multiple inputs to nested array

问题

const informations = reactive([
  {
    title: {
      en: '',
      fr: '',
      de: '',
    },
    values: [
      {
        en: '',
        fr: '',
        de: '',
      },
    ],
  },
])

function addInformation() {
  informations.push({
    title: {
      en: '',
      fr: '',
      de: '',
    },
    values: [
      {
        en: '',
        fr: '',
        de: '',
      },
    ],
  })
}

function addValue(infIndex, valIndex) {
  informations[infIndex].values.splice(valIndex, 0, {
    en: '',
    fr: '',
    de: '',
  })
}
英文:

I want to create an array of informations. Each information contains an object as a title with 3 different languages and multiple values, where each value also contains 3 different languages.

With the help of this question, I've managed to add multiple informations with their own title and their first value. However, I have problems adding more values to each information. So we are looking at the addValue() function. Here I want to add the new value object in informations[infIndex].values, but I really have no idea how to do it. With push() or slice() I get informations[infIndex].values.slice is not a function.

Html

    <div class="informations">
      <h1>Informations</h1>
      <div v-for="(information, infIndex) in informations" :key="infIndex">
        <p>Title</p>
        <input v-model="information.title.en" placeholder="EN" />
        <input v-model="information.title.fr" placeholder="FR" />
        <input v-model="information.title.de" placeholder="DE" />

        <div
          v-for="(value, valIndex) in informations[infIndex].values"
          :key="valIndex"
        >
          <p>Values</p>
          <input v-model="value.en" placeholder="EN" />
          <input v-model="value.fr" placeholder="FR" />
          <input v-model="value.de" placeholder="DE" />
          <button @click="addValue(infIndex, valIndex)">Add Value</button>
        </div>
      </div>
      <button @click="addInformation()">Add Information</button>
      <pre>{{ informations }}</pre>
    </div>

Script setup

const informations = reactive([
  {
    title: {
      en: '',
      fr: '',
      de: '',
    },
    values: {
      value: {
        en: '',
        fr: '',
        de: '',
      },
    },
  },
])

function addInformation() {
  informations.push({
    title: {
      en: '',
      fr: '',
      de: '',
    },
    values: {
      value: {
        en: '',
        fr: '',
        de: '',
      },
    },
  })
}

function addValue(infIndex, valIndex) {
  informations[infIndex].values.splice(valIndex, 0, {
    value: {
      en: '',
      fr: '',
      de: '',
    },
  })
}

将多个输入添加到嵌套数组

将多个输入添加到嵌套数组

答案1

得分: 1

informations[infIndex].values被您当作数组来处理,但您已将其声明为对象:

values: {
      value: {
        en: '',
        fr: '',
        de: '',
      },
    },

valIndex不是实际的索引号,它是一个对象键,因为您在调用v-for时使用了一个对象:

<div
  v-for="(value, valIndex) in informations[infIndex].values"
  :key="valIndex"
>

最简单的方法是将values最初更改为数组,并且可以删除value键,这样它只是一个包含对象的简单数组:

const informations = reactive([
  {
    title: {
      en: '',
      fr: '',
      de: ''
    },
    values: [
      {
        en: '',
        fr: '',
        de: ''
      }
    ]
  }
]);

function addInformation() {
  informations.push({
    title: {
      en: '',
      fr: '',
      de: ''
    },
    values: [
      {
        en: '',
        fr: '',
        de: ''
      }
    ]
  });
}

function addValue(infIndex) {
  informations[infIndex].values.push({
    en: '',
    fr: '',
    de: ''
  });
}
英文:

You're treating informations[infIndex].values like an array but you've declared it as an object:

values: {
      value: {
        en: &#39;&#39;,
        fr: &#39;&#39;,
        de: &#39;&#39;,
      },
    },

valIndex is not an actual index number, it's an object key, because you're calling v-for with an object:

&lt;div
  v-for=&quot;(value, valIndex) in informations[infIndex].values&quot;
  :key=&quot;valIndex&quot;
&gt;

It would be easiest to just change values to initially be an array, and I would get rid of the value key as well so it's just a simple array of objects:

const informations = reactive([
  {
    title: {
      en: &#39;&#39;,
      fr: &#39;&#39;,
      de: &#39;&#39;
    },
    values: [
      {
        en: &#39;&#39;,
        fr: &#39;&#39;,
        de: &#39;&#39;
      }
    ]
  }
]);

function addInformation() {
  informations.push({
    title: {
      en: &#39;&#39;,
      fr: &#39;&#39;,
      de: &#39;&#39;
    },
    values: [
      {
        en: &#39;&#39;,
        fr: &#39;&#39;,
        de: &#39;&#39;
      }
    ]
  });
}

function addValue(infIndex) {
  informations[infIndex].values.push({
    en: &#39;&#39;,
    fr: &#39;&#39;,
    de: &#39;&#39;
  });
}

huangapple
  • 本文由 发表于 2023年4月4日 17:28:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75927728.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定