简体   繁体   English

基于列值更改的备用表格行背景颜色

[英]Alternate Table Row Background Color Based on Column Value Changing

I'm reading data into an html table from a database and would like to alternate the row color when the value in the first column changes -- nothing fancy just alternate between two colors to help visually group the data.我正在将数据从数据库读取到 html 表中,并希望在第一列中的值发生变化时交替行颜色——没什么特别的,只是在两种颜色之间交替以帮助对数据进行可视化分组。 The issue I'm having is the groups of data is dynamic, and I don't know how to change the colors based on dynamic data.我遇到的问题是数据组是动态的,我不知道如何根据动态数据更改颜色。 I'm open to use CSS, jquery, javascript -- whatever tricks you have that would work.我愿意使用 CSS、jquery、javascript——无论你有什么技巧都行。 I've created a very simple jsFiddle that has all rows the same color for you to play with.我创建了一个非常简单的jsFiddle ,它的所有行都具有相同的颜色供您使用。

UPDATED EXPLANATION: When I say I want the row colors to alternate based on the value of a column changing, what I mean is, when looking at my fiddle example, the table rows start off aliceblue, and the value in the first column is 1. When that value changes to 2 I want the table rows to change colors to lightgreen.更新的解释:当我说我希望行颜色根据列更改的值交替时,我的意思是,在查看我的小提琴示例时,表行从 aliceblue 开始,第一列中的值为 1 . 当该值更改为 2 时,我希望表格行将颜色更改为浅绿色。 When the Key column value then changes to 3 I want the colors to switch back to aliceblue.当 Key 列值更改为 3 时,我希望颜色切换回 aliceblue。 When the key column value changes to 4 I want it to flip back to light green.当键列值更改为 4 时,我希望它翻转回浅绿色。 Hope this helps to clarify...希望这有助于澄清...

As always, any help you can give would be greatly appreciated!!与往常一样,您能提供的任何帮助将不胜感激!!

 tbody tr { background: aliceblue; } .altColor { background: lightgreen; }
 <div id="banner-message"> <table> <thead> <tr> <th>Key</th> <th>val</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>1</td> </tr> <tr> <td>1</td> <td>1</td> </tr> <tr> <td>2</td> <td>1</td> </tr> <tr> <td>2</td> <td>1</td> </tr> <tr> <td>2</td> <td>1</td> </tr> <tr> <td>3</td> <td>1</td> </tr> <tr> <td>4</td> <td>1</td> </tr> <tr> <td>4</td> <td>1</td> </tr> <tr> <td>5</td> <td>1</td> </tr> <tr> <td>5</td> <td>1</td> </tr> <tr> <td>5</td> <td>1</td> </tr> </tbody> </table> </div>

You can alternate color on a table with the :nth-child selector and the odd and even rules.您可以使用 :nth-child 选择器和奇数和偶数规则在表格上交替颜色。

More can be found there https://www.w3.org/Style/Examples/007/evenodd.en.html可以在那里找到更多https://www.w3.org/Style/Examples/007/evenodd.en.html

Try something like this:尝试这样的事情:

tr:nth-child(even) {background: #CCC}
tr:nth-child(odd) {background: #FFF}

I have added some javascript logic to make it work.我添加了一些 javascript 逻辑以使其工作。 I have added two scenarios.我添加了两个场景。 One if class should be based on the Odd/Even values and another is if class should be based on the value change.一个 if 类应该基于奇数/偶数值,另一个是 if 类应该基于值的变化。

See the Snippet below:请参阅下面的片段:

 $(document).ready(function(){ $("#banner-message tbody tr").each(function() { if(parseInt($(this).find("td").first().html()) % 2 == 0){ $(this).addClass("altColor"); } }); let prevValue = parseInt($("#banner-message2 tbody tr").first().find("td").first().html()); let currentClass = ''; $("#banner-message2 tbody tr").each(function() { if(prevValue != parseInt($(this).find("td").first().html())){ (currentClass=='')?currentClass = 'altColor':currentClass=''; } $(this).addClass(currentClass); prevValue = parseInt($(this).find("td").first().html()); }); });
 tbody tr { background: aliceblue; } .altColor { background: lightgreen; } div { display:inline-block; padding:10px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="banner-message"> Based on the Even/Odd values <table> <thead> <tr> <th>Key</th> <th>val</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>1</td> </tr> <tr> <td>1</td> <td>1</td> </tr> <tr> <td>2</td> <td>1</td> </tr> <tr> <td>2</td> <td>1</td> </tr> <tr> <td>2</td> <td>1</td> </tr> <tr> <td>3</td> <td>1</td> </tr> <tr> <td>4</td> <td>1</td> </tr> <tr> <td>4</td> <td>1</td> </tr> <tr> <td>5</td> <td>1</td> </tr> <tr> <td>5</td> <td>1</td> </tr> <tr> <td>5</td> <td>1</td> </tr> </tbody> </table> </div> <div id="banner-message2"> Based on the value change <table> <thead> <tr> <th>Key</th> <th>val</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>1</td> </tr> <tr> <td>1</td> <td>1</td> </tr> <tr> <td>4</td> <td>1</td> </tr> <tr> <td>4</td> <td>1</td> </tr> <tr> <td>4</td> <td>1</td> </tr> <tr> <td>2</td> <td>1</td> </tr> <tr> <td>3</td> <td>1</td> </tr> <tr> <td>3</td> <td>1</td> </tr> <tr> <td>5</td> <td>1</td> </tr> <tr> <td>5</td> <td>1</td> </tr> <tr> <td>5</td> <td>1</td> </tr> </tbody> </table> </div>

I hope this will help you :)我希望这能帮到您 :)

If you want to get the whole string in column use如果你想在列中使用整个字符串

$(document).ready(function(){
    //let prevValue = parseInt($("#banner-message2 tbody tr").first().find("td").first().html());
    let prevValue = $("#banner-message2 tbody tr").find("td:first").html();
    console.log(prevValue);
    let currentClass = '';
    $("#banner-message2 tbody tr").each(function() {
        if(prevValue != $(this).find("td:first").html()){
        (currentClass=='')?currentClass = 'altColor':currentClass='';
        }
        $(this).addClass(currentClass);
        prevValue = $(this).find("td:first").html();
    });
});

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

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