Removing
Since version 1.19, this event rule is triggered before deleting items, before the deletion confirmation message is displayed (when applicable) and before the ItemRemoving event is triggered for each items being removed.
Its main purpose is to be able to customize messages displayed to the user when deleting one or more items.
This occurs when :
- the user clicks on the
Removebutton on the main UI view of a screen DeleteSelectedItemsAsyncare called by code in the main UI view of a screen
Important
The Removing event rule is not called when calling the DeleteDataAsync method.
The reason is that DeleteDataAsync allows you to delete items that are not selected. Displaying a confirmation message about deleting unselected items would not be understandable for the user.
If you want the Removing rule to be triggered, you should use SelectItemAsync or SelectAllItemsAsync and then DeleteSelectedItemsAsync.
See this article for more information on event rules execution order.
Arguments
The Arguments property provides the following options.
| Name | Type | Description |
|---|---|---|
Items |
IReadOnlyCollection<T> where T is the UI view model |
Collection of items being removed. |
Cancel |
bool |
If set to true, no item is removed from the view model datasource, no DELETE API is called and the ItemRemoving is not called on any of the items. |
DisplayConfirmationMessage |
boolean | Indicates whether the default deletion confirmation is displayed. true by default. |
ConfirmationMessageTitle |
string | Title displayed in the confirmation message dialog to display to the user when DisplayConfirmationMessage is true. |
ConfirmationMessageText |
string | Text displayed in the confirmation message dialog to display to the user when DisplayConfirmationMessage is true. |
Item versus DatasourceCurrent
Since the rule is not tied to a specific item, the Item property does not exist in this rule. However, you should use Arguments.Items to access the items being deleted.
DatasourceCurrent refers to the item at the current position in the datasource.
To know more on the difference between the Item and DatasourceCurrent properties in an UI view event rule, you can check this article.
Examples
Example 1
Customizing the default deletion confirmation message displayed when deleting an item.
// Getting the main depot if it is part of the items being deleted
DepotListUI? mainDepot = Arguments.Items.FirstOrDefault(i => i.MainDepot == true);
if (mainDepot != null)
{
// Customizing the title and the text of the confirmation message
Arguments.ConfirmationMessageTitle = "Warning";
Arguments.ConfirmationMessageText = $"One of the {Arguments.Items.Length} depots you are deleting is the main depot. Do you confirm the deletion?";
}
Example 2 Replacing the default deletion confirmation message by a custom popup in which the user must type the order number to confirm its deletion.
// Hiding the default confirmation message
Arguments.DisplayConfirmationMessage = false;
// Getting the order being deleted (we are in a mono-record UI view)
OrderUI order = Arguments.Items.First();
// Function for checking the value typed by the user
// Returns the error message to display next to the input when the value is invalid
Func<string?, string?> validateInput = (string? inputValue) => inputValue != null && inputValue.Equals(order.OrderNumber, StringComparison.OrdinalIgnoreCase)
? null // Return null when the value is valid
: "Invalid value"; // Return the error message when the value is invalid. When the value is invalid but you do not want to display an error message next to the input, return string.Empty
// Function for disabling the confirmation button
// It takes the value of the input of the message as an argument
// We call the validateInput function and pass the value of the input
// When validateInput returns null, this means the input value is valid and the button should be enabled, so we return false
// When validateInput returns a string, this means the input value is invalid and the button should be disabled, so we return true
Func<string?, bool> isButtonDisabled = (string? inputValue) => validateInput(inputValue) != null;
// Displaying a custom message where the user must type de name of the item being deleted to confirm
IMessageResponse response = await ShowMessageWithInputAsync(
MessageType.Danger,
$"Delete order {order.OrderNumber}",
$"Do you want to delete order \"{order.OrderNumber}\"?\nType \"{order.OrderNumber}\" to confirm.",
validateInput, // Function for validating the input value
new MessageButton(
1,
Resources.Core.Remove,
"Remove",
false,
isButtonDisabled), // Function for disabling the button
new MessageButton(MessageButtonType.Cancel));
// Cancelling the save when the user clicks on the "Cancel" button
if (response.ButtonCustomId != 1)
{
Arguments.Cancel = true;
}