Questions and discussion about developing processes and programming in PHP, JavaScript, web services & REST API.
Forum rules: Please search to see if a question has already asked before creating a new topic. Please don't post the same question in multiple forums.
By springhigh
#795946
Sorry if this has been posted before,
If i have a formula to divide one textbox by another,
is it possible that the result can be rounded off to 2 decimal places inside the current dynaform.

many thanks
User avatar
By amosbatto
#795951
You can't round with a formula, but you can use this JavaScript code in your DynaForm:
Code: Select all
function roundToFixed(_float, _digits){
  var rounder = Math.pow(10, _digits);
  return (Math.round(_float * rounder) / rounder).toFixed(_digits);
}

function roundTotal(price, quantity) {
  var total = '';
  if (price != '' && quantity != '') {
    var total = price * quantity;
    total = roundToFixed(total, 2)
  }
  $("#total").setValue(total);
}

//execute when the "price" or "quantity" changes:
$("#price").setOnchange(function(newVal, oldVal) {
   roundTotal(newVal, $("#quantity").getValue());
})
$("#quantity").setOnchange(function(newVal, oldVal) {
   roundTotal($("#price").getValue(), newVal);
})
roundTotal($("#price").getValue(), $("#quantity").getValue()); //execute when DynaForm loads
Where you have fields with the IDs "price", "quantity" and "total".

If you want to round inside a grid, then delete your formula and use JavaScript like this:
Code: Select all
function roundToFixed(_float, _digits){
  var rounder = Math.pow(10, _digits);
  return (Math.round(_float * rounder) / rounder).toFixed(_digits);
}

function roundTotalInGrid(row, price, quantity) {
  var total = '';
  if (price != '' && quantity != '') {
    var total = price * quantity;
    total = roundToFixed(total, 2)
  }
  //where the "total" field is the 4th field in the grid:
  $("#orderList").setValue(total, row, 4);
}

//executed when any field changes in the DynaForm
$("form").setOnchange(function(fieldId, newVal, oldVal) {
   //check whether the changed field is in the grid:
   //replace "orderList" with the name of the grid and "price" and "quantity" with the ID of grid fields
   var aMatch = fieldId.match(/^\[orderList\]\[(\d+)\]\[(price|quantity)\]$/);
   if (aMatch != null) {
     var rowNo = aMatch[1];
     if (aMatch[2] == "price") {
         var price = newVal;
         //where the "quantity" field is the 3rd grid field:
         var quantity =  $("#orderList").getValue(rowNo, 3);
     }
     else {
         //where the "price" field is the 2rd grid field:
         var price =  $("#orderList").getValue(rowNo, 2);
         var quantity = newVal;
    }
    roundTotalInGrid(rowNo, price, quantity);
})
You will need to replace "orderList", "price", "quantity" with the IDs of the fields in your DynaForm and the numbers of the grid column in grid.getValue().
User avatar
By amosbatto
#795960
O
springhigh wrote:There were a few variables above not wrapped in quotes. i.e. $("#quantity)
But changed those and all working well
Oops. Sorry about that. I wrote that code off the top of my head. I fixed the code in my previous post.
By fuian84
#828382
Hi Amos,

I copied your code for the grid and getting an error Uncaught SyntaxError: Unexpected token ')'
My Javaskills are to low to solve the error. Would you be so kind and help me?

Found it with try and Error: has to end with }})

BR and many thanks
Florian

To convert MBOX to PST, start by downloading and i[…]

My Assignment Services stands out as one of the be[…]

Erectile Dysfunction, commonly known as impotence,[…]

Want to create your own meme coin?

In the world of cryptocurrencies, a unique and exc[…]