How can i display 'add Row' in an Array only with a set condition?

default discord avatar
a16erBlech
6 months ago
4

Lets say i have an array field and i want to display the 'add row' only when i check a checkbox or use a Select-field. Is this possible?




`
const Example: CollectionConfig = {
    slug: 'example',
    fields: [
        {
            name: 'row',
            type: 'array',
            labels: {
                plural: 'rows',
                singular: 'row',
            },
            fields: [
                {
                    name: 'headline',
                    label: 'Headline',
                    type: 'text'
                },
                {
                    name: 'condition',
                    label: 'Add another one',
                    type: 'select',
                    defaultValue: 'no',
                    options: [
                        {
                            label: 'Yes',
                            value: 'yes'
                        },
                        {
                            label: 'No',
                            value: 'no'
                        }
                    ]
                }

            ]

        }
    ]
}

`



How can i display 'add Row' in an Array only with a set condition?

  • discord user avatar
    tylandavis
    Payload Team
    6 months ago

    hey @a16erBlech , it might help to explain your pattern a bit more here. In general, this sort of functionality could easily run into issues if you had rows with contradicting values for the

    condition

    field.



    Ideally any conditions applied to the array should come from the array config itself, not one of its rows, but you may have a use case where something like that would make more sense.

  • default discord avatar
    a16erBlech
    6 months ago

    hi @tylandavis thank you for your answer! For the bigger picture, i need to create a survey-app where you have different 'decision' paths. The survey consists out of multiple/single choice questions but also different kind of components - that i dont know yet. Each Question is One Page and could be a Question or just content. With every answer you could jump to a different question, so its likely possible if you choose answer x that you jump over 2 questions or just the next page with content. So i want to create something where you can set a condition for a relation to the next Question. So i could say "if the answer to question 1 is 2 then jump to 2" but also "if the answer is bigger/smaller than 42 then jump to ..'" and so on. I also want to make it possible that you can combine it with And & Or to be flexible for every case and for that i thought about using the array - cause often times its just 1 condition for the nest Question and i would only need more if i want to combine it, so only when i select and/or i could set another one.

    Bildschirmfoto_2023-05-15_um_11.44.34.png
Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.