Multiply row

1

Personnel how can I multiply the total value of the row being that I want to multiply value by value of each line

var bloco1 = $("table tr").find("td").eq(0).val();
var bloco2 = $("table tr").find("td").eq(1).val();
var bloco3 = $("table tr").find("td").eq(2).val();

var valor = eval(bloco1) * eval(bloco2) * eval(bloco3);

$("#result").html(valor);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><tableborder="1">
              <tr>
                 <th>exe</th>
                 <th>coluna2</th>
                 <th>coluna3</th>
                 <th>coluna4</th>
                 <th>coluna5</th>
              </tr>
              <tr>
                 <th>linha1</th>
                 <td><a href="#">10</a></td>
                 <td><a href="#">20</a></td>
                 <td><a href="#">15</a></td>
                 <td><a href="#">5</a></td>
              </tr>
                 <tr>
                 <th>linha2</th>
                 <td><a href="#">20</a></td>
                 <td><a href="#">28</a></td>
                 <td><a href="#">12</a></td>
                 <td><a href="#">11</a></td>
              </tr>               
              <tr>
                 <th>linha3</th>
                 <td><a href="#">3</a></td>
                 <td><a href="#">5</a></td>
                 <td><a href="#">2</a></td>
                 <td><a href="#">8</a></td>
              </tr>
    
         </table>
<p id="result">Resultado: </p>
    
asked by anonymous 18.03.2016 / 13:01

3 answers

2

You can do it like this:

$('tr').each(function(i, tr) {
    if (i == 0) return; // para não contar com a primeira linha de titulos
    var nome = $(this).find('th').text(); // vai buscar o nome
    var valor = $(this).find('td a').get().reduce(function(sum, el) {
        return sum * parseFloat(el.innerHTML); // multiplica todos os valores da linha
    }, 1);
    var p = $('<p/>', { // cria um elmento "p" com o resultado
        html: [nome, valor].join(': ')
    });
    $('#result').append(p);
});

jsFiddle: link

    
06.05.2016 / 21:21
1

You will need to use a loop to go through all the elements and get the value of each and multiply them.

var blocos = $("table tr").find("td");
var total = 1;

blocos.each(function (index, element) {
  total *= parseInt($(this).find('a').text());
});

console.log(total);

In the example, first I got all the tds and added it to a single object, then I use each to go through each one, then access the element using this and get the value that is in the a tag %, convert to integer and multiply all values.

    
18.03.2016 / 13:49
1

You'll need to put an identifier on every tr.

<table border="1">
              <tr id="0">
                 <th>exe</th>
                 <th>coluna2</th>
                 <th>coluna3</th>
                 <th>coluna4</th>
                 <th>coluna5</th>
              </tr>
              <tr id="1">
                 <th>linha1</th>
                 <td><a href="#">10</a></td>
                 <td><a href="#">20</a></td>
                 <td><a href="#">15</a></td>
                 <td><a href="#">5</a></td>
              </tr>
                 <tr id ="2">
                 <th>linha2</th>
                 <td><a href="#">20</a></td>
                 <td><a href="#">28</a></td>
                 <td><a href="#">12</a></td>
                 <td><a href="#">11</a></td>
              </tr>               
              <tr id="3">
                 <th>linha3</th>
                 <td><a href="#">3</a></td>
                 <td><a href="#">5</a></td>
                 <td><a href="#">2</a></td>
                 <td><a href="#">8</a></td>
              </tr>

         </table>
<p id="result">Resultado: </p>

Then perform a each to retrieve information for each line

var array1 = array();
var array2 = array();
var array3 = array();

$("#1 a").each(function (index, element) {
     //armazenar no array 1
     array1[index] = parseInt($(this).html()); 
});
...
$("#3 a").each(function (index, element) {
     //armazenar no array 3
     array3[index] = parseInt($(this).html()); 
});

Then you can use the array to perform any type of calculation.

    
18.03.2016 / 15:48