Related Items Sum

rss

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 Kozlov
    Dmitry Kozlov
    Admin
    Member for: 6 years 2 months
    #1 by Dmitry Kozlov Dec 18 ’16 at 11:41

    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);
    
  • TWendt
    Member
    Member for: 5 years 2 months 13 days
    #2 by TWendt Jan 2 ’17 at 05:19

    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 Kozlov
    Dmitry Kozlov
    Admin
    Member for: 6 years 2 months
    #3 by Dmitry Kozlov Jan 2 ’17 at 11:16

    [#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)

  • TWendt
    Member
    Member for: 5 years 2 months 13 days
    #4 by TWendt Jan 3 ’17 at 02:48

    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 Kozlov
    Dmitry Kozlov
    Admin
    Member for: 6 years 2 months
    #5 by Dmitry Kozlov Jan 4 ’17 at 03:04

    [#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);
  • TWendt
    Member
    Member for: 5 years 2 months 13 days
    #6 by TWendt Jan 4 ’17 at 04:17

    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
    Member
    Member for: 5 years 2 months 13 days
    #7 by TWendt Jan 4 ’17 at 08:17

    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 Kozlov
    Dmitry Kozlov
    Admin
    Member for: 6 years 2 months
    #8 by Dmitry Kozlov Jan 5 ’17 at 02:26

    [#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.');
  • TWendt
    Member
    Member for: 5 years 2 months 13 days
    #9 by TWendt Jan 10 ’17 at 09:43

    Many thanks, Dmitry,

    it works.

    Best wishes

    Tom

  • GerardoFlores
    Member
    Member for: 1 year 8 months 4 days
    #10 by GerardoFlores Mar 22 ’18 at 06:42

    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

Displaying 1 to 10 of 10 messages