回答編集履歴
2
複数のtable要素に適用できるようにした
test
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
### 修正方針
|
2
|
+
|
3
|
+
|
4
|
+
|
1
5
|
> ```JavaScript
|
2
6
|
|
3
7
|
> var len = editTbl.rows[1].cells.length; // 列数, セルへの参照はrowsのcellsコレクション
|
@@ -10,11 +14,15 @@
|
|
10
14
|
|
11
15
|
合計値を出力するtd要素ノードの `cellIndex` を変数に格納し、`editTbl.rows[1].cells[cellIndex]` で cellIndex を固定すれば、列の合計値を算出できるでしょう。
|
12
16
|
|
13
|
-
構造上、thead, tbody, tfoot に分けた方が良いと思ったので、HTMLに少し手を入れました。
|
17
|
+
後述のコードでは、構造上、thead, tbody, tfoot に分けた方が良いと思ったので、HTMLに少し手を入れました。
|
18
|
+
|
19
|
+
|
20
|
+
|
14
|
-
|
21
|
+
### コード
|
15
|
-
|
16
|
-
|
22
|
+
|
23
|
+
|
24
|
+
|
17
|
-
- [table要素の列の合計値を算出 - JSFiddle](https://jsfiddle.net/75wt6pja/
|
25
|
+
- [table要素の列の合計値を算出 - JSFiddle](https://jsfiddle.net/75wt6pja/2/)
|
18
26
|
|
19
27
|
|
20
28
|
|
@@ -22,7 +30,7 @@
|
|
22
30
|
|
23
31
|
<!--テーブル1月-->
|
24
32
|
|
25
|
-
<table
|
33
|
+
<table class="calc">
|
26
34
|
|
27
35
|
<thead>
|
28
36
|
|
@@ -286,45 +294,341 @@
|
|
286
294
|
|
287
295
|
</table>
|
288
296
|
|
289
|
-
<p><button
|
297
|
+
<p><button class="execute-calc" type="button">計算</button></p>
|
290
298
|
|
291
299
|
<!--//テーブル1月-->
|
292
300
|
|
293
301
|
|
294
302
|
|
303
|
+
<!--テーブル2月-->
|
304
|
+
|
305
|
+
<table class="calc">
|
306
|
+
|
307
|
+
<thead>
|
308
|
+
|
309
|
+
<tr>
|
310
|
+
|
311
|
+
<th colspan="6">2月</th>
|
312
|
+
|
313
|
+
</tr>
|
314
|
+
|
315
|
+
<tr>
|
316
|
+
|
317
|
+
<th colspan="2">収入</th>
|
318
|
+
|
319
|
+
<th colspan="2">出費</th>
|
320
|
+
|
321
|
+
<th colspan="1">備考</th>
|
322
|
+
|
323
|
+
</tr>
|
324
|
+
|
325
|
+
</thead>
|
326
|
+
|
327
|
+
<tbody>
|
328
|
+
|
329
|
+
<tr>
|
330
|
+
|
331
|
+
<td>文字</td>
|
332
|
+
|
333
|
+
<td><input type="number"></td>
|
334
|
+
|
335
|
+
<td>文字</td>
|
336
|
+
|
337
|
+
<td><input type="number"></td>
|
338
|
+
|
339
|
+
<td>サンプル</td>
|
340
|
+
|
341
|
+
</tr>
|
342
|
+
|
343
|
+
<tr>
|
344
|
+
|
345
|
+
<td>文字</td>
|
346
|
+
|
347
|
+
<td><input type="number"></td>
|
348
|
+
|
349
|
+
<td>文字</td>
|
350
|
+
|
351
|
+
<td><input type="number"></td>
|
352
|
+
|
353
|
+
<td>サンプル</td>
|
354
|
+
|
355
|
+
</tr>
|
356
|
+
|
357
|
+
<tr>
|
358
|
+
|
359
|
+
<td>文字</td>
|
360
|
+
|
361
|
+
<td><input type="number"></td>
|
362
|
+
|
363
|
+
<td>文字</td>
|
364
|
+
|
365
|
+
<td><input type="number"></td>
|
366
|
+
|
367
|
+
<td>サンプル</td>
|
368
|
+
|
369
|
+
</tr>
|
370
|
+
|
371
|
+
<tr>
|
372
|
+
|
373
|
+
<td>文字</td>
|
374
|
+
|
375
|
+
<td><input type="number"></td>
|
376
|
+
|
377
|
+
<td>文字</td>
|
378
|
+
|
379
|
+
<td><input type="number"></td>
|
380
|
+
|
381
|
+
<td>サンプル</td>
|
382
|
+
|
383
|
+
</tr>
|
384
|
+
|
385
|
+
<tr>
|
386
|
+
|
387
|
+
<td>文字</td>
|
388
|
+
|
389
|
+
<td><input type="number"></td>
|
390
|
+
|
391
|
+
<td>文字</td>
|
392
|
+
|
393
|
+
<td><input type="number"></td>
|
394
|
+
|
395
|
+
<td>サンプル</td>
|
396
|
+
|
397
|
+
</tr>
|
398
|
+
|
399
|
+
<tr>
|
400
|
+
|
401
|
+
<td>文字</td>
|
402
|
+
|
403
|
+
<td><input type="number"></td>
|
404
|
+
|
405
|
+
<td>文字</td>
|
406
|
+
|
407
|
+
<td><input type="number"></td>
|
408
|
+
|
409
|
+
<td>サンプル</td>
|
410
|
+
|
411
|
+
</tr>
|
412
|
+
|
413
|
+
<tr>
|
414
|
+
|
415
|
+
<td>文字</td>
|
416
|
+
|
417
|
+
<td><input type="number"></td>
|
418
|
+
|
419
|
+
<td>文字</td>
|
420
|
+
|
421
|
+
<td><input type="number"></td>
|
422
|
+
|
423
|
+
<td>サンプル</td>
|
424
|
+
|
425
|
+
</tr>
|
426
|
+
|
427
|
+
<tr>
|
428
|
+
|
429
|
+
<td>文字</td>
|
430
|
+
|
431
|
+
<td><input type="number"></td>
|
432
|
+
|
433
|
+
<td>文字</td>
|
434
|
+
|
435
|
+
<td><input type="number"></td>
|
436
|
+
|
437
|
+
<td>サンプル</td>
|
438
|
+
|
439
|
+
</tr>
|
440
|
+
|
441
|
+
<tr>
|
442
|
+
|
443
|
+
<td>文字</td>
|
444
|
+
|
445
|
+
<td><input type="number"></td>
|
446
|
+
|
447
|
+
<td>文字</td>
|
448
|
+
|
449
|
+
<td><input type="number"></td>
|
450
|
+
|
451
|
+
<td>サンプル</td>
|
452
|
+
|
453
|
+
</tr>
|
454
|
+
|
455
|
+
<tr>
|
456
|
+
|
457
|
+
<td>文字</td>
|
458
|
+
|
459
|
+
<td><input type="number"></td>
|
460
|
+
|
461
|
+
<td>文字</td>
|
462
|
+
|
463
|
+
<td><input type="number"></td>
|
464
|
+
|
465
|
+
<td>サンプル</td>
|
466
|
+
|
467
|
+
</tr>
|
468
|
+
|
469
|
+
<tr>
|
470
|
+
|
471
|
+
<td>文字</td>
|
472
|
+
|
473
|
+
<td><input type="number"></td>
|
474
|
+
|
475
|
+
<td>文字</td>
|
476
|
+
|
477
|
+
<td><input type="number"></td>
|
478
|
+
|
479
|
+
<td>サンプル</td>
|
480
|
+
|
481
|
+
</tr>
|
482
|
+
|
483
|
+
<tr>
|
484
|
+
|
485
|
+
<td>文字</td>
|
486
|
+
|
487
|
+
<td><input type="number"></td>
|
488
|
+
|
489
|
+
<td>個人貯金</td>
|
490
|
+
|
491
|
+
<td><input type="number"></td>
|
492
|
+
|
493
|
+
<td>サンプル</td>
|
494
|
+
|
495
|
+
</tr>
|
496
|
+
|
497
|
+
<tr>
|
498
|
+
|
499
|
+
<td>文字</td>
|
500
|
+
|
501
|
+
<td><input type="number"></td>
|
502
|
+
|
503
|
+
<td>共通貯金</td>
|
504
|
+
|
505
|
+
<td><input type="number"></td>
|
506
|
+
|
507
|
+
<td>サンプル</td>
|
508
|
+
|
509
|
+
</tr>
|
510
|
+
|
511
|
+
</tbody>
|
512
|
+
|
513
|
+
<tfoot>
|
514
|
+
|
515
|
+
<tr>
|
516
|
+
|
517
|
+
<th>合計</th>
|
518
|
+
|
519
|
+
<td class="sum"><input type="number"></td>
|
520
|
+
|
521
|
+
<th>合計</th>
|
522
|
+
|
523
|
+
<td class="sum"><input type="number"></td>
|
524
|
+
|
525
|
+
<td>サンプル</td>
|
526
|
+
|
527
|
+
</tr>
|
528
|
+
|
529
|
+
<tr>
|
530
|
+
|
531
|
+
<th colspan="5">残高</th>
|
532
|
+
|
533
|
+
</tr>
|
534
|
+
|
535
|
+
<tr>
|
536
|
+
|
537
|
+
<td colspan="5"></td>
|
538
|
+
|
539
|
+
</tr>
|
540
|
+
|
541
|
+
<tr>
|
542
|
+
|
543
|
+
<th colspan="3">個人貯金残高</th>
|
544
|
+
|
545
|
+
<td colspan="2"></td>
|
546
|
+
|
547
|
+
</tr>
|
548
|
+
|
549
|
+
<tr>
|
550
|
+
|
551
|
+
<th colspan="3">共通貯金残高</th>
|
552
|
+
|
553
|
+
<td colspan="2"></td>
|
554
|
+
|
555
|
+
</tr>
|
556
|
+
|
557
|
+
<tr>
|
558
|
+
|
559
|
+
<th colspan="3">合計貯金残高</th>
|
560
|
+
|
561
|
+
<td colspan="2"></td>
|
562
|
+
|
563
|
+
</tr>
|
564
|
+
|
565
|
+
</tfoot>
|
566
|
+
|
567
|
+
</table>
|
568
|
+
|
569
|
+
<p><button class="execute-calc" type="button">計算</button></p>
|
570
|
+
|
571
|
+
<!--//テーブル2月-->
|
572
|
+
|
573
|
+
|
574
|
+
|
295
575
|
<script>
|
296
576
|
|
297
577
|
'use strict';
|
298
578
|
|
579
|
+
(function () {
|
580
|
+
|
299
|
-
|
581
|
+
function handleClick (event) {
|
300
|
-
|
582
|
+
|
301
|
-
var table = event.target.
|
583
|
+
var table = event.target.parentNode.previousElementSibling,
|
302
|
-
|
584
|
+
|
303
|
-
sumList = table.getElementsByClassName('sum');
|
585
|
+
sumList = table.getElementsByClassName('sum');
|
304
|
-
|
305
|
-
|
306
|
-
|
586
|
+
|
587
|
+
|
588
|
+
|
307
|
-
for (var i = 0, len = sumList.length, sumCell, sum; i < len; ++i) {
|
589
|
+
for (var i = 0, len = sumList.length, sumCell, sum; i < len; ++i) {
|
308
|
-
|
590
|
+
|
309
|
-
sumCell = sumList[i];
|
591
|
+
sumCell = sumList[i];
|
310
|
-
|
592
|
+
|
311
|
-
sum = 0;
|
593
|
+
sum = 0;
|
312
|
-
|
313
|
-
|
314
|
-
|
594
|
+
|
595
|
+
|
596
|
+
|
315
|
-
for (var j = 0, rows = table.tBodies[0].rows, rLen = rows.length, cellIndex = sumCell.cellIndex; j < rLen; ++j) {
|
597
|
+
for (var j = 0, rows = table.tBodies[0].rows, rLen = rows.length, cellIndex = sumCell.cellIndex; j < rLen; ++j) {
|
316
|
-
|
598
|
+
|
317
|
-
sum += rows[j].cells[cellIndex].firstChild.valueAsNumber || 0;
|
599
|
+
sum += rows[j].cells[cellIndex].firstChild.valueAsNumber || 0;
|
600
|
+
|
601
|
+
}
|
602
|
+
|
603
|
+
|
604
|
+
|
605
|
+
sumCell.firstChild.value = sum;
|
318
606
|
|
319
607
|
}
|
320
608
|
|
321
|
-
|
322
|
-
|
323
|
-
sumCell.firstChild.value = sum;
|
324
|
-
|
325
609
|
}
|
326
610
|
|
611
|
+
|
612
|
+
|
613
|
+
function main () {
|
614
|
+
|
615
|
+
var button = this.document.getElementsByClassName('execute-calc');
|
616
|
+
|
617
|
+
|
618
|
+
|
619
|
+
for (var i = 0, len = button.length; i < len; ++i) {
|
620
|
+
|
621
|
+
button[i].addEventListener('click', handleClick, false);
|
622
|
+
|
623
|
+
}
|
624
|
+
|
625
|
+
}
|
626
|
+
|
627
|
+
|
628
|
+
|
629
|
+
main.call(this);
|
630
|
+
|
327
|
-
}
|
631
|
+
}.call(this));
|
328
632
|
|
329
633
|
</script>
|
330
634
|
|
@@ -332,4 +636,12 @@
|
|
332
636
|
|
333
637
|
|
334
638
|
|
639
|
+
### 更新履歴
|
640
|
+
|
641
|
+
|
642
|
+
|
643
|
+
- 2017/07/05 12:43 複数のtable要素に適用できるようにした
|
644
|
+
|
645
|
+
|
646
|
+
|
335
647
|
Re: tukapon_ さん
|
1
`editTbl\.rows\[1\]\.cells\[cellIndex\]` で cellIndex を固定
test
CHANGED
@@ -8,6 +8,8 @@
|
|
8
8
|
|
9
9
|
`querySelectorAll` やjQueryのセレクタエンジンはそこそこコストがかかるので、速さを重視するなら `rows[i].cells[j]` も有りだと思います。
|
10
10
|
|
11
|
+
合計値を出力するtd要素ノードの `cellIndex` を変数に格納し、`editTbl.rows[1].cells[cellIndex]` で cellIndex を固定すれば、列の合計値を算出できるでしょう。
|
12
|
+
|
11
13
|
構造上、thead, tbody, tfoot に分けた方が良いと思ったので、HTMLに少し手を入れました。
|
12
14
|
|
13
15
|
|