简体   繁体   English

jQuery / JavaScript在表列中找到最高价值

[英]jQuery/JavaScript find highest value in table column

Here is my table 这是我的桌子

<table id="aktivita">
  <tr>
    <td>Jack</td>
    <td>450</td>
    <td>794</td>
    <td>784</td>
  </tr>
  <tr>
    <td>Steve</td>
    <td>145</td>
    <td>794</td>
    <td>789</td>
  </tr>
  <tr>
    <td>Mike</td>
    <td>2794</td>
    <td>2794</td>
    <td>79</td>
  </tr>
</table>

How can I get each column since the second (after the names) and set the highest value class high, the lowest value class low? 如何获得第二列(名称之后)的每一列,并将最高值类别设置为高,将最低价值类别设置为低? If more td have same value and is highest (or lowest) all must have proper class. 如果更多的td具有相同的值并且最高(或最低),则所有td必须具有适当的类别。

$('#aktivita tr').each(function(){
  var first = $(this).find("td:nth-of-type(2)").text();
  var second = $(this).find("td:nth-of-type(3)").text();
  var third = $(this).find("td:nth-of-type(4)").text();


  console.log(first);
  console.log(second);
  console.log(third);
});

This is what I have yet. 这就是我所拥有的。 I don't like much that im using for variables basicly same code. 我不太喜欢即时消息用于基本上相同代码的变量。 Is there a way how to make it dry? 有没有办法使它干燥?

You can loop over td s and find min and max and process accordingly 您可以遍历td s并找到min和max并进行相应处理

 $(function(){ var cols = [] var trs = $('#aktivita tr') var data =$.each(trs , function(index, tr){ $.each($(tr).find("td").not(":first"), function(index, td){ cols[index] = cols[index] || []; cols[index].push($(td).text()) }) }); cols.forEach(function(col, index){ var max = Math.max.apply(null, col); var min = Math.min.apply(null, col) $('#aktivita tr').find('td:eq('+(index+1)+')').each(function(i, td){ $(td).toggleClass('max', +$(td).text() === max) $(td).toggleClass('min', +$(td).text() === min) }) }) }) 
 .max { color: blue } .min { color: red } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <table id="aktivita"> <tr> <td>Jack</td> <td>450</td> <td>794</td> <td>784</td> </tr> <tr> <td>Steve</td> <td>145</td> <td>794</td> <td>789</td> </tr> <tr> <td>Mike</td> <td>2794</td> <td>2794</td> <td>79</td> </tr> </table> 

You can use map() to find min and max value of each tr and add classes. 您可以使用map()查找每个tr最小值和最大值并添加类。

 var i = 1; while (i < $('tr:first-child td').length) { var ar = $('table tr').map(function() { return Number($(this).find('td:nth-child(' + (i + 1) + ')').text()); }) var max = Math.max.apply(null, ar); var min = Math.min.apply(null, ar); var ar = $('table tr').find('td').each(function() { if (Number($(this).text()) == max) { $(this).addClass('max'); } else if (Number($(this).text()) == min) { $(this).addClass('min'); } }) i++; } 
 .max { color: red; } .min { color: green; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id="aktivita"> <tr> <td>Jack</td> <td>450</td> <td>794</td> <td>784</td> </tr> <tr> <td>Steve</td> <td>145</td> <td>794</td> <td>789</td> </tr> <tr> <td>Mike</td> <td>2794</td> <td>2794</td> <td>79</td> </tr> </table> 

This one seems to work for me: 这个似乎对我有用:

var offset = 1;

$('#aktivita tr').each(function () {
    var values = [];

    $(this).children('td').each(function (i,v) {
        if (i < offset) return;
        var value = parseInt($(this).text());
        if(!isNaN(value))
            values.push(value);
    });

    var maxValue = Math.max.apply(null, values);
    var minValue = Math.min.apply(null, values);

    $(this).children('td').each(function () {
        if ($(this).text() == maxValue.toString()) {
            $(this).addClass('max');
        }

        if ($(this).text() == minValue.toString()) {
            $(this).addClass('min');
        }
    });

});

Where offset is the number of columns before the actual values start ( just in case you only delivered simplified data). 其中offset是实际值开始之前的列数(以防万一,您仅提供了简化的数据)。

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

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