Skip to main content
January 19, 2024
Question

Swap two rows in readable grid

  • January 19, 2024
  • 25 replies
  • 0 views

Hi, I have readable grid in which i am using arrow icon, so if I click on that arrow so that row should go up and upper row should come down. I can get all data items from current row, but how I will get data from previous row, So I can apply swapping conditiin

    25 replies

    rithanir5887
    January 19, 2024

    Hello [mention:fc49608a491a4f9f9f9d41ed960420df:e9ed411860ed4f2ba0265705b8793d05] ,

    Below code contains logic to move rows up and down. 

    {
      a!localVariables(
        local!duplicate,
        local!removed,
        local!change,
        local!data: index(
          a!queryRecordType(
            recordType: 'recordType!{9e939d67-33c2-43f5-8030-51fe874a4980}PS Company',
            filters: a!queryFilter(
              field: 'recordType!{9e939d67-33c2-43f5-8030-51fe874a4980}PS Company.fields.{19721518-b5ee-4deb-b4b2-5b0d5603d6ba}isActive',
              operator: "=",
              value: true
            ),
            pagingInfo: a!pagingInfo(startIndex: 1, batchSize: 100)
          ),
          "data",
          null
        ),
        {
          a!gridLayout(
            label: "Editable Grid",
            labelPosition: "ABOVE",
            headerCells: {
              a!gridLayoutHeaderCell(label: "Company Name"),
              a!gridLayoutHeaderCell(label: "Postcode"),
              a!gridLayoutHeaderCell(label: "City"),
              a!gridLayoutHeaderCell(label: "State "),
              a!gridLayoutHeaderCell(label: "Remove"),
              a!gridLayoutHeaderCell(label: "Move Up"),
              a!gridLayoutHeaderCell(label: "Move Down")
            },
            columnConfigs: {},
            rows: a!forEach(
              items: local!data,
              expression: a!gridRowLayout(
                contents: {
                  a!textField(
                    value: fv!item['recordType!{9e939d67-33c2-43f5-8030-51fe874a4980}PS Company.fields.{c048e290-9f07-4fe7-a919-71b572004f35}companyName'],
                    saveInto: fv!item['recordType!{9e939d67-33c2-43f5-8030-51fe874a4980}PS Company.fields.{c048e290-9f07-4fe7-a919-71b572004f35}companyName']
                  ),
                  a!textField(
                    value: fv!item['recordType!{9e939d67-33c2-43f5-8030-51fe874a4980}PS Company.fields.{07050c26-84c9-4749-8aa2-54675c403e0e}companyAddressPostCode'],
                    saveInto: fv!item['recordType!{9e939d67-33c2-43f5-8030-51fe874a4980}PS Company.fields.{07050c26-84c9-4749-8aa2-54675c403e0e}companyAddressPostCode']
                  ),
                  a!textField(
                    value: fv!item['recordType!{9e939d67-33c2-43f5-8030-51fe874a4980}PS Company.fields.{9070d09a-ffd1-485b-9ac3-159cf5bed7ed}companyAddressCity'],
                    saveInto: fv!item['recordType!{9e939d67-33c2-43f5-8030-51fe874a4980}PS Company.fields.{9070d09a-ffd1-485b-9ac3-159cf5bed7ed}companyAddressCity']
                  ),
                  a!textField(
                    value: fv!item['recordType!{9e939d67-33c2-43f5-8030-51fe874a4980}PS Company.fields.{80559642-a863-41e2-ad38-129e72546134}companyAddressState'],
                    saveInto: fv!item['recordType!{9e939d67-33c2-43f5-8030-51fe874a4980}PS Company.fields.{80559642-a863-41e2-ad38-129e72546134}companyAddressState']
                  ),
                  a!richTextDisplayField(
                    value: a!richTextIcon(
                      icon: if(
                        fv!item['recordType!{9e939d67-33c2-43f5-8030-51fe874a4980}PS Company.fields.{19721518-b5ee-4deb-b4b2-5b0d5603d6ba}isActive'],
                        "times",
                        "check"
                      ),
                      caption: "click to add or remove company",
                      link: a!dynamicLink(
                        saveInto: if(
                          fv!item['recordType!{9e939d67-33c2-43f5-8030-51fe874a4980}PS Company.fields.{19721518-b5ee-4deb-b4b2-5b0d5603d6ba}isActive'],
                          {
                            a!save(
                              fv!item['recordType!{9e939d67-33c2-43f5-8030-51fe874a4980}PS Company.fields.{19721518-b5ee-4deb-b4b2-5b0d5603d6ba}isActive'],
                              false()
                            )
                          },
                          {
                            a!save(
                              fv!item['recordType!{9e939d67-33c2-43f5-8030-51fe874a4980}PS Company.fields.{19721518-b5ee-4deb-b4b2-5b0d5603d6ba}isActive'],
                              true()
                            )
                          }
                        )
                      ),
                      color: if(
                        fv!item['recordType!{9e939d67-33c2-43f5-8030-51fe874a4980}PS Company.fields.{19721518-b5ee-4deb-b4b2-5b0d5603d6ba}isActive'],
                        "NEGATIVE",
                        "POSITIVE"
                      )
                    )
                  ),
                  a!richTextDisplayField(
                    value: a!richTextIcon(
                      showWhen: not(fv!isFirst),
                      icon: "arrow-up",
                      link: a!dynamicLink(
                        value: fv!item,
                        saveInto: {
                          local!removed,
                          a!save(
                            local!change,
                            index(local!data, fv!index - 1, null)
                          ),
                          a!save(
                            local!data,
                            insert(local!data, fv!item, fv!index - 1)
                          ),
                          a!save(local!data, remove(local!data, fv!index)),
                          a!save(
                            local!data,
                            a!update(local!data, fv!index, local!change)
                          )
                        }
                      )
                    )
                  ),
                  a!richTextDisplayField(
                    value: a!richTextIcon(
                      showWhen: not(fv!isLast),
                      icon: "arrow-down",
                      link: a!dynamicLink(
                        value: fv!item,
                        saveInto: {
                          local!removed,
                          a!save(local!data, remove(local!data, fv!index)),
                          a!save(
                            local!data,
                            insert(local!data, local!removed, fv!index + 1)
                          )
                        }
                      )
                    )
                  ),
                  
                }
              )
            )
          ),
          a!linkField(
            links: a!dynamicLink(
              label: "Add New Company",
              saveInto: {
                a!save(
                  target: local!data,
                  value: { append(local!data, "") }
                )
              }
            )
          )
        }
      )
    }

    harshitb6843
    January 19, 2024

    The OP must be using a gridField() and not a gridLayout()

    I have readable grid
    rithanir5887
    January 19, 2024

    Yes. I have worked on this logic already but for editable grid. I gave it for reference purpose just to move rows up and down.

    jayaprakashr0001
    January 19, 2024

    Hi ,

    use the below code it might help.

    What is the use case for this?

    a!localVariables(
      local!data:{
        a!map(
          name:"Test",
          city:"ABC"
        ),
        a!map(
          name:"Test1",
          city:"QWE"
        ),
        a!map(
          name:"Test2",
          city:"DFG"
        ),a!map(
          name:"Test3",
          city:"ZXC"
        ),a!map(
          name:"Test4",
          city:"YUI"
        )
      },
      local!temp,
      local!index,
      a!gridField(
        label: "Employee Data",
        data: local!data,
        columns: {
          a!gridColumn(
            label: "Name",
            value: fv!row.name
          ),
          a!gridColumn(
            label: "Name",
            value: fv!row.city
          ),
          a!gridColumn(
            label: "Name",
            value: a!richTextDisplayField(
              value: {
                a!richTextIcon(
                  icon:"arrow-up",
                  link: a!dynamicLink(
                    saveInto: {
                      a!save(local!data, remove(local!data, fv!identifier)),
                      a!save(
                        local!data,
                        insert(local!data, fv!row, fv!identifier - 1)
                      )
                    }
                  )
                )
              }
            )
          ),
          a!gridColumn(
            label: "Name",
            value: a!richTextDisplayField(
              value: {
                a!richTextIcon(
                  icon:"arrow-down",
                  link: a!dynamicLink(
                    saveInto: {
                      a!save(local!data, remove(local!data, fv!identifier)),
                      a!save(
                        local!data,
                        insert(local!data, fv!row, fv!identifier + 1)
                      )
                    }
                  )
                )
              }
            )
          )
        }
      )
    )

    harshitb6843
    January 19, 2024

    You can us these code blocks in your

    Down Arrow

    a!save(
      ri!data,
      a!update(
        ri!data,
        {
          wherecontains(fv!row, ri!data),
          wherecontains(fv!row, ri!data) + 1
        },
        {
          ri!data[wherecontains(fv!row, ri!data) + 1],
          fv!row
        }
      )
    )

    Up Arrow

    a!save(
      ri!data,
      a!update(
        ri!data,
        {
          wherecontains(fv!row, ri!data),
          wherecontains(fv!row, ri!data) - 1
        },
        {
          ri!data[wherecontains(fv!row, ri!data) - 1],
          fv!row
        }
      )
    )

    amaans4178
    January 19, 2024

    I believe instead of using this

    wherecontains(fv!row, ri!data)

    we should go with this 
    fv!identfier

    for finding the index of current item 

    harshitb6843
    January 19, 2024

    Have been using it for ages. Did not find any issue :)

    stefanhelzle0001
    January 19, 2024

    Find below my generic expression to swap two items in a list.

    if(
      and(
        not(rule!PCO_IsVoid(ri!list)),
        ri!index1 > 0,
        ri!index1 <= count(ri!list),
        ri!index2 > 0,
        ri!index2 <= count(ri!list),
        ri!index1 <> ri!index2
      ),
      updatearray(
        ri!list,
        {ri!index1, ri!index2},
        {ri!list[ri!index2], ri!list[ri!index1]}
      ),
      ri!list
    )

    harshitb6843
    January 19, 2024

    As Appian suggests, now you should replace it with a!update() 

    stefanhelzle0001
    January 19, 2024

    Thanks. Done :-)

    January 19, 2024

    HI [mention:fc49608a491a4f9f9f9d41ed960420df:e9ed411860ed4f2ba0265705b8793d05] 

    Use the below code in saveinto parameter of your dynamic link for moving up

     a!save(
                                  local!data,
                                  insert(local!data, fv!item, fv!index - 1)
                                ),
                                a!save(
                                  local!data,
                                  remove(local!data, fv!index + 1)
                                )

    Use the below code in saveinto parameter of your dynamic link for moving down

    a!save(
                                  local!data,
                                  insert(local!data, fv!item, fv!index + 2)
                                ),
                                a!save(
                                  local!data,
                                  remove(local!data, fv!index)
                                )

    January 19, 2024

    I solved the problem by own. This is the logic which I was required.

    a!gridColumn(
    label: "",
    value: a!richTextDisplayField(
    value: a!richTextIcon(
    icon: "arrow-down",
    caption: "Move down",
    link: a!dynamicLink(
    saveInto: {
    a!save(fv!currentPage.data[fv!identifier],fv!currentPage.data[fv!identifier+1]),
    a!save(fv!currentPage.data[fv!identifier+1],fv!row),
    },

    ),
    showWhen: fv!currentPage.startIndex
    )
    )
    )

    amaans4178
    January 19, 2024