Search code examples
jsonconditional-statementssurveysurveyjs

Sharing data between questions – cannot produce different results for second business


I'm working with the 'share data between questions' example on SurveyJS.

Instead of 'employers', I'm asking for business names and then go on to ask for the opening hours of each business.

To replicate the issue you would input 2 or more businesses. On the following page, the opening hour question is 'visible if' the first boolean question is answered true.

This works fine for the first business, however, subsequent businesses hang off the first boolean result rather than their own boolean result.

Meaning that the 'visible if' condition only works for the first business, if subsequent businesses answer no to the boolean, this does not affect how the opening hours question appears to them.

How can I make the opening hours question respond to each individual business rather than the first business?

I've included the JSON file below to hopefully explain the issue a bit better.

I believe the issue is because the 'visbleif' is targeting businesses(0).seperateopeninghours, ie due to the 0 part always looking at the first item in the collection.

Is there a way this number can change depending on what business you are looking at?

{
    "pages":[
        {
            "name":"page1",
            "elements":[
                {
                    "type":"matrixdynamic",
                    "name":"BusinessName",
                    "title":"Enter Business name(s)",
                    "valueName":"businesses",
                    "isRequired":true,
                    "columns":[
                        {
                            "name":"BusinessName",
                            "title":"Name of business",
                            "cellType":"text",
                            "isRequired":true
                        }
                    ],
                    "rowCount":1,
                    "minRowCount":1,
                    "addRowText":"Add another business"
                }
            ],
            "title":"CoMap Business Profiler"
        },
        {
            "name":"page2",
            "elements":[
                {
                    "type":"paneldynamic",
                    "name":"arrray_employer_info",
                    "title":"Your businesses",
                    "valueName":"businesses",
                    "templateElements":[
                        {
                            "type":"boolean",
                            "name":"SeparateOpenning",
                            "title":"does {panel.BusinessName} have different opening hours?"
                        },
                        {
                            "type":"matrixdropdown",
                            "name":"OpenClosed1",
                            "visibleIf":"{businesses[0].SeparateOpenning} = true",
                            "title":"Which days is {panel.BusinessName} open?",
                            "columns":[
                                {
                                    "name":"Closed",
                                    "title":"Open or Closed?",
                                    "cellType":"boolean",
                                    "labelTrue":"Closed",
                                    "labelFalse":"Open"
                                }
                            ],
                            "cellType":"boolean",
                            "rows":[
                                "Monday",
                                "Tuesday",
                                "Wednesday",
                                "Thursday",
                                "Friday",
                                "Saturday",
                                "Sunday"
                            ]
                        },
                        {
                            "type":"matrixdropdown",
                            "name":"OpeningHours1",
                            "visibleIf":"{businesses[0].SeparateOpenning} = true",
                            "startWithNewLine":false,
                            "title":"Please enter the opening hours for {panel.name}.",
                            "hideNumber":true,
                            "columns":[
                                {
                                    "name":"Opening Time",
                                    "cellType":"text",
                                    "inputType":"time"
                                },
                                {
                                    "name":"Closing Time",
                                    "cellType":"text",
                                    "inputType":"time"
                                }
                            ],
                            "cellType":"text",
                            "rows":[
                                {
                                    "value":"Monday",
                                    "visibleIf":"{businesses[0].OpenClosed1.Monday.Closed} = '[object Object]'"
                                },
                                {
                                    "value":"Tuesday",
                                    "visibleIf":"{businesses[0].OpenClosed1.Tuesday.Closed} = false"
                                },
                                {
                                    "value":"Wednesday",
                                    "visibleIf":"{businesses[0].OpenClosed1.Wednesday.Closed} = false"
                                },
                                {
                                    "value":"Thursday",
                                    "visibleIf":"{businesses[0].OpenClosed1.Thursday.Closed} = false"
                                },
                                {
                                    "value":"Friday",
                                    "visibleIf":"{businesses[0].OpenClosed1.Friday.Closed} = false"
                                },
                                {
                                    "value":"Saturday",
                                    "visibleIf":"{businesses[0].OpenClosed1.Saturday.Closed} = false"
                                },
                                {
                                    "value":"Sunday",
                                    "visibleIf":"{businesses[0].OpenClosed1.Sunday.Closed} = false"
                                }
                            ]
                        }
                    ],
                    "templateTitle":"Business name: {panel.BusinessName}",
                    "allowAddPanel":false,
                    "allowRemovePanel":false
                }
            ],
            "title":"xxx"
        }
    ]
}

Solution

  • Each panel that is created is corresponds to 1 instances of businesses - so swap

    "{businesses[0].SeparateOpenning} = true",

    for

    {panel.SeparateOpenning} = true"