Related Items Sum

Discussions about Forms Designer for SharePoint 2013 / 2016 and Office 365.
TWendt
Posts: 109
Joined: Mon Sep 08, 2014

15 Dec 2016

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

User avatar
Dmitry Kozlov
Site Admin
Posts: 1524
Joined: Thu Jun 07, 2012

18 Dec 2016

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)
;

TWendt
Posts: 109
Joined: Mon Sep 08, 2014

02 Jan 2017

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

User avatar
Dmitry Kozlov
Site Admin
Posts: 1524
Joined: Thu Jun 07, 2012

02 Jan 2017

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)

TWendt
Posts: 109
Joined: Mon Sep 08, 2014

03 Jan 2017

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

User avatar
Dmitry Kozlov
Site Admin
Posts: 1524
Joined: Thu Jun 07, 2012

04 Jan 2017

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);

TWendt
Posts: 109
Joined: Mon Sep 08, 2014

04 Jan 2017

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

TWendt
Posts: 109
Joined: Mon Sep 08, 2014

04 Jan 2017

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

User avatar
Dmitry Kozlov
Site Admin
Posts: 1524
Joined: Thu Jun 07, 2012

05 Jan 2017

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.');

TWendt
Posts: 109
Joined: Mon Sep 08, 2014

10 Jan 2017

Many thanks, Dmitry,

it works.

Best wishes

Tom

Locked
  • Information
  • Who is online

    Users browsing this forum: No registered users and 22 guests