Multiply line

Asked

Viewed 325 times

1

Personal how can I multiply the total value of the lines 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>
<table border="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>

  • Multiply column value ? how would this be?

  • Didn’t I understand, you that the value of lines or the value contained in the lines? found the question a little confusing

  • user3010128 - I want to take the values contained in the lines and multiply row by row. exe: 10 * 20 = 200 - 200 * 3 =600

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: https://jsfiddle.net/dt6orytk/

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, I first got all the tds and added in a single object, then use the each to go through each, then access the element using the this and take the amount that is on the tag a, convert to integer and multiply all values.

1

You’ll need to put an identifier on each 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 the information from 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 kind of calculation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.