回答編集履歴
2
コメントを受けて追記
test
CHANGED
@@ -49,3 +49,211 @@
|
|
49
49
|
|
50
50
|
|
51
51
|
[算出 Setter 関数 | 算出プロパティとウォッチャ — Vue.js](https://jp.vuejs.org/v2/guide/computed.html#%E7%AE%97%E5%87%BA-Setter-%E9%96%A2%E6%95%B0)
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
---
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
# コメントを受けて追記
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
```html
|
64
|
+
|
65
|
+
<body>
|
66
|
+
|
67
|
+
<div id='app'>
|
68
|
+
|
69
|
+
<div>
|
70
|
+
|
71
|
+
<h1>sample</h1>
|
72
|
+
|
73
|
+
<table class='table'>
|
74
|
+
|
75
|
+
<thead>
|
76
|
+
|
77
|
+
<tr>
|
78
|
+
|
79
|
+
<th><input type='checkbox' v-model="isAllSelected"></input></th>
|
80
|
+
|
81
|
+
<th>商品名</th>
|
82
|
+
|
83
|
+
<th>単価</th>
|
84
|
+
|
85
|
+
<th>数量</th>
|
86
|
+
|
87
|
+
<th>消費税率</th>
|
88
|
+
|
89
|
+
<th>税抜金額計</th>
|
90
|
+
|
91
|
+
<th>消費税額</th>
|
92
|
+
|
93
|
+
<th>合計金額</th>
|
94
|
+
|
95
|
+
</tr>
|
96
|
+
|
97
|
+
</thead>
|
98
|
+
|
99
|
+
<tbody>
|
100
|
+
|
101
|
+
<tr v-for='(detail,index) in details' v-bind:key='detail.id'>
|
102
|
+
|
103
|
+
<th><input type='checkbox' v-model='detail.isChecked'></input></th>
|
104
|
+
|
105
|
+
<th><input type='text'></th>
|
106
|
+
|
107
|
+
<th><input type='number' v-model.number='detail.unitPrice' style="text-align:right"></th>
|
108
|
+
|
109
|
+
<th><input type='number' v-model.number='detail.num' style="text-align:right"></th>
|
110
|
+
|
111
|
+
<th><input type='number' v-model.number='detail.rate' style="text-align:right"></th>
|
112
|
+
|
113
|
+
<th>{{ (detail.unitPrice * detail.num).toLocaleString()}} </th>
|
114
|
+
|
115
|
+
<th>{{ (detail.unitPrice * detail.num * detail.rate/100).toLocaleString()}} </th>
|
116
|
+
|
117
|
+
<th>{{ ((detail.unitPrice * detail.num ) + (detail.unitPrice * detail.num *
|
118
|
+
|
119
|
+
detail.rate/100)).toLocaleString() }} </th>
|
120
|
+
|
121
|
+
<th><button @click='deleteRow(index)'>削除</button></th>
|
122
|
+
|
123
|
+
</tr>
|
124
|
+
|
125
|
+
</tbody>
|
126
|
+
|
127
|
+
</table>
|
128
|
+
|
129
|
+
<button @click='addRow'>行を追加</button>
|
130
|
+
|
131
|
+
<p>税抜金額計{{ totalPrice.toLocaleString() }}円</p>
|
132
|
+
|
133
|
+
<p>消費税額計{{ totalTax.toLocaleString() }}円</p>
|
134
|
+
|
135
|
+
<p>税込金額計{{ totalPriceWithTax.toLocaleString() }}円</p>
|
136
|
+
|
137
|
+
</div>
|
138
|
+
|
139
|
+
</div>
|
140
|
+
|
141
|
+
<script src='https://unpkg.com/vue'></script>
|
142
|
+
|
143
|
+
</body>
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
<script>
|
148
|
+
|
149
|
+
let app = new Vue({
|
150
|
+
|
151
|
+
el: '#app',
|
152
|
+
|
153
|
+
data() {
|
154
|
+
|
155
|
+
return {
|
156
|
+
|
157
|
+
details: [{
|
158
|
+
|
159
|
+
isChecked: false,
|
160
|
+
|
161
|
+
unitPrice: '',
|
162
|
+
|
163
|
+
num: '',
|
164
|
+
|
165
|
+
rate: 10
|
166
|
+
|
167
|
+
}]
|
168
|
+
|
169
|
+
}
|
170
|
+
|
171
|
+
},
|
172
|
+
|
173
|
+
computed: {
|
174
|
+
|
175
|
+
//税抜金額計
|
176
|
+
|
177
|
+
totalPrice: function () {
|
178
|
+
|
179
|
+
return this.details.filter(x => x.isChecked).reduce((sum, detail) => sum + detail.unitPrice * detail.num, 0);
|
180
|
+
|
181
|
+
},
|
182
|
+
|
183
|
+
//消費税計
|
184
|
+
|
185
|
+
totalTax: function () {
|
186
|
+
|
187
|
+
return this.details.filter(x => x.isChecked).reduce((sum, detail) => sum + detail.unitPrice * detail.num * detail.rate / 100, 0);
|
188
|
+
|
189
|
+
},
|
190
|
+
|
191
|
+
//税込額計
|
192
|
+
|
193
|
+
totalPriceWithTax: function () {
|
194
|
+
|
195
|
+
return this.details.filter(x => x.isChecked).reduce((sum, detail) => sum + (detail.unitPrice * detail.num) + (detail.unitPrice * detail.num * detail.rate / 100), 0);
|
196
|
+
|
197
|
+
},
|
198
|
+
|
199
|
+
isAllSelected: {
|
200
|
+
|
201
|
+
get: function () {
|
202
|
+
|
203
|
+
return !this.details.some(x => !x.isChecked)
|
204
|
+
|
205
|
+
},
|
206
|
+
|
207
|
+
set: function (bool) {
|
208
|
+
|
209
|
+
if (bool) {
|
210
|
+
|
211
|
+
this.details.forEach(x => x.isChecked = true);
|
212
|
+
|
213
|
+
} else {
|
214
|
+
|
215
|
+
this.details.forEach(x => x.isChecked = false);
|
216
|
+
|
217
|
+
}
|
218
|
+
|
219
|
+
}
|
220
|
+
|
221
|
+
}
|
222
|
+
|
223
|
+
},
|
224
|
+
|
225
|
+
methods: {
|
226
|
+
|
227
|
+
//行追加
|
228
|
+
|
229
|
+
addRow: function () {
|
230
|
+
|
231
|
+
this.details.push({
|
232
|
+
|
233
|
+
isChecked: false,
|
234
|
+
|
235
|
+
unitPrice: '',
|
236
|
+
|
237
|
+
num: '',
|
238
|
+
|
239
|
+
rate: 10
|
240
|
+
|
241
|
+
})
|
242
|
+
|
243
|
+
},
|
244
|
+
|
245
|
+
//行削除
|
246
|
+
|
247
|
+
deleteRow: function (index) {
|
248
|
+
|
249
|
+
this.details.splice(index, 1)
|
250
|
+
|
251
|
+
}
|
252
|
+
|
253
|
+
}
|
254
|
+
|
255
|
+
})
|
256
|
+
|
257
|
+
</script>
|
258
|
+
|
259
|
+
```
|
1
別解
test
CHANGED
@@ -7,3 +7,45 @@
|
|
7
7
|
|
8
8
|
|
9
9
|
解決方法ですが、details配列すべての要素にアクセスして`isChecked`を変更するのがいいでしょう。
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
---
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
セッタを使う方がスマートかも。
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
```js
|
22
|
+
|
23
|
+
isAllSelected: {
|
24
|
+
|
25
|
+
get: function () {
|
26
|
+
|
27
|
+
return !this.details.some(x => !x.isChecked)
|
28
|
+
|
29
|
+
},
|
30
|
+
|
31
|
+
set: function (bool) {
|
32
|
+
|
33
|
+
if (bool) {
|
34
|
+
|
35
|
+
this.details.forEach(x => x.isChecked = true);
|
36
|
+
|
37
|
+
} else {
|
38
|
+
|
39
|
+
this.details.forEach(x => x.isChecked = false);
|
40
|
+
|
41
|
+
}
|
42
|
+
|
43
|
+
}
|
44
|
+
|
45
|
+
}
|
46
|
+
|
47
|
+
```
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
[算出 Setter 関数 | 算出プロパティとウォッチャ — Vue.js](https://jp.vuejs.org/v2/guide/computed.html#%E7%AE%97%E5%87%BA-Setter-%E9%96%A2%E6%95%B0)
|