Page 1 of 3

Related Items Sum

Posted: 15 Dec 2016
by TWendt
Hi,

i created a related item (List) where i add the savings of a project. Now i need the sum of the savings. Is it possible to solve it with the forms designer?

Best wishes

Tom

Re: Related Items Sum

Posted: 18 Dec 2016
by Dmitry Kozlov
Hi Tom,

You can either add 'total' to the list view and then pick this view in the related items control or calculate total with JavaScript:

Code: Select all

var total = 0;
var rows = fd.relatedItems(0).data('ctx').ListData.Row;
rows.forEach(function(item) {
    total += parseFloat(item.FieldName)
});
alert(total)
;

Re: Related Items Sum

Posted: 02 Jan 2017
by TWendt
Hi Dmitry,

in the Quick Edit mode of the Related Item the sum it will not displayed. And the java script works not for me.

Can you give me more information about the script?

var total = 0;
var rows = fd.relatedItems(0).data('ctx').ListData.Row; relateditems is the css class? ctx is the name of the list?
rows.forEach(function(item) {
total += parseFloat(item.FieldName) Fieldname ist the name of the field from my project costs ?
});
alert(total);

Best wishes

Tom

Re: Related Items Sum

Posted: 02 Jan 2017
by Dmitry Kozlov
Hi Tom,

Here you just need to set the right zero-based index of the Related Items control in the form:

var rows = fd.relatedItems(0).data('ctx').ListData.Row;

Leave other code without changes.

Here you should set the right Internal Name of the field from the related list that you need to sum:

total += parseFloat(item.FieldName)

Re: Related Items Sum

Posted: 03 Jan 2017
by TWendt
Hi Dmitry,

many thanks for the information. Now the function is okay, but the output of my currency is not okay. For example:

1000€+2000€+3000€ = 6

Is it possible to write the result into a field of the list?

Best wishes

Tom

Re: Related Items Sum

Posted: 04 Jan 2017
by Dmitry Kozlov
Hi,

Try to replace this line:

Code: Select all

total += parseFloat(item.FieldName);
with:

Code: Select all

total += parseFloat(item[FieldName + '.']);
To assign the result to a field, use the code:

Code: Select all

fd.field('FieldName').value(total);

Re: Related Items Sum

Posted: 04 Jan 2017
by TWendt
Hi Dmitry,

thanks for the information. If i use

var total = 0;
var rows = fd.relatedItems(0).data('ctx').ListData.Row;
rows.forEach(function(item) {
total += parseFloat(item.Kosten)
});
fd.field('Gesamt').value(total);

The field "Gesamt" is filled.

If i use

var total = 0;
var rows = fd.relatedItems(0).data('ctx').ListData.Row;
rows.forEach(function(item) {
total += parseFloat(item[Kosten + '.']);
});
fd.field('Gesamt').value(total);

the field "Gesamt" is not filled.

What's wrong?

Best wishes

Tom

Re: Related Items Sum

Posted: 04 Jan 2017
by TWendt
Hi Dmitry,

i found the error.

var total = 0;
var rows = fd.relatedItems(0).data('ctx').ListData.Row;
rows.forEach(function(item) {
total += parseFloat(item.Kosten + ',')
});
fd.field('Gesamt').value(total);

But one question, the field "Gesamt" is filled with one number

For example: in the field "Kosten" i typed in 1000, in the field "Gesamt" i see only the number 1. I think this is a problem from javascript.

What must i do to display the number in the correct format?

Best wishes

Tom

Re: Related Items Sum

Posted: 05 Jan 2017
by Dmitry Kozlov
item.Kosten contains a formatted value e.g. '1 000 $'. There should be another field, most likely it's item['Kosten.'] which contains a number. Try to replace this line:

Code: Select all

total += parseFloat(item.Kosten + ',')
with this:

Code: Select all

total += parseFloat(item['Kosten.');

Re: Related Items Sum

Posted: 10 Jan 2017
by TWendt
Many thanks, Dmitry,

it works.

Best wishes

Tom