移除和 RemoveIf Power Apps 中的函式Remove and RemoveIf functions in Power Apps

移除資料來源中的記錄Removes records from a data source.

描述Description

Remove 函式Remove function

使用 Remove 函式來移除資料來源中的一或多筆特定記錄。Use the Remove function to remove a specific record or records from a data source.

集合中,整筆記錄必須相符。For collections, the entire record must match. 您可以使用 All 引數來移除記錄的所有複本;否則系統只會移除一個記錄複本。You can use the All argument to remove all copies of a record; otherwise, only one copy of the record is removed.

RemoveIf 函式RemoveIf function

使用 RemoveIf 函式,根據一個或一組條件來移除一或多筆記錄。Use the RemoveIf function to remove a record or records based on a condition or a set of conditions. 每個條件可以是任何會產生 truefalse 的公式,而且可以依名稱來參考資料來源的資料行Each condition can be any formula that results in a true or false and can reference columns of the data source by name. 系統會針對每一筆記錄個別評估每個條件,如果所有條件都評估為 true,便會移除該記錄。Each condition is evaluated individually for each record, and the record is removed if all conditions evaluate to true.

RemoveRemoveIf 都會以資料表形式來傳回已修改的資料來源。Remove and RemoveIf return the modified data source as a table. 您只能在行為公式中使用這兩個函式。You can use both functions only in behavior formulas.

您也可以使用 Clear 函式來移除資料來源中的所有記錄。You can also use the Clear function to remove all of the records in a data source.

委派Delegation

搭配資料來源使用時,無法委派這些函數。When used with a data source, these functions can't be delegated. 將僅擷取資料來源的第一個部分,然後再套用函數。Only the first portion of the data source will be retrieved and then the function applied. 結果不一定代表完整的全貌。The result may not represent the complete story. 製作期間可能會出現警告,提醒您有此限制並建議盡可能切換至可委派的替代函數。A warning may appear at authoring time to remind you of this limitation and to suggest switching to delegable alternatives where possible. 如需詳細資訊,請參閱委派概觀For more information, see the delegation overview.

語法Syntax

Remove( DataSource, Record1 [, Record2, ... ] [, All ] )Remove( DataSource, Record1 [, Record2, ... ] [, All ] )

  • DataSource – 必要項目。DataSource – Required. 包含您要移除之一或多筆記錄的資料來源。The data source that contains the record or records that you want to remove.
  • Record(s) – 必要項目。Record(s) – Required. 要移除的一或多筆記錄。The record or records to remove.
  • All – 選擇性項目。All – Optional. 在集合中,相同的記錄可能出現多次。In a collection, the same record may appear more than once. 您可以新增 All 引數來移除記錄的所有複本。You can add the All argument to remove all copies of the record.

Remove( DataSource, Table [, All ] )Remove( DataSource, Table [, All ] )

  • DataSource – 必要項目。DataSource – Required. 包含您要移除之記錄的資料來源。The data source that contains the records that you want to remove.
  • Table – 必要項目。Table – Required. 要移除之記錄的資料表。A table of records to remove.
  • All – 選擇性項目。All – Optional. 在集合中,相同的記錄可能出現多次。In a collection, the same record may appear more than once. 您可以新增 All 引數來移除記錄的所有複本。You can add the All argument to remove all copies of the record.

RemoveIf( DataSource, Condition [, ... ] )RemoveIf( DataSource, Condition [, ... ] )

  • DataSource – 必要項目。DataSource – Required. 包含您要移除之一或多筆記錄的資料來源。The data source that contains the record or records that you want to remove.
  • Condition(s) – 必要項目。Condition(s) – Required. 對於要移除的一或多筆記錄會評估為 true 的公式。A formula that evaluates to true for the record or records to remove. 您可以在公式中使用 DataSource 的資料行名稱。You can use column names from the DataSource in the formula. 如果您指定多個條件,這些條件全都必須評估為 true 才能移除該一或多筆記錄。If you specify multiple Conditions, all must evaluate to true for the record or records to be removed.

範例-單一公式Examples - single formulas

在這些範例中,您會對名為 IceCream 且開頭為此資料表資料的一或多筆資料來源記錄進行移除︰In these examples, you'll remove a record or records in a data source that's named IceCream and that starts with the data in this table:

建立包含範例記錄的集合Create a collection with sample records

若要使用此資料建立集合:To create a collection with this data:

  1. 插入按鈕控制項。Insert a Button control.

  2. 將按鈕控制項的OnSelect屬性設定為以下公式:Set button control's OnSelect property to the below formula:

    ClearCollect( IceCream,
                  { ID: 1, Flavor: "Chocolate",  Quantity: 100 },
                  { ID: 2, Flavor: "Vanilla",    Quantity: 200 },
                  { ID: 3, Flavor: "Strawberry", Quantity: 300 }
    )
    
  3. 按住 ALT 鍵時,選取 [] 按鈕:Select the button while holding down the Alt key:

使用公式從集合中移除範例記錄Remove sample records from collection using a formula

