In my Component called IncomeList
, there is a method named sumValue
. This method simply adds different numbers together to produce one value, for example 3+5 = 8. Similarly, in another Component named OutputList
, the same logic is applied but with a method called sumValueOutput
. Now, I want to utilize both values in a new component named WinTotal
. I've attempted using props and Vuex, however, I haven't been successful so far and I'm unsure where to begin. Thank you for your assistance!
IncomeList:
<template>
<div class="container-income">
<button class="btn btn-info" @click="showModal">show modal</button>
<div class="hidden-container-income" id="test123">
<input type="text" class="income-input" placeholder="What needs to be done" v-model="newIncome" @keyup.enter="addincome">
<input type="text" class="value-input" placeholder="€" v-model="newIncomeValue" @keyup.enter="addValue">
<transition-group name="fade" enter-active-class="animated fadeInUp" leave-active-class="animated fadeOutDown">
<income-item v-for="income in incomesFiltered" :key="income.id" :income="income"
@removedIncome="removeincome">
</income-item>
</transition-group>
<div class="extra-container">
<div><label><input type="checkbox" style="display: none" :checked="!anyRemaining" @change="checkAllincomes"></label></div>
<div>{{ remaining }} elements</div>
</div>
<div class="sum-container">
<div><label> Total Income: </label></div>
<div>{{ sumValue }} €</div>
</div>
</div>
</div>
</template>
<script>
import IncomeItem from './IncomeItem'
export default {
name: 'income-list',
components: {
IncomeItem,
},
data () {
return {
newIncome: '',
newIncomeValue: '',
idForincome: 3,
incomes: [
{
'id': 1,
'title': 'Finish Vue Screencast',
'value': 300,
'completed': false,
'editing': false,
},
{
'id': 2,
'title': 'Take over world',
'value': 315,
'completed': false,
'editing': false,
},
{
'id': 3,
'title': 'Excellent',
'value': 313,
'completed': false,
'editing': false,
},
]
}
},
computed: {
remaining() {
return this.incomes.filter(income => !income.completed).length
},
anyRemaining() {
return this.remaining != 0
},
incomesFiltered() {
return this.incomes
},
sumValue() {
return this.incomesFiltered.reduce((a, c) => a + c.value, 0)
},
},
methods: {
addincome() {
if (this.newIncome.trim().length == 0) {
return
}
this.incomes.push({
id: this.idForincome,
title: this.newIncome,
value: this.newIncomeValue,
completed: false,
})
this.newIncome = ''
this.newIncomeValue = ''
this.this.idForincome++
},
removeincome(id) {
const index = this.incomes.findIndex((item) => item.id == id)
this.incomes.splice(index, 1)
},
checkAllincomes() {
this.incomes.forEach((income) => income.completed = event.target.checked)
},
clearCompleted() {
this.incomes = this.incomes.filter(income => !income.completed)
},
finishedEdit(data) {
const index = this.incomes.findIndex((item) => item.id == data.id)
this.incomes.splice(index, 1, data)
},
//Same for Value
addValue() {
if (this.newIncomeValue.trim().length == 0) {
return
}
this.incomes.push({
id: this.idForincome,
title: this.newIncome,
value: this.newIncomeValue,
completed: false,
})
this.newIncome = ''
this.newIncomeValue = ''
this.this.idForincome++
},
showModal () {
if (document.getElementById('test123').style.display == 'none' ) {
document.getElementById('test123').style.display = 'block';
}
else {
document.getElementById('test123').style.display = 'none'
}
},
},
};
</script>
OutputList:
<template>
<div class="container-output1">
<button class="btn btn-info1" @click="showModal">show modal</button>
<div class="hidden-container-output1" id="test1231">
<input type="text" class="output-input1" placeholder="What needs to be done" v-model="newOutput" @keyup.enter="addoutput">
<input type="text" class="value-input1" placeholder="€" v-model="newOutputValue" @keyup.enter="addValue">
<transition-group name="fade" enter-active-class="animated fadeInUp" leave-active-class="animated fadeOutDown">
<output-item v-for="output in outputsFiltered" :key="output.id" :output="output"
@removedoutput="removeOutput">
</output-item>
</transition-group>
<div class="extra-container1">
<div><label><input type="checkbox" style="display: none" :checked="!anyRemaining" @change="checkAlloutputs"></label></div>
<div>{{ remaining }} elements</div>
</div>
<div class="sum-container1">
<div><label> Total Output: </label></div>
<div>{{ sumValueOutput }} €</div>
</div>
</div>
</div>
</template>
<script>
import OutputItem from './OutputItem'
export default {
name: 'output-list',
components: {
OutputItem,
},
data () {
return {
newOutput: '',
newOutputValue: '',
idForOutput: 3,
outputs: [
{
'id': 1,
'title': 'Finish Vue Screencast',
'value': 300,
'completed': false,
'editing': false,
},
{
'id': 2,
'title': 'Take over world',
'value': 315,
'completed': false,
'editing': false,
},
{
'id': 3,
'title': 'Excellent',
'value': 311,
'completed': false,
'editing': false,
},
]
}
},
computed: {
remaining() {
return this.outputs.filter(output => !output.completed).length
},
anyRemaining() {
return this.remaining != 0
},
outputsFiltered() {
return this.outputs
},
sumValueOutput() {
var outputValue = this.outputsFiltered.reduce((a, c) => a + c.value, 0);
return outputValue;
},
},
methods: {
addOutput() {
if (this.newOutput.trim().length == 0) {
return
}
this.outputs.push({
id: this.idForOutput,
title: this.newOutput,
value: this.newOutputValue,
completed: false,
})
this.newOutput = ''
this.newOutputValue = ''
this.this.idForOutput++
},
removeOutput(id) {
const index = this.outputs.findIndex((item) => item.id == id)
this.outputs.splice(index, 1)
},
checkAlloutputs() {
this.outputs.forEach((output) => output.completed = event.target.checked)
},
clearCompleted() {
this.outputs = this.outputs.filter(output => !output.completed)
},
finishedEdit(data) {
const index = this.outputs.findIndex((item) => item.id == data.id)
this.outputs.splice(index, 1, data)
},
//Same for Value
addValue() {
if (this.newOutputValue.trim().length == 0) {
return
}
this.outputs.push({
id: this.idForOutput,
title: this.newOutput,
value: this.newOutputValue,
completed: false,
})
this.newOutput = ''
this.newOutputValue = ''
this.this.idForOutput++
},
showModal () {
if (document.getElementById('test1231').style.display == 'none' ) {
document.getElementById('test1231').style.display = 'block';
}
else {
document.getElementById('test1231').style.display = 'none'
}
}
}
}
</script>