3
votes

I have tabular form where col1 and col2 are numbers, and col3 should contain the difference between col1 and col2.

col1 has already had data and col 1 is editable, based on change in col1, col3 data will dynamically change.

To achieve this I have used ajax callback process and JavaScript, But the problem is when the difference is 0.xx -0.xx, I am getting parsing error. other values I am able to print in col3.

Declare    
p_curr  number;    
p_prev    number;    
p_diff number;    

Begin    
 p_prev    := to_number(apex_application.g_x01);    
 p_curr    := to_number(apex_application.g_x02);    

   SELECT p_curr - p_prev into p_diff    
   from dual;    

  -- return calculated value  
   sys.htp.p(p_diff);    
 End;  

--JavaScript

 function f_CALC_DIFF(pThis) {    

 var row_id     = pThis.id.substr(4);    
 var s          = $('#f18_'+row_id).val().replace(/[^\d.-]/g, '');   
 var curr       = $(pThis).val().replace(/[^\d.-]/g, '');   

 if(!s){  
  var s= 0;  
  var prev = s;}  
else{  
  var prev = s;}  

apex.server.process   
( "CALC_DIFF", { x01: prev, x02: curr },   
{ success: function( pData ) {   
$('#f23_'+row_id).val(pData);}}  
);    
}   

Please suggest the solution for the above problem.

I'm using Oracle Apex version 4.2

1
What parsing error exactly, and where? Why use to_number here when value is already a number: to_number(p_curr - p_prev)? You should be able to calculate the difference between 2 numbers in Javascript, without needing an expensive AJAX call to the datbase.Tony Andrews
Parsing error: Syntax Error, Unexpected token. The problem is I want to print the value in col3 dynamically based on changes made in col2 as it is editable column. col3 value is (col2-col1) So I used the Ajax call to database.ApexDev

1 Answers

3
votes

Instead of using expensive AJAX call to database, use JavaScript.

function f_CALC_DIFF(pThis) {    

 var row_id     = pThis.id.substr(4);    
 var s          = $('#f18_'+row_id).val().replace(/[^\d.-]/g, '');   
 var curr       = $(pThis).val().replace(/[^\d.-]/g, '');   

if(!s){  
var s= 0;  
var prev = s;}    
else{  
var prev = s;}  

  var diff = 0; 
 diff = (curr - prev);
 var n = diff.toFixed(2);
 alert (n);
 $('#f23_'+row_id).val(n);

// Please remove the AJAX call to database.
 /*apex.server.process   
( "CALC_DIFF", { x01: prev, x02: curr },   
{ success: function( pData ) {   
$('#f23_'+row_id).val(pData);}}  
);    
}   */