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

Question

Question

Forms to calculate remaining totals in a table

asked on February 18, 2020

Hi Guys!

Sure this can be done but struggling to make it work and wondered if someone had already implemented something similar.

I have a coding table which the client specifies a total amount and in the table they would like when they add a new row to split the total amount over the coding rows.

I had this working but if they overwrite an amount and then click to add a new row then it wipes their input and inserts the split amount. Is there a way to not overwrite this amount? Or a calculation where the new row checks how much is already split and just inserts the remaining amount?

something like:

Where amount is 1000, first line is 500. They would click to add a new row and it would auto to 500. When they overwrite this to 400 and then click add, the new row would be set to 100.

 

Any help would be much appreciated!

Thanks

Tina

 

1 0

Answer

SELECTED ANSWER
replied on February 18, 2020 Show version history

Javascript:

$(function() {
	$( ".lineItems .cf-table-add-row" ).click( function() {
		let remainingVal = $( ".totalRemaining [vo]" ).val(),
			lastTotal = $( ".lineItems .lineTotal [vo]:last" );
			
		if ( remainingVal ) {
			lastTotal.val( remainingVal );
		} else {
			lastTotal.val( $( ".total [vo]" ) );
		}
		
		lastTotal.trigger( "change" );
	});
});

Make a hidden number field (outside of the table) with the following formula:

=total - SUM(lineItems.lineTotal)

With the class of "totalRemaining".

Class of table in my example is "lineItems", the class of my table total field is "lineTotal" and the non-table total field is just "total", with the variables of those three being named the same (adjust the code to match yours).

2 0
replied on February 19, 2020

Thanks so much Lindsay :)

1 0
replied on February 19, 2020

Not a problem.

0 0

Replies

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

Sign in to reply to this post.