link2740 link2741 link2742 link2743 link2744 link2745 link2746 link2747 link2748 link2749 link2750 link2751 link2752 link2753 link2754 link2755 link2756 link2757 link2758 link2759 link2760 link2761 link2762 link2763 link2764 link2765 link2766 link2767 link2768 link2769 link2770 link2771 link2772 link2773 link2774 link2775 link2776 link2777 link2778 link2779 link2780 link2781 link2782 link2783 link2784 link2785 link2786 link2787 link2788 link2789 link2790 link2791 link2792 link2793 link2794 link2795 link2796 link2797 link2798 link2799 link2800 link2801 link2802 link2803 link2804 link2805 link2806 link2807 link2808 link2809 link2810 link2811 link2812 link2813 link2814 link2815 link2816 link2817 link2818 link2819 link2820 link2821 link2822 link2823 link2824 link2825 link2826 link2827 link2828 link2829 link2830 link2831 link2832 link2833 link2834 link2835 link2836 link2837 link2838 link2839 link2840 link2841 link2842 link2843 link2844 link2845 link2846 link2847 link2848 link2849 link2850 link2851 link2852 link2853 link2854 link2855 link2856 link2857 link2858 link2859 link2860 link2861 link2862 link2863 link2864 link2865 link2866 link2867 link2868 link2869 link2870 link2871 link2872 link2873 link2874 link2875 link2876

[Vue.js] Child propagation to parent from object of objects

there is a table with column dropdowns and instead of creating objects for each (ie data: {col1: {},..}), there is a object of objects that is initialized and populated on “created”.

Thus, the issue I’m having is, the child components are not properly propagating changes to the parent component when the object being passed to the child prop is an object from an object of objects.

FYI the tableData is retrieved via axios.
I’m using quasar and vue.

I’ve initially had a single object being passed to the child components and was able to have sync modifier working properly. Then moved on to making an object of objects and passing to each column.

console.log(this.selected) in the child component selectionList is printing empty.

NOTE: this is an example that contains the root issue.

table.vue.js #parent

<q-table
row-key=”id”
:tableData
:columns
:visible-columns=”visibleColumns”>
<q-tr slot=”top-row” slot-scope=”props”>
<q-td v-for=”col in visibleColumns”>
<child-component
:selected.sync=”dropDownSelected[col]“
:options=”dropDownOptions[col]“
/>
</q-td>
</q-tr>
</q-table>

<script>
import ChildComponent from ‘path/to/child-component.vue’;
export default{
name: ‘table’,
components: {
‘child-component’: ChildComponent
},
data(){
return{
tableData: [{id: 1, col1: ‘a’, col2: ‘b’, col3: ‘c’}],
columns: [
{name: ‘col1’, label: ‘Col1’, field: ‘col1’},
{name: ‘col2’, label: ‘Col2’, field: ‘col2’},
{name: ‘col3’, label: ‘Col3’, field: ‘col3’},
],
visibleColumns: [‘col1’, ‘col2’],
dropDownSelected: {},
dropDownOptions: {}
}
},
created() {
this.initializeDropDown();
this.populateTableSelect();
},
methods: {
initializeDropDown(){
for(var col in this.columns){
this.dropDownSelected[col.name] = [];
this.dropDownOptions[col.name] = [];
}
},
populateTableSelect(){
// initiallize all columns with new Set
var tmp_set = {};
for(var col in this.dropDownOptions){
tmp_set[col] = new Set();
}
// iterate row, add items to set
for(var row = 0; row < this.tableData.length; row++){
for(var col in this.dropDownOptions){
tmp_set[col].add(this.tableData[row][col]);
}
}
for (var col in this.dropDownOptions) {
tmp_set[col] = […tmp_set[col]];
tmp_set[col].sort();
tmp_set[col] = tmp_set[col].map(item => ({ label: item, value: item }));
tmp_set[col].unshift({ label: ‘Select All’, value: ‘Select All’ });
this.dropDownOptions[col] = tmp_set[col];
}
}
},
}
</script>

child-component.vue

<q-select
multiple
:options=”options”
:value=”selected”
@input=”selectionList”>
</q-select>

<script>
export default{
name: ‘child-component’,
props: {
options: {type: Array, required: true},
selected: {type: Array, default: () => ([])},
allValue: { type: String, default: ‘Select All’ }
},
methods: {
selectionList(inputList){
// logic below used from outside source
if (inputList.find(item => item === this.allValue)){
if(this.selected.find(item => item === this.allValue)){
// case 1: one item is unchecked while ‘all’ is checked -> unchecks ‘all’ + keep other
inputList.splice(inputList.indexOf(this.allValue), 1);
this.$emit(‘update:selected’, inputList);
}
else {
// case 2: ‘all’ is checked -> select-all
this.$emit(‘update:selected’, this.options.map(option => option.value));
}
}
else {
if(this.selected.find(item => item === this.allValue)){
// case 3: unchecking ‘all’ -> clear selected
this.$emit(‘update:selected’, []);
}
else{
if(inputList.length === this.options.length -1){
// case 4: len is equal to options -> select-all
this.$emit(‘update:selected’, this.options.map(option => option.value));
}
else {
// case 5: check an item
this.$emit(‘update:selected’, inputList);
}
}
}
},
},
}
</script>

I expected passing an object from an object of objects would’ve allowed ‘Select All’ logic to propagate to the parent.

I see no errors coming so there is no clue as to what js is doing.

Solution :

From what I see, this is a simple reactivity problem explained in the documentation.
dropDownOptions and dropDownSelected are objects on which you add new keys. Every time you do that, you need to call this.$set.

I think if you only change this method you should be good:

initializeDropDown(){
for(var col in this.columns){
this.$set(this.dropDownSelected, col.name, []);
this.$set(this.dropDownOptions, col.name, []);
}
},