【发布时间】:2019-09-10 01:01:05
【问题描述】:
我从 Vue 开始,但遇到了一点困难。
在下图中,我有一个包含一些项目的表格:
每次选择一个项目并且数量增加时,我需要在我的addOptional 方法(可选)中,我的变量获取该项目的数量与该值连接。例如,如果我选择锤子看起来像这样 `
let variavel = opcional.Qtd + 'x' + opcional.Code
如果我给 console.log 结果将是 2x1
但是如果我选择另一个选项,例如 Serrote,我应该将第一个选项加入同一个变量中,并用 Pipe (|) 分隔,示例如下所示。
2x1 | 1x2
我该怎么做?我应该使用数组吗?
我已经拥有的:
new Vue({
el: '#app',
data() {
return {
Opcionais: [
{ Code: 1, Nome: 'Martelo', Valor: 50.00, Qtd: 0 },
{ Code: 2, Nome: 'Serrote', Valor: 50.00, Qtd: 0 },
{ Code: 3, Nome: 'Prego', Valor: 60.00, Qtd: 0 }
]
}
},
methods: {
addOpcional(opcional) {
// The variable below should receive the value of the quantity plus the code. If more than one option is chosen the variable must receive this new value and separate with pipe example Qty (2) x Code (2) | Qty (3) x Code (2)
opcional.Qtd += 1
let Code = [opcional.Qtd + 'x' + opcional.Code]
},
remove(opcional) {
}
}
})
<script src="https://unpkg.com/vue"></script>
<div id="app">
<template>
<div class="usuario-lista">
<table>
<thead>
<tr>
<th>#Code</th>
<th>Nome</th>
<th>Valor Unitário</th>
<th>Valor Total</th>
</tr>
</thead>
<tbody>
<tr v-for="opcional in Opcionais" :key="opcional.Code">
<td>
<button @click="opcional.Qtd ? opcional.Qtd-- : false">-</button>
<input type="text" :value="opcional.Qtd">
<button @click="addOpcional(opcional)">+</button>
</td>
<td>{{ opcional.Nome }}</td>
<td>{{ opcional.Valor }}</td>
<td>{{ opcional.Valor * opcional.Qtd }}</td>
</tr>
</tbody>
</table>
</div>
</template>
</div>
【问题讨论】:
标签: javascript vue.js