Related Items Sum
Posted by TWendt - Dec 15 ’16 at 07:08
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
-
Dmitry KozlovAdminMember for: 10 years
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:
var total = 0; var rows = fd.relatedItems(0).data('ctx').ListData.Row; rows.forEach(function(item) { total += parseFloat(item.FieldName) }); alert(total);
-
TWendtMemberMember for: 9 years 13 days
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
-
Dmitry KozlovAdminMember for: 10 years
[#2]: 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)
-
TWendtMemberMember for: 9 years 13 days
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
-
Dmitry KozlovAdminMember for: 10 years
[#4]: Hi,
Try to replace this line:
total += parseFloat(item.FieldName);
with:
total += parseFloat(item[FieldName + '.']);
To assign the result to a field, use the code:
fd.field('FieldName').value(total);
-
TWendtMemberMember for: 9 years 13 days
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
-
TWendtMemberMember for: 9 years 13 days
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
-
Dmitry KozlovAdminMember for: 10 years
[#7]: 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:
total += parseFloat(item.Kosten + ',')
with this:
total += parseFloat(item['Kosten.');
-
TWendtMemberMember for: 9 years 13 days
Many thanks, Dmitry,
it works.
Best wishes
Tom
-
GerardoFloresMemberMember for: 5 years 6 months 8 days
Hi Dimitry,
I was using this code and works for me very well (i have 6 relateditems, in this case im using the 5th)
function SumaVueloRelatedItems() {
var _total = 0;
var rows = fd.relatedItems(5).data('ctx').ListData.Row;
rows.forEach(function(item) {
_total += parseFloat(item.Total.replace(/,/g,""))
});
fd.field('Flight_Info_Sum').control().value(_total);
};But now i was trying to update my 'Flight_Info_Sum' control for each row that i add/modify in real-time but without success, What do you recommend me?
Thanks in advance