【问题标题】:Highlight duplicate row values JavaScript突出显示重复的行值 JavaScript
【发布时间】:2019-02-15 22:14:01
【问题描述】:

我正在使用 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>

【问题讨论】:

    标签: javascript jquery arrays string dom


    【解决方案1】:

    不要将array 用于rowValues,最好使用object,以便检查key 值是否存在。

    您还可以使用颜色的array 来获取动态颜色,并在找到新值时不断移动array,这样每个不同的值都会有其相关的不同颜色。

    并且没有必要检查else 块中的count,因为每当你到达这个块时,就意味着这个value 已经存在于array 中。

    这应该是你的代码:

    $(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>

    【讨论】:

      【解决方案2】:

      我会为每个内容文本创建一个数组,其中包含具有相同内容的单元格。一旦你有了这个,你可以迭代它并根据需要突出显示单元格。

      为了生成随机颜色,我添加了一个方法,它有一个 Set 来跟踪生成的颜色。它将检查之前是否已生成随机生成器颜色并继续生成颜色,直到生成唯一颜色。

      您最终可能会使用使文本难以辨认的颜色或两种没有足够对比度来区分它们的随机颜色。所以这不是一个完美的解决方案。

      function generateRandomInt(max, min = 0) {
        return Math.floor(Math.random() * (max - min + 1) + min);
      }
      
      /**
       * This method will return a new method, when the returned method is 
       * called it will return a unique color. Subsequent calls to the color
       * generator will never return the same color.
       */
      function colorGenerator() {
        // Create a Set at the function scope which we can use to keep
        // track of the colors generated by the returned method.
        const
          generatedColors = new Set();
          
        return () => {
          let randomColor;
          // Keep generating a random color in the format "rgb(R,G,B)" until 
          // a color is generated that doesn't yet exist in the set. This doesn't
          // take into account that at some point you'll run out of 
          // possible colors (but it will take 16M+ tries).
          do {
            randomColor = `rgb(${generateRandomInt(255)},${generateRandomInt(255)},${generateRandomInt(255)})`;
          } while (generatedColors.has(randomColor));
          
          // Add the generated, unique, color to the set.
          generatedColors.add(randomColor);
          
          // Return the random color.
          return randomColor;  
        };
      }
      
      function highlightDoubles(table) {
        const
          // Get all the element with the content CSS class.
          contentCells = table.querySelectorAll('.content'),
          // Create map, the cell content will be the key and the value will be
          // an array with cells that have the key as content.
          contentMap = new Map();
         
        // IE doesn't support forEach on a NodeList, convert it to an array first.
        Array.from(contentCells).forEach(cell => {
          const
            // For each cell check if the content has been encountered before. If so
            // return the map value and else create a new array.
            array = (contentMap.has(cell.textContent))
              ? contentMap.get(cell.textContent)
              : [];
            // Push the current cell into the array.
            array.push(cell)
            // Store the array in the map.
            contentMap.set(cell.textContent, array);
        });
      
        // Create a color generator, it will create a random
        // color but never the same color twice.
        const 
          randomColor = colorGenerator();
          
        // Iterate over all the entries in the map, each entry is a unique 
        // cell content text
        contentMap.forEach(cells => {
          // When the lengths of the cells array is less than 2 it means 
          // it is not multiple times in the table. Exit now.
          if (cells.length < 2) {
            return;
          }
          
          // Generate a random color for the current content text. This is just
          // a very naive implementation. It doesn't make any promises on readability.
          const
            color = randomColor();
            
          // Apply the random color to all the cells with the same content.
          cells.forEach(cell => {
            cell.style.backgroundColor = color;
          });
        });
      }
      
      highlightDoubles(document.getElementById('sortable'));
      <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>

      【讨论】:

        【解决方案3】:

        每当您在 if 语句中创建一个新对象时,请执行类似的操作

        var rowComposite = new Object();
        rowComposite.count = 1;
        rowComposite.row = this;
        var myColor = generateRandomColor();
        rowComposite.color = myColor;
        

        然后,当您分配颜色时,分配它 .color 而不仅仅是“红色”:

        $(rowComposite.row).css('backgroundColor', rowComposite.color);
        

        您可以在此处阅读有关随机颜色生成的 js:Random color generator

        【讨论】:

          【解决方案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',getRandomColor());
                      }
                     // $(this).css('backgroundColor', getRandomColor());
                     $(this).css('backgroundColor', 'red');
                      rowComposite.count++;
                  }
              });
          });
          
          function getRandomColor() {
            var letters = '0123456789ABCDEF';
            var color = '#';
            for (var i = 0; i < 6; i++) {
              color += letters[Math.floor(Math.random() * 16)];
            }
            return 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>
                  <tr>
                      <td class="content">C</td>
                  </tr>
              </tbody>
          </table>

          【讨论】:

            【解决方案5】:
            function random_rgba() {
                var o = Math.round, r = Math.random, s = 255;
                return 'rgba(' + o(r()*s) + ',' + o(r()*s) + ',' + o(r()*s) + ',' + r().toFixed(1) + ')';
            }
            
            $(function() {
            
                var tableRows = $("#sortable tbody tr"); //find all the rows
                var rowValues =  {}; //to keep track of which values appear more than once
                var color = "";
                tableRows.each(function() { 
                    var rowValue = $(this).find(".content").html();
                    if (rowValues[rowValue]){
                        color = rowValues[rowValue];
                    }else{
                        color = random_rgba();
                      rowValues[rowValue] = color;
                    }
                    $(this).css('backgroundColor', color);
                });
            });
            

            在这里你有它的工作:https://jsfiddle.net/zn0g9u34/9/

            【讨论】:

              【解决方案6】:

              你可以试试这个。简约时尚:

              $(function(){
                  var st=document.createElement("style");
                  document.head.appendChild(st);
                  var sty={}, s;
                  var A={}, cou=1;
                  $("#sortable .content").each(function(i, c){
                       if(!sty[c.innerHTML]){
                          sty[c.innerHTML]=1;
                          A[c.innerHTML]=cou++;
                       }else sty[c.innerHTML]+=1;
                       c.setAttribute("data-w", A[c.innerHTML]);
                  });
                  for(s in sty){
                      if(sty[s]>1){
                          st.sheet.insertRule("#sortable .content[data-w='"+A[s]+"']{background-color:rgb("+
                              parseInt(Math.random()*256)+","+ 
                              parseInt(Math.random()*256)+","+
                              parseInt(Math.random()*256)+
                          ");}", 0);
                      };
                  };
              });
              

              对不起,我正在用手机打字,无法使用小提琴。


              Try it online!

              【讨论】:

                猜你喜欢
                • 2016-06-19
                • 1970-01-01
                • 2016-09-07
                • 1970-01-01
                • 2016-03-13
                • 2014-03-28
                • 2014-05-02
                • 1970-01-01
                相关资源
                最近更新 更多