Hide Button for Specified Entity, Specified Form, and Specified Subgrid

Hide Button for Specified Entity, Specified Form, and Specified Subgrid

Scenario

In our scenario, we have two subgrids of the same entity on a form. We aim to conditionally display a custom button for each subgrid based on certain criteria, such as the subgrid's name, the entity, and the form where it's located.

Solution: hideSubgridButtonWithSubgridName Function

The core of our solution is the hideSubgridButtonWithSubgridName JavaScript function. This function checks the context of the subgrid and decides whether to display the button.


// ***************************************************************
// hideSubgridButtonWithSubgridName - Dynamically controls the visibility of ribbon buttons
// in a subgrid based on the subgrid's name, entity, and form.
//
// Parameters:
//   primaryControl: Reference to the form context.
//   selectedControl: Reference to the selected grid control.
//   entityName: Name of the entity.
//   formName: Name of the form.
//   subgridName: Logical name of the subgrid.
//
// Returns true if the button should be hidden; false otherwise.
// ***************************************************************
function hideSubgridButtonWithSubgridName(primaryControl, selectedControl, entityName, formName, subgridName)
{
    var formContext = primaryControl;
    if (!formContext || !selectedControl) {
        return false; // Essential controls are not available.
    }

    // Entity check
    var currentEntityName = formContext.data.entity.getEntityName();
    if (currentEntityName !== entityName) {
        return false;
    }

    // Form name check
    var currentFormName = formContext.ui.formSelector.getCurrentItem().getLabel();
    if (currentFormName !== formName) {
        return false;
    }

    // Subgrid name check
    var currentSubgridName = selectedControl.getName();
    if (currentSubgridName !== subgridName) {
        return false;
    }

    return true; // Conditions met, hide the button.
}


Implementation in Ribbon Workbench

  1. Create a Custom Command: In Ribbon Workbench, set up a custom command for the button.

  2. Define Custom Rule: Add a CustomRule that uses this JavaScript function.

  3. Configure Parameters: Ensure the ribbon button passes the necessary parameters (primaryControl, selectedControl, entityName, formName, subgridName) to the function.

No comments:

Post a Comment