Column Sum in a Grid

Hi,

Can someone please provide a small & clear example of code snippet on how to find total for column values in a grid ?
I am new to Appian and finding difficult in understanding the related blogs & docs.

Usecase: I have two columns. Rows appear upon clicking Add Dynamic link. last row of the grid should have Total/Sum of respective columns row data

  Discussion posts and replies are publicly visible

  • 0
    Certified Lead Developer

    Do you have any example code for what you've tried so far?

  • Thank you Mike for the response. Here is my code 

    a!localVariables(
      local!questionID: {"230108"},
      local!gridQuestionsList:rule!GPO_quesDetailsInput(questionInputID: local!questionID),
      local!program:"test",
    
      
      a!gridLayout(
        headerCells: {
             a!forEach(
              items: local!gridQuestionsList.questionID,
              expression: 
                a!localVariables(
                  local!list: rule!GPO_questionDetails(questionID: fv!item),
                  a!gridLayoutHeaderCell(label: local!list.label)
                )),
            if(not(rule!APN_isTrue(ri!isReadOnly)),a!gridLayoutHeaderCell(label: ""),{})
         },
        rows: a!forEach(
          items: local!questionID,
          expression: a!gridRowLayout(
            contents: {
              if(
                rule!APN_arrayLength(local!questionID) >= fv!index,
              a!dropdownField(
                labelPosition: "COLLAPSED",
                placeholder: "Please select one",
                choiceLabels: {"Consumer Credit","Consumer Debit","Prepaid","Ecommerece"},
                choiceValues: {"Consumer Credit","Consumer Debit","Prepaid","Ecommerece"},
                value:"",
                saveInto: local!program,
                required: not(rule!APN_isTrue(ri!isReadOnly)),
                disabled: rule!APN_isTrue(ri!isReadOnly)
              ),
              a!richTextDisplayField(
                label:"Total",
                labelPosition: "COLLAPSED",
                value:{
                  a!richTextItem(
                    text: "Total:",
                    style: "STRONG"
                  )
                }
              )
              ),
              a!textField(
                labelPosition: "COLLAPSED",
                placeholder: "MM/YYYY",
                value: "",
                saveInto: local!program,
                required: not(rule!APN_isTrue(ri!isReadOnly)),
                readOnly: rule!APN_isTrue(ri!isReadOnly),
     
              ),
              a!floatingPointField(
                value: "",
                saveInto: local!program
              ),
             a!imageField(
                label: "delete " & fv!index,
                images: a!documentImage(
                  document: a!iconIndicator("REMOVE"),
                  altText: "Remove Row",
                  link: a!dynamicLink(
                    value: fv!index,
                    saveInto: {
                      a!save(local!program, remove(local!program, save!value))
                    }
                  )
                ),
                size: "ICON"
              )
            }
          )  
        ),
        addRowlink: if(
          not(rule!APN_isTrue(ri!isReadOnly)),
          a!dynamicLink(
            label: "Add",
            value: (
              startDate: today() + 1
            ),
            saveInto: {
              a!save(local!program, append(local!program, save!value))
            }
          ),
          {}
        ),
        rowHeader: 1
      )
    )
    


    I have tried this. But not able see Total label at all.
    Also not sure how can I sum up values.

  • 0
    Certified Lead Developer
    in reply to mounikam0001

    Can you clarify - are you intending to make a separate column for each row of data in local!gridQuestionsList?  That seems counter intuitive to the way an editable grid would normally work.

  • No Mike. I will have n questions (depends on user selections). Each question will be treated as a column. First column will be a dropdown selection. For the selected option, values will be entered for each question (i.e under each column) in that row.  Total will be the last row which is sum of all values of the question (i.e a column).
    I think it requires some code change for rows and columns but I am more concerned about calculating totals for each column. 

  • 0
    Certified Lead Developer
    in reply to mounikam0001

    In an editable grid luckily it's pretty easy to simply declare an extra row below the dynamically-generated rows, containing the same number of columns as the above rows (please note also that every row will need the same number of columns, i'm unclear if your use case fits this).  Then in that final row you simply make all cells read-only values (either a text field with readOnly set to true, or rich text display fields), and declare your calculated column totals.  You can either do this in the value declaration itself, or in a local variable that's calculated in advance (the latter would make it easier to persist the calculated totals to a different variable for the purposes of saving, if you need this).

  • Hi Mike,

    how can I insert a row with the total of my columns at the end of read-only grid?

    thanks

  • 0
    Certified Lead Developer
    in reply to matteos7659
    row with the total of my columns at the end of read-only grid

    That's a much harder question, as the original question here involved an editable grid.  Generally it might be easiest to take the above approach (using an editable grid still) and just making all fields read-only.

  • 0
    Certified Lead Developer

    You can have the textField just below the grid to display the total, it would be easier.