【问题标题】:Repeating elements with Adaptive Cards Templating使用自适应卡片模板重复元素
【发布时间】:2020-07-14 19:56:10
【问题描述】:

我正在创建一个自适应卡片,它要求我使用Designer 重复数组中的一些元素。我正在尝试为发票创建项目列表,因此我需要重复购物车中的项目....

我有一个带有这个容器的模板,其中包含需要重复的元素:

   {
            "type": "Container",
            "items": [
                {
                    "type": "Container",
                    "items": [
                        { 
                            "$data": "${items}",
                            "type": "ColumnSet",
                            "columns": [
                                {
                                    "type": "Column",
                                    "items": [
                                        {
                                            "type": "TextBlock",
                                            "wrap": true,
                                            "text": "${quantity}"
                                        }
                                    ],
                                    "width": "auto"
                                },
                                {
                                    "type": "Column",
                                    "spacing": "Medium",
                                    "items": [
                                        {
                                            "type": "TextBlock",
                                            "wrap": true,
                                            "text": "${product.name}"
                                        }
                                    ],
                                    "width": "stretch"
                                },
                                {
                                    "type": "Column",
                                    "items": [
                                        {
                                            "type": "TextBlock",
                                            "text": "${cost}",
                                            "wrap": true
                                        }
                                    ],
                                    "width": "auto"
                                },
                                {
                                    "type": "Column",
                                    "id": "chevronDown1",
                                    "spacing": "Small",
                                    "verticalContentAlignment": "Center",
                                    "items": [
                                        {
                                            "type": "Image",
                                            "selectAction": {
                                                "type": "Action.ToggleVisibility",
                                                "title": "collapse",
                                                "targetElements": [
                                                    "cardContent1",
                                                    "chevronUp1",
                                                    "chevronDown1"
                                                ]
                                            },
                                            "url": "https://adaptivecards.io/content/down.png",
                                            "width": "20px",
                                            "altText": "collapsed"
                                        }
                                    ],
                                    "width": "auto"
                                },
                                {
                                    "type": "Column",
                                    "id": "chevronUp1",
                                    "isVisible": false,
                                    "spacing": "Small",
                                    "verticalContentAlignment": "Center",
                                    "items": [
                                        {
                                            "type": "Image",
                                            "selectAction": {
                                                "type": "Action.ToggleVisibility",
                                                "title": "expand",
                                                "targetElements": [
                                                    "cardContent1",
                                                    "chevronUp1",
                                                    "chevronDown1"
                                                ]
                                            },
                                            "url": "https://adaptivecards.io/content/up.png",
                                            "width": "20px",
                                            "altText": "expanded"
                                        }
                                    ],
                                    "width": "auto"
                                }
                            ]
                        },
                        {
                            "type": "Container",
                            "id": "cardContent1",
                            "isVisible": false,
                            "items": [
                                {
                                    "type": "Container",
                                    "items": [
                                        {
                                            "type": "TextBlock",
                                            "isSubtle": true,
                                            "wrap": true,
                                            "text": "${product.description}"
                                        }
                                    ]
                                }
                            ]
                        }
                    ],
                }
            ],
        }

以及如下所示的示例数据:

{
    "items": [
        { 
            "quantity": "1", 
            "unitCost": "55", 
            "cost": "55", 
            "product": {
                "name": "Product 1", 
                "description": "Lorem ipsum dolor sit amet"
            }
        }, { 
            "quantity": "2", 
            "unitCost": "55", 
            "cost": "55", 
            "product": {
                "name": "Product 2", 
                "description": "Lorem ipsum dolor sit amet"
            }
        }
    ]
}

我关注了example here,但我无法获得相同的效果...我推测这是因为我有嵌套元素。

