隐藏

使用 Patch 函数更新库

发布:2024/10/7 17:42:39作者:管理员 来源:本站 浏览次数:21

本动手实验为您展示如何在库中使用 Patch 函数。


   登录到 Power Apps。


   从主页屏幕左侧的导航面板中选择 + 创建,然后选择空白应用,并在空白画布应用下选择创建。


   将您的应用命名为“Patch 练习”或您选择的其他适当标题,然后选择创建。


   选择插入按钮并添加按钮控件,将其 OnSelect 属性设置为以下公式:

   PowerApps Formula


ClearCollect(CityPopulations,

{City:"London", Country:"United Kingdom", Population:8615000},

{City:"Berlin", Country:"Germany", Population:3562000},

{City:"Madrid", Country:"Spain", Population:3165000},

{City:"Rome", Country:"Italy", Population:2874000},

{City:"Paris", Country:"France", Population:2273000},

{City:"Hamburg", Country:"Germany", Population:1760000},

{City:"Barcelona", Country:"Spain", Population:1602000},

{City:"Munich", Country:"Germany", Population:1494000},

{City:"Milan", Country:"Italy", Population:1344000})


将按钮的 Text 属性设置为“Collect”。 然后按住 Alt 键并选择此按钮。 (此操作会创建 CityPopulations 集合并存储数据。)


插入垂直库控件并从数据源中选择 CityPopulations。


选中库后,在右侧属性面板中将布局从空白更改为标题、副标题和正文。


同样在属性面板中,选择字段并将数据中的正文字段更新为人口。


插入文本输入控件。 从“属性”面板中,将可访问标签属性设置为“Country”,然后在树视图中,右键单击并选择重命名以将文本输入重命名为“tiCountry”。


再次,插入文本输入控件。 从“属性”面板中,将可访问标签属性设置为“文本输入”,然后在树视图中,右键单击文本输入并将其重命名为 tiCity。


重复上一步,插入第三个文本输入,将其命名为“tiPopulation”并将可访问标签属性设置为“Population”。


选择插入选项卡,添加按钮控件,将其 Text 属性设置为“Patch Country”并将 OnSelect 属性设置为以下公式:

PowerApps Formula


Patch(CityPopulations,Defaults(CityPopulations),{Country:tiCountry.Text,City:tiCity.Text,Population:Value(tiPopulation.Text)})


如下图所示对齐控件:


对齐后的 Patch 练习控件的屏幕截图。


在继续之前,请注意编辑栏的左侧有一个助手图标。 如果您对 Power Apps 不太熟悉,或者想要弄清楚公式中的代码的含义,您可以选择“助手”图标并要求它“解释此公式”。


助手注释的屏幕截图。


解释具有复制按钮,您可以在其中复制此答案的文本,然后将其作为备注添加到您的代码中。 添加双正斜杠“//”以向您的代码添加注释,如下所示:

Power


   //This expression is used to patch a new record to the 'CityPopulations' data source. It takes the default values from the data source, and updates the 'Country', 'City', and 'Population' fields with the values entered in the 'tiCountry', 'tiCity', and 'tiPopulation' controls respectively.


   现在,让我们向库中添加更多国家/地区。 将应用置于预览模式下。


   在 Country 文本输入字段中,输入“USA”。 在 City 文本输入字段中,输入“Orlando”。 在 Population 文本输入字段中,输入“280832”。


   现在按 Patch Country 按钮并向下滚动到库的底部;您应该会看到刚刚使用按钮的 OnSelect 属性的 Patch 函数公式添加的新记录。


恭喜,您已使用 Patch 函数向库中添加了新记录!