Select only one choice in grid selection

Hello,

I'd like to be able to select only one choice in a grid selection at the same time.
Is there a way to force that?

OriginalPostID-202357

OriginalPostID-202357

  Discussion posts and replies are publicly visible

  • It is possible by adding validation at grid level to check if gridSelection.selected has more than one value or disabling submit buttons if user has selected more than two rows.
  • You can try the below approach:
    In validations of a!gridField() component, use the logic

    if(count(local!gridSelection.selected)>1, "You can select only one choice", null)

    Also in saveInto, use the logic

    {
    local!gridSelection,
    if(
    count(local!gridSelection.selected)>1,
    {},
    a!save(local!selectedEmployeeId, index(save!value, "selected", null))
    )
    }


    Where local!gridSelection is
    a!gridSelection(
    pagingInfo: a!pagingInfo(
    startIndex: 1,
    batchSize: 20,
    sort: a!sortInfo(
    field: "fieldName",
    ascending: true/false
    )
    )
    )
  • 0
    Certified Lead Developer

    I had a similar requirement and didn't want to give a validation error message.
    The intention is to automatically change the selection to the latest selected value in the grid. When User selects check all it persists the value that was selected before this operation.

    This is done as below in Grid's SaveInto

    saveInto: {
      local!gridSelection,
      a!save(
        local!gridSelection.selected,
        if(
          rule!APN_isEmpty(
            local!gridSelection.selected
          ),
          {},
          if(
            length(
              local!gridSelection.selected
            ) > 2,
            index(
              local!gridSelection.selected,
              1,
              ""
            ),
            index(
              reverse(
                local!gridSelection.selected
              ),
              1,
              ""
            )
          )
        )
      )
    }

  • Hi mokhtarc970,
    You can do by making some changes in the saveInto parameter of grid as follows:
    saveInto: {
    local!gridSelection, //you create a variable to save grid selection data
    a!save(
    local!selectedId, //you save the selected value to another local variable
    reverse(local!gridSelection.selected)[1] //you reverse the local!gridSelection.selected to get the first value as the latest value
    ),
    a!save( // set the local value as null
    local!gridSelection.selected,
    null
    ),
    a!save( // reset the value to previous variable
    local!gridSelection.selected,
    local!selectedId
    )
    }
  • 0
    Certified Lead Developer
    Hi shivamg,

    there are two things,

    1. When you check the select all option more than once the selection value will always toggle between the last and the second last option, this is because the items will be appended sequentially (which are not already present in the selection).

    To give a consistent behavior I restricted it to the previous value when selecting all i.e selection > 2.

    2. If you deselect an already selected item, the grid will fail to render so we explicitly need to set it to an uninitialized array "{}".
  • 0
    Certified Lead Developer

    I find the official appian SAIL recipe approach of throwing a validation error to be unnecessary and kinda clunky, no offense, and to be quite honest it doesn't make much sense to me that after all this time they've never engineered the paging grid component to contain a "singleSelect" parameter (or alternatively "maxSelections" for greater flexibility) where the "select all" control will be hidden when # of selections is restricted (or at least, smaller than the current page size).

    Out of necessity I've developed the following code which handles the single-select use case pretty strongly IMHO, and it has been refined quite a bit over the past 3 years or so:

    saveInto: {
      a!save(
        local!gridSelection,
        a!gridSelection(
          pagingInfo: save!value.pagingInfo,
          selected: index(
            save!value.selected,
            length(save!value.selected),
            {}
          )
        )
      )
    }

    This code does similar to what some of the prior examples here suggest, though a bit more compact, and automatically handles the use case of de-selecting the current selected item.  Note that there's currently no way to avoid the behavior where clicking the "select all" control causes the last item on the current page to be selected, as mentioned above - this is probably the single strongest reason there really needs to be an OOB control for restricting the number of selections.

  • 0
    Certified Lead Developer
    in reply to Mike Schmitt

    Hey Mike,

    Agree that there should be an out of the box option to handle it in an elegant manner.

    Also, When you say ,"Note that there's currently no way to avoid the behavior where clicking the "select all" control causes the last item on the current page to be selected, as mentioned above". Did you try the code I gave above.

    because it works for me, i.e retains the previous selected value.
    For this reason only I wont directly index the length but use it to see if I have to index the first or the last one

    see the lines -

    if(
      --If below is true it means you have clicked select all
      length(
        local!gridSelection.selected
      ) > 2,
      --logic to retain the older value
      index(
        local!gridSelection.selected,
        1,
        ""
      ),
      --Logic to retain the new value
      index(
        reverse(
          local!gridSelection.selected
        ),
        1,
        ""
      )
    )


    please let me know if this helps

  • 0
    Certified Lead Developer
    in reply to manisht209
    I Agree that there should be an out of the box option to handle it, but we should also accept this fact, Appian currently releasing pretty huge and great updates into each one of their releases. And for 17.2 they have majorly focused on Better UI designing, Decision and easy integration approach. They might have this also in their mind, and in future they may release an out of the box for this as well. For example they have automate File Upload component for multiple in a decent way in 17.2 release, in the same way they may release these features also in upcoming releases, but can't be sure.

    But i feel, if they provide everything OOTB then there won't be any challenges, difficulties and won't be anything to learn for the developer, and the designer won't have understanding about what's going on behind the screen, and however we can handle these scenarios by writing our own logic also.