公式Formula 描述Description 結果Result
Remove( IceCream,
First( Filter( IceCream, Flavor="Chocolate" ) ) )
Remove( IceCream,
First( Filter( IceCream, Flavor="Chocolate" ) ) )
移除資料來源中的 Chocolate 記錄。Removes the Chocolate record from the data source.

IceCream 資料來源已修改。The IceCream data source has been modified.
Remove( IceCream,
First( Filter( IceCream, Flavor="Chocolate" ) ) First( Filter( IceCream, Flavor="Strawberry" ) ) )
Remove( IceCream,
First( Filter( IceCream, Flavor="Chocolate" ) ) First( Filter( IceCream, Flavor="Strawberry" ) ) )
從資料來源中移除兩筆記錄。Removes two records from the data source.

IceCream 資料來源已修改。The IceCream data source has been modified.
RemoveIf( IceCream, Quantity > 150 )RemoveIf( IceCream, Quantity > 150 ) 移除 Quantity 大於 150 的記錄。Removes records that have a Quantity that's greater than 150.

IceCream 資料來源已修改。The IceCream data source has been modified.
RemoveIf( IceCream, Quantity > 150, Left( Flavor, 1 ) = "S" )RemoveIf( IceCream, Quantity > 150, Left( Flavor, 1 ) = "S" ) 移除 Quantity 大於 150 且 Flavor 開頭為 S 的記錄。Removes records that have a Quantity that's greater than 150 and Flavor starts with an S.


IceCream 資料來源已修改。The IceCream data source has been modified.
RemoveIf( IceCream, true )RemoveIf( IceCream, true ) 從資料來源中移除所有記錄。Removes all records from the data source.

IceCream 資料來源已修改。The IceCream data source has been modified.

在此範例中,您將使用資源控制項來列出資料表中的記錄。In this example, you'll use a Gallery control to list the records in a table. 然後使用remove函式來選擇性地移除專案。And then use the Remove function to selectively remove an item.

準備範例資料Prepare for sample data

這個範例會使用範例應用程式和資料所提供 Common Data Service 中的 [連絡人] 實體。This example uses the Contacts entity in Common Data Service available with the sample apps and data. 您可以在建立環境時部署範例應用程式和資料You can deploy sample apps and data when you create an environment. 您也可以改為使用任何其他資料來源。You can also use any other data source instead.

