conditional formatting of html table cells

前端 未结 6 1270
鱼传尺愫
鱼传尺愫 2021-02-04 12:49

Are there any out of the box solutions to have conditional formatting of HTML tables?

With conditional formatting I am more interested in having different colors as cell

6条回答
  •  别那么骄傲
    2021-02-04 13:23

    Maintain two variables for highest and lowest values in the table.

    Add a function that gets called any time the table changes. Iterate through each cell and recalculate the highest and lowest values as necessary and then with an if statement (or something similar) reassign the correct color. For example, for each cell:

    if(cellValue < minValue)
        minValue = cellValue;
    else if(cellValue > maxValue)
        maxValue = cellValue;
    
    var bracket = (cellValue - minValue) / (maxValue - minValue);
    
    if(bracket < .2)
        // make the cell green
    else if(bracket < .4)
        // make the cell green-yellow
    else if(bracket < .6)
        // make the cell yellow
    

    ...and so on. This is very brute force though. You can probably find a way to optimize the process of reassigning colors to existing cells.

提交回复
热议问题