首页 > 解决方案 > 如何使用 Vuejs 在数组中分配键索引

问题描述

嗨,我有这样的表格:

<tr v-for="(post, index) in posts" v-bind:index="index">
   <td>{{ post.rut }}</td>
   <td>{{ post.names }} {{ post.father_lastname }} {{ post.mother_lastname }}</td>
   <td>
      <input type="number" class="form-control" id="exampleInputEmail1" v-model="form.amount[index]" placeholder="Ingresa el monto">
   </td>
</tr>

你怎么能看到它有 v-model="form.amount[index]" 但我想做的是:

 <input type="number" class="form-control" id="exampleInputEmail1" v-model="form.amount[post.rut]" placeholder="Ingresa el monto">

我的意思是我想分配我自己的索引我的自定义索引我想知道我该怎么做?

我的 vuejs 代码是这样的:

   data: function() {
        return {
            form: {
                amount: [],
            },

我在 vuejs 中声明了数量数组,你可以在上面看到,但我需要在数组中分配我自己的索引,因为如果我发送数据并在此时检查数组,它是:

 0 => value, 1 => value

但我需要这样做

'2714155' =>  value, '4578745' => value...

我怎样才能做到这一点?谢谢

标签: laravelvue.js

解决方案


将您的数据声明为对象,然后将值分配给特定键。

data() {
    return {
        form: {
            amount: {},
        },
    }
}

您可以按原样使用所需的布局。

 <input 
    type="number" 
    class="form-control" 
    id="exampleInputEmail1" 
    v-model="form.amount[post.rut]" <-- here you assign a specific key
    placeholder="Ingresa el monto">

推荐阅读