Skip to content Skip to sidebar Skip to footer

Find The Smallest Value Of A Table Row Using Javascript

Ok currently I'm using this code to find it but I need to filter out null fields so that all my cells that are blank don't turn red. $('tr').each(function highlight() { var $td

Solution 1:

if $('#asd').text() evaluates to ""

then +$('#asd').text() will evaluate to 0


To find minimum in whole table

http://jsfiddle.net/6DgAW/

var vals = $('tr td').map(function () {
    returnisNaN(parseInt($(this).text(), 10)) ? parseInt($(this).text(), 10) :  null;
}).get();

// then find their minimumvar min = Math.min.apply(Math, vals);

// tag any cell matching the min value
$('tr td').filter(function () {
    returnparseInt($(this).text(), 10) === min;
}).addClass('alert alert-danger');

To find minimum in each row

http://jsfiddle.net/DggUN/18/

$('tr').each(function(){
    var vals = $('td,th',this).map(function () {
        returnparseInt($(this).text(), 10) ? parseInt($(this).text(), 10) :  null;
    }).get();
    // then find their minimumvar min = Math.min.apply(Math, vals);

    // tag any cell matching the min value
    $('td,th', this).filter(function () {
        returnparseInt($(this).text(), 10) === min;
    }).addClass('alert alert-danger');
});

Solution 2:

I havent tested this code

var min=0; 
   $("tr").each(function(e){
     var tr = $(this);
     var valuses =$(this).find(".YourClassNameForTheParticularfield").html(); 
     if(!isNaN(Number(Values))) //NullChecking
     {
        if(min>Number(mark));
          {
            min = mark;
          }
      }
   });

isNaN checks whether it is not a number which includes null or text , Using isNaN may solve your issue

Solution 3:

var rows = $("tr");
var minValArray = newArray();
for(var i = 0; i < rows.length; i++) {
    var row = rows[i];
    var elements = $(row).find("td");
    var min = 9999999999; // make this larger than other numbers in your tablefor(var j = 0; j < elements.length; j++) {
        if(!isNaN(parseInt($(elements[j]).text())) && parseInt($(elements[j]).text()) < min)
             min = parseInt($(elements[j]));
    }
    minValArray.push(min);
}

This should give minimum of all the rows in an array.

Post a Comment for "Find The Smallest Value Of A Table Row Using Javascript"