简体   繁体   English

html表格单元格的条件格式

[英]conditional formatting of html table cells

Are there any out of the box solutions to have conditional formatting of HTML tables? 是否有任何开箱即用的解决方案有条件格式的HTML表格?

With conditional formatting I am more interested in having different colors as cell background depending on the value (numeric) of that or some other column (in the same table). 使用条件格式化时,我更感兴趣的是将不同的颜色作为单元格背景,具体取决于该值或其他列的值(数字)(在同一个表中)。

Something similar to what we have in excel Conditional Formating -> Color Scales -> Red Yellow Green. 类似于我们在excel中的条件格式 - >颜色标度 - >红黄绿色。 I want to implement that in a table that is being dynamically generated via JSP. 我想在一个通过JSP动态生成的表中实现它。

Are there any JavaScript/jquery or JSP solutions for this? 是否有任何JavaScript / jquery或JSP解决方案?

http://jsfiddle.net/stofke/Ya68Q/ http://jsfiddle.net/stofke/Ya68Q/

      $(function() {
            $('tr > td:odd').each(function(index) {
                var scale = [['vPoor', 10], ['poor', 50], ['avg', 250], ['good', 1250], ['vGood', 6250]];
                var score = $(this).text();
                for (var i = 0; i < scale.length; i++) {
                    if (score <= scale[i][1]) {
                        $(this).addClass(scale[i][0]);
                    }
                }
            });
       });

My first take on this, given the following table structure: 鉴于以下表结构,我的第一个看法:

<table>
    <col id="name" />
    <col id="score" />
    <thead>
        <tr>
            <th>Name</th>
            <th>Score</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Allan, Paul</td>
            <td>28</td>
        </tr>
        <tr>
            <td>Bartlett, James</td>
            <td>33</td>
        </tr>
        <tr>
            <td>Callow, Simon</td>
            <td>25</td>
        </tr>
        <tr>
            <td>Dennis, Mark</td>
            <td>19</td>
        </tr>
        <tr>
            <td>Ennals, Simon</td>
            <td>10</td>
        </tr>
        <tr>
            <td>Finnegan, Seamus</td>
            <td>21</td>
        </tr>
    </tbody>
</table>

css: CSS:

table {
    width: 20em;
}

#score {
    width: 50%;
}

#name {
    width: 50%;
}

th {
    border-bottom: 2px solid #000;
    padding: 0.5em 0 0.1em 0;
    font-size: 1.2em;
}

td {
    border-bottom: 2px solid #ccc;
    padding: 0.5em 0 0.1em 0;
}

th:nth-child(even),
td:nth-child(even) {
    text-align: center;
}

.vGood {
    background-color: #0f0;
}

.good {
    background-color: #0c0;
}

.avg {
    background-color: #060;
}

.poor {
    background-color: #c00;
}

.vPoor {
    background-color: #f00;
}

and jQuery: 和jQuery:

$('tbody tr td:not(":first")').each(

function() {
    var vGood = 30,
        good = 25,
        avg = 20,
        poor = 15,
        vPoor = 10,
        score = $(this).text();

    if (score >= vGood) {
        $(this).addClass('vGood');
    }
    else if (score < vGood && score >= good) {
        $(this).addClass('good');
    }
    else if (score <good && score >= avg) {
        $(this).addClass('avg');
    }
    else if (score < avg&& score >= poor) {
        $(this).addClass('poor');
    }
    else if (score < poor) {
        $(this).addClass('vPoor');
    }
    });

JS Fiddle demo . JS小提琴演示

This is, of course, a brute-force approach. 当然,这是一种蛮力的方法。 It'll work, but it's not the most optimised/efficient approach. 它会起作用,但它不是最优化/最有效的方法。

You could use css and dynamically generate class names on the backend... so on the backend, you would add class="level1" (or "level2" or "level3" etc) based on the calculated value of the cell. 您可以使用css并在后端动态生成类名...所以在后端,您可以根据单元格的计算值添加class =“level1”(或“level2”或“level3”等)。 Then you could control the display of those classes through simple css. 然后你可以通过简单的CSS控制这些类的显示。

You could set up some css classes: 你可以设置一些css类:

.row { background-color: #00ff00; }
.alt { backgorund-color: #ff00ff; }

<table>
    <tr class="row">
        <td>&lt;cell contents go here&gt;</td>
    </tr>
    <tr class="alt">
        <td>&lt;cell contents go here&gt;</td>
    </tr>
</table>

The jquery option is also simple, but this is how I would do it honestly. jquery选项也很简单,但这就是我如何诚实地做到这一点。

HTH HTH

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. 迭代每个单元格并根据需要重新计算最高值和最低值,然后使用if语句(或类似的东西)重新分配正确的颜色。 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. 您可以找到一种方法来优化将颜色重新分配给现有单元格的过程。

I have done similar to similar to what ZDYN and David propose but in a more mathematically proven way. 我的做法类似于ZDYN和David所提出的,但是以更加数学证明的方式。

After my dynamic values are calculated I go around calculating percentiles for the columns that I want to color code using 计算完我的动态值后,我会计算我想要使用颜色代码的列的百分位数

(L/N)*100 (L / N)×100
where: L=> Number of Items less the value for which percentile is being calculated 其中:L =>项目数减去计算百分位数的值
N => Total number of items N =>项目总数

Now, depending on the percentile I get from the above calculations, appropriate colors are assigned for use when displaying the table. 现在,根据我从上面的计算得到的百分位数,在显示表格时分配适当的颜色。
With this approach I get the flexibility of using different color palettes at different granularity levels as the need be. 通过这种方法,我可以根据需要灵活地在不同的粒度级别使用不同的调色板。
For example I can use Red for percentile ranging from 0-5 in one case and 0-10 in another. 例如,我可以使用红色作为百分位数,在一种情况下为0-5,在另一种情况下为0-10。 All these parts can be flexibly coded for as all of the above steps are done @ backend. 所有这些部件都可以灵活编码,因为所有上述步骤都是在@ backend完成的。 (in Java) (在Java中)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM