标记HTML表格中的最高和最低值

时间:2014-06-27 13:05:40

标签: javascript jquery html

我有一个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

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

2 个答案:

答案 0 :(得分:4)

您正在错误的地方更新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"); 
                }
            }
            });
    })

答案 1 :(得分:1)

此代码允许最低或最高值之间的平局。对不起......我开始玩它并且无法帮助它......

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/

相关问题