You are viewing limited content. For full access, please sign in.

Discussion

Discussion

Forms Javascript - Subtotal a table's columns without causing an endless loop

posted on November 12, 2018

Not sure how to subtotal a table properly without creating an endless change loop.

Here is an example of a table where the columns are subtotaled in the last row.

The subtotal function sets the last input with the total of the above rows. Simple.

However the subtotal function needs to run, when a column's value is changed. IE:

  $('li.opExpenses').on('change', '.budget, .matchBudget, .rereq, .matchExp', function() {
      
        SubTotal();
    });
  

This means I can not run a .change() method when setting the value. Since it would go into an endless loop, changing the column, thinking there is an update, and subtotaling again.

Here is where that causes problems. When you do not run a .change() method setting a field value, the value of the field is displayed properly, but it doesn't actually contain the value yet for other functions to act on.

So I have another function that needs to run after subtotaling, I call the function, it attempt to read the subtotals, and it returns the incorrect value.

How do you properly set the value of the last field in a table with a full .change() event without creating an endless loop?

0 0
replied on November 12, 2018 Show version history

Hello,

Have you tried using the built-in Functions to handle the calculations? Between the SUM() function, and the INDEX(field,ROW()) function if you need to target a specific row, you should be able to accomplish this without the need for any custom JavaScript.

0 0
replied on November 14, 2018

I have had trouble with using those functions. I did just figure out what was going on though, it wasn't that the field values were inaccessible after all.  Javascript was actually running the code out of order.

If you write code like this

 $.getScript("master.js", function() {

console.log('hello');

});

console.log('world');

The output is: world hello

0 0
replied on November 14, 2018

The functions can be a bit tricky, but they're based on the same thing used by programs like Excel so it just takes some getting used to. The JavaScript route is perfectly fine if you got it working; I've just found that once you get them working the functions are a bit easier to implement/maintain.

As time goes on and I've gotten more familiar with everything, I've found myself moving more toward using the built-in functionality whenever possible, but I definitely started out with mostly JavaScript. Actually, when I first started with Workflow I did a lot in C# scripts until I knew had a better handle on all the different built-in activities lol.

0 0
replied on November 14, 2018

Ok, I just tried the SUM function again and it appears to be working now. There is very little you can do if any kind of conditional logic is requested though, which is 95% of the time for me, if not on the first build, immediately after they test.

We also had a problem where many submissions were lost because a function was not valid. It dropped the whole submission over one function. At least if javascript is invalid, it just fails to execute. The form can still be submitted and the data in the fields is always saved (with the one exception of the read-only field problem).

0 0
replied on November 14, 2018 Show version history

That is true. The logic of a function can get a bit messy because you need to find the right combination of IF, AND, OR functions nested within one another. I've found that the main reason a function gets marked as invalid is that a field it uses for something wasn't available. I've used IF functions to avoid issues like that, but I agree that the really complex stuff is less of a hassle with JavaScript.

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.