Check negative balance

Asked

Viewed 204 times

0

Via Javascript or Jquery, I need to check if there is a negative balance. If you have a negative balance, then: var SALDO_DISPONIVEL = 0 other than: var SALDO_DISPONIVEL = 1

The following is the source html:

 <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> 

  • How do you know what is the negative giving value? did not fail to say what is the account !

  • In other words, if you have any <td id='balance'> that is negative the SALDO_DISPONIVEL variable must be 0. In this example of HTML there is no negative balance, then the SALDO_DISPONIVEL variable must be 1

2 answers

1


Very simple using javascript only... Take all elements with class . balance, check if you have any negative, if you have 0. else it is 1.

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

Edit: had parseint, changed code to 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>

  • Perfect friend! I read all the answers, but this one served as a GLOVE! This is what I needed!

  • @Y.Menon Thanks friend, I’m happy to help.

0

Select all the latter tds table and make a loop for checking that the value is less than 0.

But it is necessary to treat the values before the check, take the points that separate the thousand and then replace the comma by point, since Javascript considers the "point" the decimal separator.

The code starts the SALDO_DISPONIVEL worthwhile 1, and the for keep checking line by line and if one of the values is less than zero, it changes the value of the variable SALDO_DISPONIVEL for 0 and stop 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>

Browser other questions tagged

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