简体   繁体   English

如何在vue.js中将从状态检索的数组数据分配给数组数据对象的v模型?

[英]How to assign array data retrieved from state to v-model of array data object in vue.js?

I have a list of applicants from vuex state retrieved using the vuex getters. 我有一个使用vuex getters从vuex状态检索的申请人列表。 and from the front end side, I wat to record their respective results as an array and submit their results. 从前端开始,我想将它们各自的结果记录为数组并提交结果。 but I can't assign the applicants id to v-model. 但我无法将申请人ID分配给v模型。

I have tried using an array and array objects but does not work. 我试过使用数组和数组对象,但是不起作用。 I have tried to loop the list of applicants on the page create and assign to the array data declared in Vue data. 我试图在页面上循环创建申请人列表,并将其分配给Vue数据中声明的数组数据。

Template Code 模板代码

<template>
<table style="margin-left:10%;">
          <tr>
            <th>#</th>
            <th>Full Name</th>
            <th>Result</th>
          </tr>
          <tr
            v-for="(applicant, key) in getApplicants"
            :key="applicant.applicant_id"
          >
            <td>{{ key + 1 }}</td>
            <td>
              {{ applicant.first_name }} {{ applicant.middle_name }}
              {{ applicant.last_name }}
              <b-form-input v-model="record.applicant_id[key]"></b-form-input>
            </td>
            <td>
              <b-form-input v-model="record.exam_result[key]"></b-form-input>
            </td>
          </tr>
        </table>
<template>

script code 脚本代码

data() {
    return {
      update: false,
      record: {
        job_vacancy_id: null,
        exam_type_id: null,
        applicant_id: [],
        exam_result: []
      }
    };

methods:{
 save() {
      var object = {
        job_vacancy_id: this.record.job_vacancy_id,
        manpower_requisition_id: this.manpower_requisition_id,
        applicants_id: this.applicants_id,
        exam_result: this.record.exam_result
      };
      console.log(object);
    },
populate() {
      let appliacnts = this.getApplicants;
      for (var i = 0; i <= appliacnts.length; i++) {
        this.record.applicant_id = this.appliacnts.applicant_id[i];
      }
    }
},

created() {
    this.populate();
  }

There are several mistakes: 有几个错误:

  1. this.record.applicant_id is an array, so you need an index on the end, before the = . this.record.applicant_id是一个数组,因此您需要在=之前结尾处有一个索引。
  2. this.appliacnts should not have a this . this.appliacnts不应具有this
  3. this.appliacnts.applicant_id[i] has the index on the wrong part. this.appliacnts.applicant_id[i]的索引在错误的部分。 It should be before applicant_id . 它应该在applicant_id之前。
  4. appliacnts is not spelt correctly. appliacnts拼写不正确。 It is consistent though, so it isn't causing any problems. 但是,它是一致的,因此不会引起任何问题。

Correcting for all that we get: 更正我们得到的一切:

populate() {
  const applicants = this.getApplicants;
  for (var i = 0; i <= applicants.length; i++) {
    this.record.applicant_id[i] = applicants[i].applicant_id;
  }
}

It's not a particularly safe implementation though. 但是,这并不是一个特别安全的实现。 If it gets called again and the array is shorter it'll leave the old entries in the array. 如果再次调用它并且数组更短,它将把旧条目保留在数组中。 I'd be more inclined to write it like this: 我更倾向于这样写:

populate() {
  const applicants = this.getApplicants;
  this.record.applicant_id = applicants.map(applicant => applicant.applicant_id);
}

I think it should be this.record.applicant_id[i] = appliacnts.applicant_id[i] 我认为应该是this.record.applicant_id[i] = appliacnts.applicant_id[i]

populate() {
      let appliacnts = this.getApplicants;
      for (var i = 0; i <= appliacnts.length; i++) {
        this.record.applicant_id[i] = appliacnts.applicant_id[i];
      }
    }
},

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

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