Check Negative Balance

0

Via JavaScript or Jquery, I need to check if there is any negative balance. If you have a negative balance, then: var BALANCE_DISPONIVEL = 0 if no: var BALANCE_DISPONIVEL = 1

Below is the html source:

 <html> 
 <head> 
 	<title>Histórico das Aprovações</title> 
 	<meta http-equiv='Content-Type' content='text/html; charset=ISO-8859-1'> 
 	<style type='text/css'> 
 		table { 
 			table-layout: fixed; 
 			border-collapse: collapse; 
 		} 
  
 		th { 
 			border: 1px solid #000; 
 			font-family: Verdana, Arial, sans-serif; 
 			font-size: 0.8em; 
 			background-color: #009fef; 
 			padding: 0px 5px 0px 5px; 
 		} 
  
 		td { 
 			border: 1px solid #000; 
 			font-family: Verdana, Arial, sans-serif; 
 			font-size: 0.8em; 
 			padding: 0px 5px 0px 5px; 
 		} 
  
 		.centro { 
 			text-align: center; 
 		} 
  
 		.cinzaClaro { 
 			background-color: #CACACA; 
 		} 
  
 		.cinzaEscuro { 
 			background-color: #EBEBEB; 
 		} 
 	</style> 
 </head> 
 <body style='text-align: center; margin-left: 0px; margin-top: 3px;'> 
 	<table> 
 		<thead> 
 			<tr> 
 				<th style='width: 100px;'>Material</th> 
 				<th style='width: 100px;'>Verba Total</th> 
 				<th style='width: 100px;'>Saldo Disponível</th> 
 			</tr> 
 		</thead> 
 		<tbody> 
 			<tr class='cinzaEscuro'> 
 				<td class='centro'>210</td> 
 				<td class='centro'>76.045,00</td> 
 				<td id='saldo' class='centro'>45.555,00</td> 
 			</tr> 
 			<tr class='cinzaClaro'> 
 				<td class='centro'>211</td> 
 				<td class='centro'>100,00</td> 
 				<td id='saldo' class='centro'>80,00</td> 
 			</tr> 
 		</tbody> 
 	</table> 
 </body> 
 </html> 
    
asked by anonymous 23.02.2018 / 14:04

2 answers

1

Very simple using only javascript ... Get all the elements with class .saldo, check if you have any negatives, if it has a 0. mark, otherwise it gets 1.

PS: Remember to avoid duplicating ids on the page, id must be unique. In this case it is best to use one class to identify the items since there are several.

Edit: I had done parseInt, I changed code for parseFloat: D

window.onload = function() {
  var saldos = document.querySelectorAll('.saldo');
  var output = document.getElementById('disponivel');
  
  var saida = 1;
  let innerValue;
  for(let k of saldos) {
    innerValue = k.innerHTML.replace(/\./g, '').replace(/,/g, '.');
    if(parseFloat(innerValue) < 0) {
      saida = 0;
      break;
    }
  }
  
  output.innerHTML = "SALDO DISPONIVEL: " + saida;
}
<html>
<head>
  <title>Histórico das Aprovações</title>
  <meta http-equiv='Content-Type' content='text/html; charset=ISO-8859-1'>
  <style type='text/css'>
    table {
      table-layout: fixed;
      border-collapse: collapse;
    }
    
    th {
      border: 1px solid #000;
      font-family: Verdana, Arial, sans-serif;
      font-size: 0.8em;
      background-color: #009fef;
      padding: 0px 5px 0px 5px;
    }
    
    td {
      border: 1px solid #000;
      font-family: Verdana, Arial, sans-serif;
      font-size: 0.8em;
      padding: 0px 5px 0px 5px;
    }
    
    .centro {
      text-align: center;
    }
    
    .cinzaClaro {
      background-color: #CACACA;
    }
    
    .cinzaEscuro {
      background-color: #EBEBEB;
    }
  </style>
</head>

<body style='text-align: center; margin-left: 0px; margin-top: 3px;'>
  <table>
    <thead>
      <tr>
        <th style='width: 100px;'>Material</th>
        <th style='width: 100px;'>Verba Total</th>
        <th style='width: 100px;'>Saldo Disponível</th>
      </tr>
    </thead>
    <tbody>
      <tr class='cinzaEscuro'>
        <td class='centro'>210</td>
        <td class='centro'>76.045,00</td>
        <td class='centro saldo'>45.555,00</td>
      </tr>
      <tr class='cinzaClaro'>
        <td class='centro'>211</td>
        <td class='centro'>100,00</td>
        <td class='centro saldo'>80,00</td>
      </tr>
    </tbody>
  </table>
  
  <p id="disponivel"></p>
  
</body>
  
</html>
    
23.02.2018 / 14:56
0

Select all last tds of the table and loop for by checking if the value is less than 0 .

But you have to treat the values before the check, get the points that separate the thousand and then replace the comma with a point, since JavaScript considers the "dot" the decimal separator.

The code starts the SALDO_DISPONIVEL with value 1 , and the for checks line by line and if one of the values is less than zero, it changes the value of the variable SALDO_DISPONIVEL to 0 and pauses the loop.

$(function(){
   var tds =  $("table tbody tr td:last-child"),
       SALDO_DISPONIVEL = 1;
   
   for(var x=0; x<tds.length; x++){
      var valor = $(tds[x]).text().replace(".","").replace(",",".");
      if(parseFloat(valor) < 0){ SALDO_DISPONIVEL = 0; break; }
   }
   
   console.log("Saldo disponível = "+SALDO_DISPONIVEL);
});
table { 
   table-layout: fixed; 
   border-collapse: collapse; 
} 

th { 
   border: 1px solid #000; 
   font-family: Verdana, Arial, sans-serif; 
   font-size: 0.8em; 
   background-color: #009fef; 
   padding: 0px 5px 0px 5px; 
} 

td { 
   border: 1px solid #000; 
   font-family: Verdana, Arial, sans-serif; 
   font-size: 0.8em; 
   padding: 0px 5px 0px 5px; 
} 

.centro { 
   text-align: center; 
} 

.cinzaClaro { 
   background-color: #CACACA; 
} 

.cinzaEscuro { 
   background-color: #EBEBEB; 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table> 
   <thead> 
      <tr> 
         <th style='width: 100px;'>Material</th> 
         <th style='width: 100px;'>Verba Total</th> 
         <th style='width: 100px;'>Saldo Disponível</th> 
      </tr> 
   </thead> 
   <tbody> 
      <tr class='cinzaEscuro'> 
         <td class='centro'>210</td> 
         <td class='centro'>76.045,00</td> 
         <td id='saldo' class='centro'>45.555,00</td> 
      </tr> 
      <tr class='cinzaClaro'> 
         <td class='centro'>211</td> 
         <td class='centro'>100,00</td> 
         <td id='saldo' class='centro'>80,00</td> 
      </tr> 
      <tr class='cinzaEscuro'> 
         <td class='centro'>211</td> 
         <td class='centro'>100,00</td> 
         <td id='saldo' class='centro'>-80,00</td> 
      </tr> 
      <tr class='cinzaClaro'> 
         <td class='centro'>211</td> 
         <td class='centro'>100,00</td> 
         <td id='saldo' class='centro'>80,00</td> 
      </tr> 
   </tbody> 
</table>
    
23.02.2018 / 15:17