botframework-sdk icon indicating copy to clipboard operation
botframework-sdk copied to clipboard

carousel card issue when using with for loop to make it dynamic but it show only last card

Open subhans3tech opened this issue 10 years ago • 8 comments

var msg = new builder.Message(session) .textFormat(builder.TextFormat.xml) .attachmentLayout(builder.AttachmentLayout.carousel);

        msg.attachments(HeroCard);
        for(var i = 0; i < jsonObj.d.results.length;i++) 
                        {
                     new builder.HeroCard(session)
                    .title(jsonObj.d.results[i].Name1)
                    .text("<b>%s</b> sot user",jsonObj.d.results[i].Name1)
                    .images([builder.CardImage.create(session,"https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/Seattlenighttimequeenanne.jpg/800px-Seattlenighttimequeenanne.jpg").tap(builder.CardAction.showImage(session,"https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/Seattlenighttimequeenanne.jpg/800px-Seattlenighttimequeenanne.jpg")),
                ])
                .buttons([

                    builder.CardAction.imBack(session, jsonObj.d.results[0].Name1, "Select")
                ]);

                        }
    builder.Prompts.choice(session, msg, "select:100|select:101|select:102");

subhans3tech avatar Sep 23 '16 10:09 subhans3tech

is it possible you're hitting the 5-card limit (on some channels) with this? Try boxing the for loop to a max of 5 and see how you fare as a troubleshooting step.

brandonh-msft avatar Sep 23 '16 22:09 brandonh-msft

i am using with limti three also but it is not working

subhans3tech avatar Sep 24 '16 16:09 subhans3tech

I think you should check this 'jsonObj.d.results.length'. check the value of this is correct or not using console.log.

rahul251993 avatar Sep 30 '16 10:09 rahul251993

i also have checked it its less than 5 but it shows last card only

subhans3tech avatar Oct 03 '16 08:10 subhans3tech

Are you setting the layout property to "carousel"?

tomlm avatar Nov 05 '16 17:11 tomlm

yes

subhans3tech avatar Nov 09 '16 08:11 subhans3tech

hey @subhans3tech you are missing an append step (that's why you only see the last card). according to the AttachmentLayout reference, attachments should be rendered as a list. not sure if it's the most elegant solution, but given a JSON, docs, with schema:

"results": [{
        "Name": [{
            "text": "...",
            "href": "..."
        }],
        "Category": [{
            "text": "..."
        }],
        "Image": [{
            "src": "...",
            "alt": "...",
            "href": "..."
        }],
        "Rating": [{
            "text": "..."
        }],
        "Address": [{
            "text": "..."
        }]
    },
...
]

then this should do the trick:

var tmpDeck = [];
docs[0].results.slice(0,3).forEach( (result) => {
    var tmpCard = [
        new builder.HeroCard(session)
            .title(result.Name[0].text)
            .subtitle(`${result.Category[0].text}, ${result.Rating[0].text}`)
            .text(result.Address[0].text)
            .images([
                builder.CardImage.create(session, result.Image[0].src)
                    .tap(builder.CardAction.showImage(session, result.Image[0].src)),
            ])
            .buttons([
                builder.CardAction.openUrl(session, result.Image[0].href, "Reviews")
            ])
    ];
    tmpDeck.push(...tmpCard);
});

var msg = new builder.Message(session)
    .attachmentLayout(builder.AttachmentLayout.carousel)
    .attachments(tmpDeck);

// Show carousel
session.send(msg);

lyzs90 avatar Nov 15 '16 16:11 lyzs90

If this did not solve your issue, please reopen.

chrimc62 avatar Nov 28 '16 01:11 chrimc62