0

数字を含むhtmlテーブルがあります。例えば:

Col1 Col2 Col3
 5    3    1
 1    2    1
 10   3    2

そして、次の数学に従って、各セルに特定の色の背景を持たせるために Javascript を使用したいと思います。たとえば、3 つの列の 1 つ (各行) が他の 2 つの列の合計よりも大きい場合:

Col1 > Col2 + Col3 => bkg color: #000
Col2 > Col1 + Col3 => bkg color: #333
Col3 > Col1 + Col3 => bkg color: #666

Javascriptでできますか?誰でもコードを手伝ってもらえますか?

4

3 に答える 3

0

ここにあなたのための何かがあります(http://jsfiddle.net/AbnCz/3/)。これはアルゴとしてはうまくスケーリングしませんが、要件に従って機能します。行/列をさらに追加する場合は、colors配列に適切な色を追加します。

>更新:各セルトラバーサルを通じて合計を決定するのではなく、合計をキャッシュするようにパフォーマンスの更新を行いました

HTML

<table id="dataTable">
    <tr>
        <td>20</td>
        <td>50</td>
        <td>70</td>
    </tr>
    <tr>
        <td>40</td>
        <td>2</td>
        <td>7</td>
    </tr>
    <tr>
        <td>5</td>
        <td>2</td>
        <td>60</td>
    </tr>
</table>

Javascript

var colors = ["#000","#333","#666"];
var t = document.getElementById('dataTable');

var rows = t.getElementsByTagName('tr'), row, cells, tgtCell, rowSum, othersSum;

// let's go through the rows
for(var r=0; r<rows.length; r++){

   row = rows[r];
   cells = row.getElementsByTagName('td');
   rowSum = 0;

   // lets get the sum for the row.
   // we'll subtract each cell from it to get the remaining sum.
   for(var _c=0; _c<cells.length; _c++){
       rowSum += parseInt(cells[_c].textContent,10);
   }

   // let's go through the cells
   for(var c=0; c<cells.length; c++){

       tgtCell = cells[c];
       tgtVal = parseInt(tgtCell.textContent, 10);

       othersSum = rowSum - tgtVal;

       // if the target is greater than the remaining sum, style it
       if(tgtVal > othersSum){
           tgtCell.style.backgroundColor = colors[c % colors.length];           
       }

   }

}
于 2013-04-27T18:17:24.547 に答える
0

これを試して :

HTML:

<table id="dataTable">
            <tr>
                <td>3</td>
                <td>5</td>
                <td>1</td>
            </tr>
            <tr>
                <td>1</td>
                <td>2</td>
                <td>4</td>
            </tr>
            <tr>
                <td>16</td>
                <td>13</td>
                <td>2</td>
            </tr>
        </table>

ジャバスクリプト:

var table = document.getElementById('dataTable'), activeCells
                    row = table.getElementsByTagName('tr'),
                    cell = table.getElementsByTagName('td');

                var colorArray = new Array('red', 'blue', 'yellow');

                //loop through all rows
                for ( var i = 0; i < row.length; ++i) {

                    //get cells currently being read
                    activeCells = row[i].getElementsByTagName('td');

                    //prepare storage
                    var cellArray = new Array(),
                        newCellArray = new Array(),
                        cellElementArray = new Array(),
                        sum = 0;

                    //loop through active cells
                    for ( var x = 0; x < activeCells.length; ++x ) {

                        var currentCell = activeCells[x],
                            cellVal = parseInt( currentCell.innerHTML );

                        cellArray[x] = cellVal;
                        newCellArray[x] = cellVal;

                        cellElementArray[x] = currentCell;

                    }

                    //loop through Cell Array
                    for ( var y = 0; y < cellArray.length; ++y ) {

                        newCellArray.splice(y, 1);

                        for ( var z = 0; z < newCellArray.length; ++z ) {
                            sum += newCellArray[z];
                        }

                        newCellArray = [];

                        for ( var n = 0; n < cellArray.length; ++n ) {
                            newCellArray[n] = cellArray[n];
                        }

                        console.log( sum);

                        if ( cellArray[y] > sum ) {

                            console.log( 'in');

                            cellElementArray[y].style.backgroundColor = colorArray[y];
                        }

                        sum = 0;

                    }
                }

私が実装した追加機能は、これが動的であることです。セルの数を増やしてみてください。それでも計算されます。

また、好みに応じて colorArray を変更してください。列順です。何かのようなものvar colorArray = new Array('#000','#333','#667');

jsfiddle デモ: http://jsfiddle.net/aVqCU/

于 2013-04-27T16:54:53.187 に答える
0

私はこのコードを自分でテストしていません。しかし、それは次のようなものでなければなりません:

 var table = document.getElementById("table"); //Replace "table" with the id of your table in the HTML
    var table = document.getElementById("table"); //Replace "table" with the id of your table in the HTML
for (var i = 0, row; row = table.rows[i]; i++)   //iterate through rows
{

    var cell1 = row.cells[0];
    var cell2 = row.cells[1];
    var cell3 = row.cells[2];

    if(parseFloat(cell1.innerHTML) > (parseFloat(cell2.innerHTML) + parseFloat(cell3.innerHTML)))
    {   
        cell1.style.backgroundColor = "#000";
    }
    if(parseFloat(cell2.innerHTML) > parseFloat(cell3.innerHTML) + parseFloat(cell1.innerHTML))
    {
        cell2.style.backgroundColor = "#333";
    }
    if(parseFloat(cell3.innerHTML) > parseFloat(cell2.innerHTML) + parseFloat(cell1.innerHTML))
    {
        cell3.style.backgroundColor = "#666";
    }
}

テキストを数値に変換するには、row.cells で parseInt または parseFloat を使用する必要がある場合があります。

于 2013-04-27T12:45:47.263 に答える