表(table要素)に、ハイライト表示、自動合計、平均値自動算出の機能を実装する方法。
実装例
名前 | 算数 | 国語 | 英語 | 合計 |
---|---|---|---|---|
太郎 | 84 | 52 | 72 | |
次郎 | 62 | 90 | 89 | |
花子 | 73 | 55 | 98 | |
翼 | 76 | 52 | 53 | |
岬 | 83 | 69 | 56 | |
合計 | 算数 | 国語 | 英語 | |
平均 | 算数 | 国語 | 英語 |
ソースコード
JavaScript
<script type="text/javascript">
window.onload = function(){
var $sampleTable = document.getElementById( "sample" );
var $sampleColgroup = $sampleTable.getElementsByTagName( "colgroup" )
var $sampleTHead = $sampleTable.tHead;
var $sampleTHeadCells = $sampleTHead.rows[0].cells;
var $sampleTBodies = $sampleTable.tBodies;
var $sampleTFoot = $sampleTable.tFoot;
var $sampleTFootCells1 = $sampleTFoot.rows[0].cells;
var $sampleTFootCells2 = $sampleTFoot.rows[1].cells;
var $sumBySubject = [];
var $sumByStudents = [];
for ( var $tbodyIndex = 0; $tbodyIndex < $sampleTBodies.length; $tbodyIndex++ ) {
var $sampleTBodyRows = $sampleTBodies[$tbodyIndex].rows;
for ( var $rowIndex = 0; $rowIndex < $sampleTBodyRows.length; $rowIndex++ ) {
var $sampleTBodyCells = $sampleTBodyRows[$rowIndex].cells;
for ( var $cellIndex = 1; $cellIndex < $sampleTBodyCells.length; $cellIndex++ ) {
$sumBySubject[$cellIndex] = 0;
}
}
}
for ( var $tbodyIndex = 0; $tbodyIndex < $sampleTBodies.length; $tbodyIndex++ ) {
var $sampleTBodyRows = $sampleTBodies[$tbodyIndex].rows;
for ( var $rowIndex = 0; $rowIndex < $sampleTBodyRows.length; $rowIndex++ ) {
$sampleTBodyRows[$rowIndex].onmouseover = function(){
this.style.backgroundColor = "yellow";
};
$sampleTBodyRows[$rowIndex].onmouseout = function(){
this.style.backgroundColor = "";
};
$sumByStudents[$rowIndex] = 0;
var $sampleTBodyCells = $sampleTBodyRows[$rowIndex].cells;
for ( var $cellIndex = 1; $cellIndex < $sampleTBodyCells.length; $cellIndex++ ) {
$sampleTBodyCells[$cellIndex].onmouseover = function(){
this.style.backgroundColor = "red";
$sampleColgroup[this.cellIndex].style.backgroundColor = "yellow";
$sampleTHeadCells[this.cellIndex].style.backgroundColor = "yellow";
$sampleTFootCells1[this.cellIndex].style.backgroundColor = "yellow";
$sampleTFootCells2[this.cellIndex].style.backgroundColor = "yellow";
};
$sampleTBodyCells[$cellIndex].onmouseout = function(){
this.style.backgroundColor = "";
$sampleColgroup[this.cellIndex].style.backgroundColor = "";
$sampleTHeadCells[this.cellIndex].style.backgroundColor = "";
$sampleTFootCells1[this.cellIndex].style.backgroundColor = "";
$sampleTFootCells2[this.cellIndex].style.backgroundColor = "";
};
if( $cellIndex != 0 ){
if( $cellIndex != $sampleTBodyCells.length - 1 ){
$sumByStudents[$rowIndex] += parseInt( $sampleTBodyCells[$cellIndex].innerHTML );
}else{
$sampleTBodyCells[$sampleTBodyCells.length-1].innerHTML = $sumByStudents[$rowIndex];
}
}
if( $cellIndex != 0 ){
$sumBySubject[$cellIndex] += parseInt( $sampleTBodyCells[$cellIndex].innerHTML );
}
}
}
}
for ( var $cellIndex = 1; $cellIndex < $sampleTFootCells1.length; $cellIndex++ ) {
$sampleTFootCells1[$cellIndex].innerHTML = $sumBySubject[$cellIndex];
}
for ( var $cellIndex = 1; $cellIndex < $sampleTFootCells2.length; $cellIndex++ ) {
$sampleTFootCells2[$cellIndex].innerHTML = Math.round( $sumBySubject[$cellIndex] / ( $sampleTable.rows.length - $sampleTHead.rows.length - $sampleTFoot.rows.length ) );
}
}
</script>
window.onload = function(){
var $sampleTable = document.getElementById( "sample" );
var $sampleColgroup = $sampleTable.getElementsByTagName( "colgroup" )
var $sampleTHead = $sampleTable.tHead;
var $sampleTHeadCells = $sampleTHead.rows[0].cells;
var $sampleTBodies = $sampleTable.tBodies;
var $sampleTFoot = $sampleTable.tFoot;
var $sampleTFootCells1 = $sampleTFoot.rows[0].cells;
var $sampleTFootCells2 = $sampleTFoot.rows[1].cells;
var $sumBySubject = [];
var $sumByStudents = [];
for ( var $tbodyIndex = 0; $tbodyIndex < $sampleTBodies.length; $tbodyIndex++ ) {
var $sampleTBodyRows = $sampleTBodies[$tbodyIndex].rows;
for ( var $rowIndex = 0; $rowIndex < $sampleTBodyRows.length; $rowIndex++ ) {
var $sampleTBodyCells = $sampleTBodyRows[$rowIndex].cells;
for ( var $cellIndex = 1; $cellIndex < $sampleTBodyCells.length; $cellIndex++ ) {
$sumBySubject[$cellIndex] = 0;
}
}
}
for ( var $tbodyIndex = 0; $tbodyIndex < $sampleTBodies.length; $tbodyIndex++ ) {
var $sampleTBodyRows = $sampleTBodies[$tbodyIndex].rows;
for ( var $rowIndex = 0; $rowIndex < $sampleTBodyRows.length; $rowIndex++ ) {
$sampleTBodyRows[$rowIndex].onmouseover = function(){
this.style.backgroundColor = "yellow";
};
$sampleTBodyRows[$rowIndex].onmouseout = function(){
this.style.backgroundColor = "";
};
$sumByStudents[$rowIndex] = 0;
var $sampleTBodyCells = $sampleTBodyRows[$rowIndex].cells;
for ( var $cellIndex = 1; $cellIndex < $sampleTBodyCells.length; $cellIndex++ ) {
$sampleTBodyCells[$cellIndex].onmouseover = function(){
this.style.backgroundColor = "red";
$sampleColgroup[this.cellIndex].style.backgroundColor = "yellow";
$sampleTHeadCells[this.cellIndex].style.backgroundColor = "yellow";
$sampleTFootCells1[this.cellIndex].style.backgroundColor = "yellow";
$sampleTFootCells2[this.cellIndex].style.backgroundColor = "yellow";
};
$sampleTBodyCells[$cellIndex].onmouseout = function(){
this.style.backgroundColor = "";
$sampleColgroup[this.cellIndex].style.backgroundColor = "";
$sampleTHeadCells[this.cellIndex].style.backgroundColor = "";
$sampleTFootCells1[this.cellIndex].style.backgroundColor = "";
$sampleTFootCells2[this.cellIndex].style.backgroundColor = "";
};
if( $cellIndex != 0 ){
if( $cellIndex != $sampleTBodyCells.length - 1 ){
$sumByStudents[$rowIndex] += parseInt( $sampleTBodyCells[$cellIndex].innerHTML );
}else{
$sampleTBodyCells[$sampleTBodyCells.length-1].innerHTML = $sumByStudents[$rowIndex];
}
}
if( $cellIndex != 0 ){
$sumBySubject[$cellIndex] += parseInt( $sampleTBodyCells[$cellIndex].innerHTML );
}
}
}
}
for ( var $cellIndex = 1; $cellIndex < $sampleTFootCells1.length; $cellIndex++ ) {
$sampleTFootCells1[$cellIndex].innerHTML = $sumBySubject[$cellIndex];
}
for ( var $cellIndex = 1; $cellIndex < $sampleTFootCells2.length; $cellIndex++ ) {
$sampleTFootCells2[$cellIndex].innerHTML = Math.round( $sumBySubject[$cellIndex] / ( $sampleTable.rows.length - $sampleTHead.rows.length - $sampleTFoot.rows.length ) );
}
}
</script>
HTML
<table id="sample">
<caption id="sampleCaption">
2組のテスト結果
</caption>
<colgroup span="1" id="sampleColgroupName"></colgroup>
<colgroup span="1"></colgroup>
<colgroup span="1"></colgroup>
<colgroup span="1"></colgroup>
<colgroup span="1"></colgroup>
<thead>
<tr>
<th>名前</th>
<th>算数</th>
<th>国語</th>
<th>英語</th>
<th>合計</th>
</tr>
</thead>
<tbody>
<tr>
<td>太郎</td>
<td>84</td>
<td>52</td>
<td>72</td>
<td></td>
</tr>
<tr>
<td>次郎</td>
<td>62</td>
<td>90</td>
<td>89</td>
<td></td>
</tr>
<tr>
<td>花子</td>
<td>73</td>
<td>55</td>
<td>98</td>
<td></td>
</tr>
<tr>
<td>翼</td>
<td>76</td>
<td>52</td>
<td>53</td>
<td></td>
</tr>
<tr>
<td>岬</td>
<td>83</td>
<td>69</td>
<td>56</td>
<td></td>
</tr>
</tbody>
<tfoot>
<tr>
<th>合計</th>
<th>算数</th>
<th>国語</th>
<th>英語</th>
<th></th>
</tr>
<tr>
<th>平均</th>
<th>算数</th>
<th>国語</th>
<th>英語</th>
<th></th>
</tr>
</tfoot>
</table>
<caption id="sampleCaption">
2組のテスト結果
</caption>
<colgroup span="1" id="sampleColgroupName"></colgroup>
<colgroup span="1"></colgroup>
<colgroup span="1"></colgroup>
<colgroup span="1"></colgroup>
<colgroup span="1"></colgroup>
<thead>
<tr>
<th>名前</th>
<th>算数</th>
<th>国語</th>
<th>英語</th>
<th>合計</th>
</tr>
</thead>
<tbody>
<tr>
<td>太郎</td>
<td>84</td>
<td>52</td>
<td>72</td>
<td></td>
</tr>
<tr>
<td>次郎</td>
<td>62</td>
<td>90</td>
<td>89</td>
<td></td>
</tr>
<tr>
<td>花子</td>
<td>73</td>
<td>55</td>
<td>98</td>
<td></td>
</tr>
<tr>
<td>翼</td>
<td>76</td>
<td>52</td>
<td>53</td>
<td></td>
</tr>
<tr>
<td>岬</td>
<td>83</td>
<td>69</td>
<td>56</td>
<td></td>
</tr>
</tbody>
<tfoot>
<tr>
<th>合計</th>
<th>算数</th>
<th>国語</th>
<th>英語</th>
<th></th>
</tr>
<tr>
<th>平均</th>
<th>算数</th>
<th>国語</th>
<th>英語</th>
<th></th>
</tr>
</tfoot>
</table>
CSS
<style>
#sampleCaption {
border-top: double;
font-size: inherit;
margin: 0px;
padding: 3px;
background-color: lightgreen;
}
#sample tr,
#sample th,
#sample td {
background-color: inherit;
font-size: inherit !important;
color: inherit !important;
}
#sample thead,
#sample tfoot,
#sampleColgroupName {
background-color: lightblue;
}
#sample {
border: double !important;
}
#sample tfoot {
border-top: double;
}
</style>
#sampleCaption {
border-top: double;
font-size: inherit;
margin: 0px;
padding: 3px;
background-color: lightgreen;
}
#sample tr,
#sample th,
#sample td {
background-color: inherit;
font-size: inherit !important;
color: inherit !important;
}
#sample thead,
#sample tfoot,
#sampleColgroupName {
background-color: lightblue;
}
#sample {
border: double !important;
}
#sample tfoot {
border-top: double;
}
</style>