【问题讨论】:

    标签: adaptive-cards


    【解决方案1】:

    要完成您想要实现的目标,您需要使用动态生成的 targetElements 和 ID。

    我使用了您的模板并对其进行了修复,以便在此处提供一个工作示例: https://www.madewithcards.io/cards/toggleable-description-in-array

    下面是代码供参考:

    {
    "type": "AdaptiveCard",
    "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
    "version": "1.2",
    "body": [{
            "type": "TextBlock",
            "text": "Items:",
            "size": "Medium",
            "weight": "Bolder"
        },
        {
            "type": "Container",
            "$data": "${items}",
            "items": [{
                    "type": "ColumnSet",
                    "columns": [{
                            "type": "Column",
                            "items": [{
                                "type": "TextBlock",
                                "wrap": true,
                                "text": "${quantity}"
                            }],
                            "width": "auto"
                        },
                        {
                            "type": "Column",
                            "spacing": "Medium",
                            "items": [{
                                "type": "TextBlock",
                                "wrap": true,
                                "text": "${product.name}"
                            }],
                            "width": "stretch"
                        },
                        {
                            "type": "Column",
                            "items": [{
                                "type": "TextBlock",
                                "text": "${cost}",
                                "wrap": true
                            }],
                            "width": "auto"
                        },
                        {
                            "type": "Column",
                            "id": "chevronDown${product.name}",
                            "spacing": "Small",
                            "verticalContentAlignment": "Center",
                            "items": [{
                                "type": "Image",
                                "selectAction": {
                                    "type": "Action.ToggleVisibility",
                                    "title": "collapse",
                                    "targetElements": [
                                        "${product.name}",
                                        "chevronUp${product.name}",
                                        "chevronDown${product.name}"
                                    ]
                                },
                                "url": "https://adaptivecards.io/content/down.png",
                                "width": "20px",
                                "altText": "collapsed"
                            }],
                            "width": "auto"
                        },
                        {
                            "type": "Column",
                            "id": "chevronUp${product.name}",
                            "isVisible": false,
                            "spacing": "Small",
                            "verticalContentAlignment": "Center",
                            "items": [{
                                "type": "Image",
                                "selectAction": {
                                    "type": "Action.ToggleVisibility",
                                    "title": "expand",
                                    "targetElements": [
                                        "${product.name}",
                                        "chevronUp${product.name}",
                                        "chevronDown${product.name}"
                                    ]
                                },
                                "url": "https://adaptivecards.io/content/up.png",
                                "width": "20px",
                                "altText": "expanded"
                            }],
                            "width": "auto"
                        }
                    ]
                },
                {
                    "type": "Container",
                    "id": "${product.name}",
                    "isVisible": false,
                    "items": [{
                        "type": "Container",
                        "items": [{
                            "type": "TextBlock",
                            "isSubtle": true,
                            "wrap": true,
                            "text": "${product.description}"
                        }]
                    }]
                }
            ]
        }
    ]
    

    }

    数据未经改动,取自您的样本。

    【讨论】:

    • 谢谢@TimCadenbach。除了ID的问题,Adaptive Cards网站上的设计师(adaptivecards.io/designer)似乎是不呈现数据的设计师,我试过了你的代码,它仍然没有工作。
    • 嗯,到底什么不适合你?也许我们在谈论不同的事情。如果您查看此madewithcards.io/cards/toggleable-description-in-array,这不是您期望的工作方式吗?
    • 我在设计器上进行测试,由于某种原因它没有将我的示例数据加载到卡上......所以我只是复制并粘贴了您发送给我的示例,它也可以不从数组中加载样本日期...它显示模板的值如${product.name}...当我在我的代码中使用相同的模板时它呈现正常。
    • 在设计器上有一个“预览”模式来启用数据绑定,你试过吗?
    • ??‍♂️ 那是我的错,我现在觉得很傻。非常感谢。
    猜你喜欢
    • 2021-05-06
    • 2021-12-05
    • 2021-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-03
    • 2022-01-08
    • 2021-01-31
    相关资源
    最近更新 更多