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 metricbits
#817420
Hello,

I have a grid with 2 dates field gFrom - From and gTo - To and also gDuration - Duration (text field). I want difference between gTo-gFrom to be automatically calculated in gDuration filed in round number of days.

I have tried the following code in JavaScript and it doesnt work (form preview doesnt load)
Code: Select all
function calcDiff()
{
   var totalRows = Number_Rows_Grid("Grid1", "gFrom");
   var from, to, diff;
   for (var i = 1; i <= totalRows; i++)
   {
      if (getGridField("Grid1", i, "gFrom") == "" || 
         getGridField("Grid1", i, "gTo") == "") 
      {
         getGridField("gDuration").value = "";
         continue;
      }
      from = new Date(getGridField("Grid1", i, "gFrom").value);
      to = new Date(getGridField("Grid1", i, "gTo").value);
      diff = Math.ceil((to.getTime() - from.getTime()) / 86400000);
      getGridField("Grid1", i, "gDuration").value = diff;
   }
}
getField("Grid1").onchange = calcDiff; 
calcDiff(); //execute when the DynaForm is first displayed
By andreaadamczyk
#817449
Hello,

You could use this code instead of yours:
Code: Select all
var indexaux=1;
function Difference()
{
  	for(i=1; i<=indexaux; i++){
		var date1=new Date($('#gridVar001').getValue(i,1));
  		var date2=new Date($('#gridVar001').getValue(i,2));
  		var diff = Math.ceil((date1 - date2) / 86400000);
      	if(isNaN(diff))
        {}else{
      		$('#gridVar001').setValue(Math.abs(diff),i,3);
        }
    }
}
$("form").setOnchange(Difference);

$("#gridVar001").onAddRow(function(row, grid, index){
	indexaux = index;
});
What this code does is to calculate the difference in days between two dates and set the result in a text control, all inside the grid.

As you can see the "indexaux" variable stores the number of rows inside the grid.

I used the "setOnchange" function for all the entire Dynaform to call the "Difference()" function; this function calculates the difference between the two dates for all the rows in the grid.

I also have attached the JSON file of the Dynaform so you can import it in your own environment.

I hope this helps you to solve your problem.

If you have any doubts, please let me know.

Best regards.
Attachments
(4.33 KiB) Downloaded 268 times
By metricbits
#817547
metricbits wrote: Wed Oct 17, 2018 12:02 am Hello,

I have a grid with 2 dates field gFrom - From and gTo - To and also gDuration - Duration (text field). I want difference between gTo-gFrom to be automatically calculated in gDuration filed in round number of days.

I have tried the following code in JavaScript and it doesnt work (form preview doesnt load)
Code: Select all
function calcDiff()
{
   var totalRows = Number_Rows_Grid("Grid1", "gFrom");
   var from, to, diff;
   for (var i = 1; i <= totalRows; i++)
   {
      if (getGridField("Grid1", i, "gFrom") == "" || 
         getGridField("Grid1", i, "gTo") == "") 
      {
         getGridField("gDuration").value = "";
         continue;
      }
      from = new Date(getGridField("Grid1", i, "gFrom").value);
      to = new Date(getGridField("Grid1", i, "gTo").value);
      diff = Math.ceil((to.getTime() - from.getTime()) / 86400000);
      getGridField("Grid1", i, "gDuration").value = diff;
   }
}
getField("Grid1").onchange = calcDiff; 
calcDiff(); //execute when the DynaForm is first displayed
This worked great! Thank you very much!

Experience heightened pleasure with Cenforce 100 M[…]

Get an instant solution to move emails to MBOX for[…]

Most Demanding OST to PST Converter

The most demanding OST to PST Converter is TrijaT[…]

Betvisa clone scripts are pre-built software solut[…]