I am getting some data from Database and pass it to the <tbody> by looping through it's rows. The rows are within a child component while the <tbody> imports them:
<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>
The "id" is unique and should be therefore representation of checked rows (checkboxes) in the "values" Array. Then am saving "values" in Vuex through mutating the action in the beforeUpdate() Lifecycle Hook and define a getter for it to be able to use this state everywhere in my app. On the other hand I am importing this child component and passing Data to it from the Array "tableBody". Just like this:
<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>
And here my State, mutation, action, and getter from my store.js file:
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;
The problem is, all this things just save the value of "id" at the actual row in the "values" Array. If I've checked five rows then is the "values" an Array of length 1 with a value of last checked row. But what I need is to fill this Array with the values of all checked rows.
I have seen a few examples where it works perfect through iterating <li> in <ul>. Maybe it depends on html Tags I am using?
It would be great to know what I am doing wrong and how I can fix it.