gpt4 book ai didi

javascript - 突出显示重复的行值 JavaScript

转载 作者:行者123 更新时间:2023-11-29 10:28:32 24 4
gpt4 key购买 nike

我正在使用 JS 来突出显示表格中的重复值。

基本上代码所做的是在数组中添加表行值,然后比较它是否存在,然后突出显示该行。

代码运行良好,但它以相同颜色(红色)突出显示所有重复值。我需要它做的是用不同的颜色突出显示每组相似的值。可以说我有 4 组重复值,每组都应该以不同的颜色突出显示。可能需要随机生成颜色,因为表中可能有多个重复值。

 $(function() {
var tableRows = $("#sortable tbody tr"); //find all the rows
var rowValues = []; //to keep track of which values appear more than once
tableRows.each(function() {
var rowValue = $(this).find(".content").html();
if (!rowValues[rowValue]) {
var rowComposite = new Object();
rowComposite.count = 1;
rowComposite.row = this;
rowValues[rowValue] = rowComposite;
} else {
var rowComposite = rowValues[rowValue];
if (rowComposite.count == 1) {
$(rowComposite.row).css('backgroundColor', 'red');
}
$(this).css('backgroundColor', 'red');
rowComposite.count++;
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="sortable">
<tbody>
<tr>
<td class="content">A</td>
</tr>
<tr>
<td class="content">B</td>
</tr>
<tr>
<td class="content">A</td>
</tr>
<tr>
<td class="content">C</td>
</tr>
<tr>
<td class="content">C</td>
</tr>
</tbody>
</table>

最佳答案

与其对rowValues 使用数组,不如使用对象,这样您就可以检查 是否存在键 值。

您还可以使用array 颜色,从那里获取动态颜色并不断移动array,只要您找到新值,这样每个不同的值都会具有其相关的独特颜色。

并且不需要检查 else block 中的 count,因为每当你到达这个 block 时,就意味着这个 value 已经存在于数组中。

你的代码应该是这样的:

$(function() {
var tableRows = $("#sortable tbody tr"); //find all the rows
var colors = ["red", "blue", "green", "yellow", "#f5b"];
var rowValues = {};
tableRows.each(function() {
var rowValue = $(this).find(".content").html();
if (!rowValues[rowValue]) {
var rowComposite = new Object();
rowComposite.count = 1;
rowComposite.row = this;
rowValues[rowValue] = rowComposite;
} else {
var rowComposite = rowValues[rowValue];
if (!rowComposite.color) {
rowComposite.color = colors.shift();
}
rowComposite.count++;
$(this).css('backgroundColor', rowComposite.color);
$(rowComposite.row).css('backgroundColor', rowComposite.color);
}
});
});

演示:

$(function() {
var tableRows = $("#sortable tbody tr"); //find all the rows
var colors = ["red", "blue", "green", "yellow", "#f5b"];
var rowValues = {};
tableRows.each(function() {
var rowValue = $(this).find(".content").html();
if (!rowValues[rowValue]) {
var rowComposite = new Object();
rowComposite.count = 1;
rowComposite.row = this;
rowComposite.color = colors.shift();
rowValues[rowValue] = rowComposite;
} else {
var rowComposite = rowValues[rowValue];
rowComposite.count++;
$(this).css('backgroundColor', rowComposite.color);
$(rowComposite.row).css('backgroundColor', rowComposite.color);
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="sortable">
<tbody>
<tr>
<td class="content">A</td>
</tr>
<tr>
<td class="content">B</td>
</tr>
<tr>
<td class="content">A</td>
</tr>
<tr>
<td class="content">C</td>
</tr>
<tr>
<td class="content">C</td>
</tr>
</tbody>
</table>

关于javascript - 突出显示重复的行值 JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52275379/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com