【问题标题】:How to pass down checkbox values from an iterating table row to an Array in Vue?如何将迭代表行中的复选框值传递给Vue中的数组?
【发布时间】:2018-10-30 20:36:31
【问题描述】:

我从数据库中获取一些数据并通过循环遍历它的行将其传递给<tbody>。这些行在子组件中,而 <tbody> 导入它们:

<template>
        <tr>
            <td>{{id}}</td>
            <td>{{indId}}</td>
            <td><input type="checkbox" :value="id" v-model="values">...</td>
        </tr>
</template>

<script>
import { mapActions } from 'vuex'
export default {
data() {
    return {
            values: []
    }
},
props: {
        id: {
            type: String,
            reuired: true
        },
        indId: {
            type: String,
            reuired: true
        },
methods: {
    ...mapActions([
        'selectValues'
    ])

},
beforeUpdate(){
        this.selectValues(this.values)
    }
}
</script>

“id”是唯一的,因此应该表示“values”数组中的选中行(复选框)。然后我通过改变beforeUpdate() Lifecycle Hook 中的操作在 Vuex 中保存“值”,并为其定义一个吸气剂,以便能够在我的应用程序的任何地方使用此状态。另一方面,我正在导入这个子组件并将数据从数组“tableBody”传递给它。就像这样:

<template>
        <table class="data-table">
        <tbody>    
             <tableBody 
             v-for="body in tableBody" :key="body.id"
            :id="body.id"
            :indId="body.ind_id"            
            />
        </tbody>    
        </table>
</template>

<script>
import tableBody from './TableParts/TableBody'
export default {
    components: {
        tableBody
    },
    props: {
        tableBody: {
            type: Array,
            required: true
        }
    }
}
</script>

这里是我的 store.js 文件中的 State、mutation、action 和 getter:

import Vuex from "vuex";
import axios from "axios";
const createStore = () => {
  return new Vuex.Store({
    state: {
      selectedValues: []
    },
    mutations: {
      selectValues(state, payload){
        state.selectedValues = payload;
      }
    },
    actions: {
      selectValues({commit}, payload){
        commit('selectValues',payload)
      } 
    },
    getters: {
      selectedValues(state){
        return state.selectedValues;
      }
    }
  });
};
export default createStore;

问题是,所有这些只是将“id”的值保存在“values”数组的实际行中。如果我检查了五行,那么“值”是一个长度为 1 的数组,其值为最后检查的行。但我需要的是用所有检查行的值填充这个数组。 我已经看到了一些通过在&lt;ul&gt; 中迭代&lt;li&gt; 来完美运行的例子。也许这取决于我正在使用的 html 标签? 很高兴知道我做错了什么以及如何解决它。

【问题讨论】:

  • 你能在保存数据的地方展示你的动作和突变吗?
  • 所以我已经编辑了我的问题

标签: arrays vue.js vue-component vuex


【解决方案1】:

tableBody 组件中的values 是本地数据,不会在行之间共享。

我看到你想将数据保存到 vuex,你可能想实现如下:

<template>
        <tr>
            <td>{{id}}</td>
            <td>{{indId}}</td>
            <td><input type="checkbox" :value="id" :checked="isSelected" @click="toggleValue">...</td>
        </tr>
</template>

<script>
import { mapActions, mapState } from 'vuex'
export default {
props: {
        id: {
            type: String,
            reuired: true
        },
        indId: {
            type: String,
            reuired: true
        },
methods: {
    ...mapActions([
        'selectValues',
        'deselectValues'
    ]),
    toggleValue() {
       if (!this.isSelected) {
         this.selectValues(this.id)
       } else {
        this.deselectValues(this.id)
       }
    }

},
computed: {
  ...mapState(['selectedValues']),
  isSelected () {
    return this.selectedValues && this.selectedValues.includes(this.id)
  }
}
</script>

要存储所有选中的行,在你的突变中,你需要连接数组,而不是重新分配它:

selectValues(state, payload){
  state.selectedValues = state.selectedValues.concat([payload])
}

deselectValues (state, payload) {
  state.selectedValues = state.selectedValues.filter(item => item !== payload)
}

【讨论】:

  • 你会在 deselectValues 突变中有错字吗?
  • 控制台显示:Cannot read property 'filter' of undefined
  • 1 票,因为它有效,但不是最佳答案,因为它没有完全解决问题并且运行速度非常慢
  • 很抱歉,deselectValues 中有拼写错误。它们应该是 state selectedValues 而不是 state.selectValues
  • 我有 3000 个对象,每个对象包含 6 个键值对
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-07
  • 2022-01-27
  • 2014-12-29
  • 2015-02-28
  • 2021-02-05
  • 1970-01-01
  • 2020-12-20
相关资源
最近更新 更多