Swap two rows in readable grid

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

  Discussion posts and replies are publicly visible

  • 0
    Certified Associate Developer

    Hello  ,

    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, "") }
                )
              }
            )
          )
        }
      )
    }

  • 0
    Certified Associate Developer

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

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

    I have readable grid
  • 0
    Certified Associate Developer
    in reply to Harshit Bumb (Appyzie)

    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.

  • 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
        }
      )
    )

  • Here is a much more optimized version of what you are trying to do for the editable grid. 

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

  • 0
    Certified Lead Developer

    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
    )

  • 0
    Certified Lead Developer
    in reply to Stefan Helzle

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

  • 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