【问题标题】:Mark highest and lowest value in HTML table在 HTML 表格中标记最高和最低值
【发布时间】:2014-06-27 22:09:47
【问题描述】:

我有一个 HTML 表格,我想通过向每列的最高和最低值添加一个类来标记单元格。我在这里找到了一些相关的问题,但是代码行为不端。

var $table = $("#mytable");
$table.find("th").each(function(columnIndex)
{
    var oldValue=0, currentValue=0, $elementToMark;
    var $trs = $table.find("tr");
    $trs.each(function(index, element)
    {
        oldValue= currentValue;
        var $td = $(this).find("td:eq("+ columnIndex +")");
        if ($td.length!=0) 
        {
            currentValue= parseFloat($td.html());
            if(currentValue > oldValue)
            {
                $elementToMark= $td;
            }
            if(index == $trs.length-1)
            {
              $elementToMark.addClass("highest"); 
            }
        }
        });
});


var $table = $("#mytable");
$table.find("th").each(function(columnIndex)
{
    var oldValue=1000000, currentValue=1000000, $elementToMark;
    var $trs = $table.find("tr");
    $trs.each(function(index, element)
    {
        oldValue= currentValue;
        var $td = $(this).find("td:eq("+ columnIndex +")");
        if ($td.length!=0) 
        {
            currentValue= parseFloat($td.html());
            if(currentValue < oldValue)
            {
                $elementToMark= $td;
            }
            if(index == $trs.length-1)
            {
              $elementToMark.addClass("lowest"); 
            }
        }
        });
});

这里也是一个 JSFiddle:Link

问题是它没有标记正确的值,我看不到原因。

【问题讨论】:

    标签: javascript jquery html


    【解决方案1】:

    你在错误的地方更新oldValue

     var $table = $("#mytable");
        $table.find("th").each(function(columnIndex)
        {
            var oldValue=0, currentValue=0, $elementToMark;
            var $trs = $table.find("tr");
            $trs.each(function(index, element)
            {
    
                var $td = $(this).find("td:eq("+ columnIndex +")");
                if ($td.length!=0) 
                {
                    currentValue= parseFloat($td.html());
                    if(currentValue > oldValue)
                    {
                        $elementToMark= $td;
                        oldValue= currentValue;
                    }
                    if(index == $trs.length-1)
                    {
                      $elementToMark.addClass("highest"); 
                    }
                }
                });
        });
    
    
        var $table = $("#mytable");
        $table.find("th").each(function(columnIndex)
        {
            var oldValue=1000000, currentValue=1000000, $elementToMark;
            var $trs = $table.find("tr");
            $trs.each(function(index, element)
            {
    
                var $td = $(this).find("td:eq("+ columnIndex +")");
                if ($td.length!=0) 
                {
                    currentValue= parseFloat($td.html());
                    if(currentValue < oldValue)
                    {
                        $elementToMark= $td;
                        oldValue= currentValue;
    
                    }
                    if(index == $trs.length-1)
                    {
                      $elementToMark.addClass("lowest"); 
                    }
                }
                });
        })
    

    ;

    【讨论】:

      【解决方案2】:

      此代码将允许在最低或最高值之间打成平手。抱歉...我开始玩它,但忍不住...

      var $table = $("#mytable");
      $table.find("th").each(function(columnIndex)
      {
          var oldValue=0, currentValue=0;
          var $trs = $table.find("tr");
          var highElements = [];
          var lowElements = [];
          var lowestValue = 99999;
          var highestValue = 0;
      
          console.log('new column ' + columnIndex);
      
          $trs.each(function(index, element)
          {
              oldValue= currentValue;
              var cell = $(this).find("td:eq("+ columnIndex +")");
      
              if (cell.length!=0) 
              {
                  currentValue= parseInt(cell.html());
                  if(currentValue < lowestValue)
                  {
                      lowestValue = currentValue;
                      lowElements = [];
                      lowElements.push(cell);
                  }
                  else if (currentValue == lowestValue) {
                      lowElements.push(cell);
                  }
      
                  if (currentValue > highestValue)
                  {
                      highestValue = currentValue;
                      highElements = [];
                      highElements.push(cell);
                  }
                  else if (currentValue == highestValue) {
                      highElements.push(cell);
                  }
              }
          });
      
          $.each(lowElements, function(i, e){
              $(e).addClass('lowest');
          });
      
          $.each(highElements, function(i, e){
              $(e).addClass('highest');
          });
      

      });

      希望对你有帮助。

      在这里工作小提琴...http://jsfiddle.net/vqfPA/6/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-13
        • 1970-01-01
        • 2021-06-24
        • 1970-01-01
        相关资源
        最近更新 更多