在此範例中,您將使用圖庫以外的按鈕來移除專案。In this example, you'll remove an item by using a button that is outside the gallery.

  1. 使用手機版面配置建立新的空白畫布應用程式Create a new blank canvas app using a Phone layout.

    使用手機版面配置的空白畫布應用程式

  2. 從左窗格中選取 [插入]。Select the Insert from the left pane.

  3. 選取 [垂直圖庫]。Select Vertical gallery.
    資源控制項隨即新增至您的畫面。A Gallery control is be added to your screen.

    使用 [插入] 工具窗格加入垂直資源庫控制項

  4. 系統會提示您選取資料來源,您可以在其中從可用的資料來源中選取資料來源。You're prompted to select a data source where you can select a data source from the available data sources.
    例如,選取 [連絡人] 實體以使用範例資料For example, select the Contacts entity to use sample data:

    選取要在資源庫中顯示的 [連絡人] 實體

    資源庫會顯示此實體中的專案:The gallery shows items from this entity:

    已新增顯示連絡人實體的資源庫

  5. 從左窗格插入按鈕控制項:Insert a Button control from left pane:

    使用 [插入] 工具窗格加入按鈕控制項

  6. 將 [新增] 按鈕移至主機庫專案底下:Move the added button below the gallery items:

    移動按鈕

  7. [更新] 按鈕 text 屬性以移除記錄Update button text property to Remove record. 您也可以使用您選擇的文字:You can also use text of your choice:

    [重新命名] 按鈕

  8. 將此按鈕控制項的OnSelect屬性設定為下列公式:Set the OnSelect property for this button control to the following formula:

    Remove( Contacts, Gallery1.Selected )
    

    設定按鈕控制項的 OnSelect 屬性

    資源庫控制項會使用 [選取的屬性],讓目前選取的記錄可用。The gallery control makes the currently selected record available using Selected property. Remove function 指的是此選取的記錄來移除它。Remove function refers to this selected record to remove it.

  9. 使用右上方的 [播放] 按鈕來預覽應用程式,或在鍵盤上按F5鍵:Preview the app using the Play button on the top right, or press F5 on keyboard:

    預覽應用程式

  10. 選取要移除的記錄,例如此範例中的的記錄:Select a record to remove, such as Nancy's record in this example:

    選取記錄

  11. 選取 [移除記錄]:Select Remove record:

    連絡人的資源庫,現在沒有已移除的南筆記錄

    選取 [] 按鈕會移除選取的記錄(在此範例中為南茜的記錄)。Selecting the button removes the selected record (in this example, Nancy's record).

  12. 關閉應用程式預覽。Close the app preview.

    提示

    您也可以搭配ALT 鍵使用替代行為,而不是使用 [播放] 按鈕或F5的 [應用程式預覽]。You can also use alternate behavior with Alt key instead of using the app preview with Play button or F5.

在此範例中,您將使用放在資源庫內的圖示來移除專案。In this example, you'll remove an item by using an icon placed inside the gallery.

建立包含範例資料的集合Create a collection with sample data

如果您已經備妥範例資料,請略過此步驟,並移至圖庫內的垃圾桶圖示If you already have prepared sample data, skip this step and move to Trash can icon inside a gallery.

  1. 按鈕控制項新增至您的螢幕。Add a Button control to your screen.

  2. OnSelect 屬性設定為下列公式:Set the OnSelect property to the following formula:

    ClearCollect( SampleContacts, 
          { 'Full Name': "Yvonne McKay (sample)",      'Primary Email': "someone_a@example.com" },
          { 'Full Name': "Susanna Stubberod (sample)", 'Primary Email': "someone_b@example.com" },
          { 'Full Name': "Nancy Anderson (sample)",    'Primary Email': "someone_c@example.com" },
          { 'Full Name': "Maria Campbell (sample)",    'Primary Email': "someone_d@example.com" },
          { 'Full Name': "Robert Lyon (sample)",       'Primary Email': "someone_e@example.com" },
          { 'Full Name': "Paul Cannon (sample)",       'Primary Email': "someone_f@example.com" },
          { 'Full Name': "Rene Valdes (sample)",       'Primary Email': "someone_g@example.com" } 
    )
    
  3. 按住 ALT 鍵時,選取按鈕。Select the button while holding down the Alt key.

建立的範例集合可供您在下列範例中使用。Sample collection is created that you can use in the following example.

  1. 使用手機版面配置建立新的空白畫布應用程式Create a new blank canvas app using a Phone layout.

    使用手機版面配置的空白畫布應用程式

  2. 從左窗格中選取 [插入]。Select the Insert from the left pane.

  3. 選取 [垂直圖庫]。Select Vertical gallery.
    資源控制項隨即新增至您的畫面。A Gallery control is be added to your screen.

    使用 [插入] 工具窗格加入垂直資源庫控制項

  4. 系統會提示您選取資料來源,您可以在其中從可用的資料來源中選取資料來源。You're prompted to select a data source where you can select a data source from the available data sources.
    例如,選取 [連絡人] 實體以使用範例資料For example, select the Contacts entity to use sample data:

    選取要在資源庫中顯示的 [連絡人] 實體

    如果您已建立集合,請改為選取您的集合:If you created a collection, select your collection instead:

    範例連絡人集合

  5. 在資源庫中的最上方專案內選取控制項。Select a control within the top item in the gallery.

    若要確保下一個步驟會將專案插入圖庫的範本中,而不是在資源庫外部,請務必遵循此步驟,再移至下一個步驟。To ensure next step inserts item into gallery's template and not outside the gallery, ensure you follow this step before moving to the next step.

    選取資源庫中的最上層記錄

  6. 從左窗格中選取 [新增] 圖示Select Add icon from left pane.

    使用 [插入] 工具窗格加入圖示控制項

    注意

    [新增] 圖示會在圖庫的左側插入 + 圖示,並針對資源庫中的每個專案進行複寫。Add icon inserts a + icon on the left side of the gallery, replicated for each item in the gallery.

  7. 在最上方的專案中,將圖示移至畫面的右側。In the top item, move the icon to the right side of the screen.

    移動圖示

  8. 選取圖示的 [圖示] 屬性,並將它設定為下列公式,以將圖示影像更新為垃圾桶圖示:Select the Icon property for icon and set it to the following formula to update the icon image as trash icon:

    Icon.Trash
    

    注意

    圖示。The Icon. 只有當您要主動編輯公式時,才會顯示前置詞。prefix is only shown when you're actively editing the formula.

    將圖示變更為垃圾桶圖示

  9. OnSelect 屬性設定為下列公式:Set the OnSelect property to the following formula:

    Remove( [@Contacts], ThisItem )
    

    注意

    您必須使用全域去除混淆運算子 [@ ... ] 在此範例中,包含使用 [連絡人] 實體的範例資料,以避免與一對多關聯性發生衝突。You must use global disambiguation operator [@...] in this example with sample data that uses the Contacts entity to avoid conflict with a One-to-Many relationship. 如果您使用資料來源(例如 SharePoint 清單或 SQL Server 資料表),則不需要使用全域 disambgulation 運算子If you use data sources such as a SharePoint list or a SQL Server table, using global disambgulation operator is not required.

    垃圾桶圖示的 OnSelect

  10. 使用右上方的 [播放] 按鈕來預覽應用程式,或在鍵盤上按F5鍵。Preview the app using the Play button on the top right, or press F5 on keyboard.

  11. 選取記錄旁的垃圾桶圖示,例如, Maria的:Select the trash icon next to a record, for example Maria's:

    已移除其中一個連絡人的圖庫

    記錄已刪除:The record is deleted:

    已刪除記錄

  12. 關閉應用程式預覽。Close the app preview.