Skip to main content
May 1, 2025
Solved

Cards as Buttons Pattern: Add / Remove from array

  • May 1, 2025
  • 11 replies
  • 0 views

I'm following the cards as a button layout.  I have a series of "buttons" that are generated dynamically.  

What I'm trying to achieve is that my component tracks which buttons are currently selected.

There is a rule input which is a list of integers of the selected buttons. I would like when the button is clicked for the id of the button to be added or removed from the list and the button card will change colors from selected to not selected and back.

Currently the coloring works when setting the selected values by default but I cannot get the values to update when clicking on the "button".

a!cardLayout(
                                    contents: {
                                      a!richTextDisplayField(
                                        value: { a!richTextItem(text: local!itemNum) },
                                        align: "CENTER"
                                      )
                                    },
                                    link: {
                                      a!dynamicLink(
                                        value: local!selectedPeriods,
                                        saveInto: a!save(ri!selectedPeriods,
                                          if(
                                            contains(ri!selectedPeriods, local!itemNum),
                                            remove(
                                              ri!selectedPeriods,
                                              whereContains(ri!selectedPeriods, local!itemNum)
                                            ),
                                            append(ri!selectedPeriods, local!itemNum)
                                          )
                                          )
                                      )
                                    },
                                    height: "AUTO",
                                    style: if(
                                      contains(ri!selectedPeriods, local!itemNum),
                                      "ACCENT",
                                      "STANDARD"
                                    ),
                                    shape: "ROUNDED",
                                    padding: "NONE",
                                    marginAbove: "NONE",
                                    marginBelow: "EVEN_LESS",
                                    showBorder: false,
                                    borderColor: "ACCENT",
                                    showShadow: true
                                  )

    Best answer by mikes0011

    the "link:" parameter expects a link, not an array of link.

    Fixing this issue seems to fix the issue I assume you're having, at least when I try it.

    11 replies

    stefanhelzle0001
    May 1, 2025

    In line 10, you are still using the local variable. Could that be the reason?

    May 1, 2025

    I have tried both ways, with local! and ri!.  Neither work to update the array when clicking on the card button.

    stefanhelzle0001
    May 1, 2025

    It feels like this is not all the code you have.

    What value does that local!itemNum have?

    mikes0011
    mikes0011Answer
    Brainy
    May 1, 2025

    the "link:" parameter expects a link, not an array of link.

    Fixing this issue seems to fix the issue I assume you're having, at least when I try it.

    May 1, 2025

    Always something so simple.  That was it.  Now I just need to update the logic for deselecting as it's just removing the first item in the list not the one I'm actually clicking on.  Thanks!

    mikes0011
    Brainy
    May 1, 2025

    Also your WhereContains is backwards - I forget this all the time, too.