Adventures with Power Platform: Toggle Tabs in a Model-Driven App with JavaScript

I recently had a request to demonstrate how to toggle tab visibility based on the value of an entity property, Item, in a Model-Driven App.

To complete the ask I used JavaScript and wired it up to the Model-Driven App form events, specifically On Load and On Change events.

Figure 1 – Preview of Model-Driven App with toggling tabs

Try it in your own environment by importing the solution located at https://github.com/mattruma/Model-Driven-App-Sample-1-Show-And-Hide-Tabs/blob/main/ModelDrivenAppSample1ShowHideTabsSolution_1_0_0_1.zip.

The solution contains the following components:

  • Model-Driven App Sample 1 – Item – Table that includes Name (primary unique name column) and Tab (internal Choice) properties.
  • Model-Driven App Sample 1 – Model-Driven App – The Model-Driven App.
  • Model-Driven App Sample 1 – Model-Driven App – The Site Map for the Model-Driven app.
  • Model-Driven App Sample 1 – Item Events – Web Resource for the JavaScript file that contains the code to toggle tab visibility.

Figure 2 – Solution objects

The Tabs column data type was a Choice, and Internal Choice. The values allowed for Tabs will be used in the JavaScript file.

Figure 3 – Internal choice options for Tabs field

I modified the Information form to display the Tab field and added three tabs.

Figure 4 – Information form

I created a JavaScript file, called Model-Driven App Sample 1 – Item Events.js, that contained the code to toggle tab visibility.

I uploaded the JavaScript file as a Web Resource to the Solution so it would be available to my forms.

I wired up the formOnLoad in the JavaScript file to the On Load Event of the Information form for the Item entity.

  1. Click the Information node, then click the Events tab then click Add Event Handler.
  2. Set Library to the JavaScript file uploaded to the Solution.
  3. Set Function to ModelDrivenAppSample1ItemEvents.formOnLoad.
  4. Set Pass execution context as first parameter as Checked.
  5. Click Done.
  6. Click Save and publish.

Figure 5 – Wiring up On Load Event

Next, I wired up the On Change event for the Tabs field.

  1. Expand the General node, do this twice, click on the Tabs node.
  2. Click the Events tab then click Add Event Handler.
  3. Set Library to the JavaScript file uploaded to the Solution.
  4. Set Function to ModelDrivenAppSample1ItemEvents.attributeOnChange.
  5. Set Pass execution context as first parameter as Checked.
  6. Click Done.
  7. Click Save and publish.

Figure 6 – Wiring up On Change Event

Click Back.

Click Publish and then click Play to see the app in action.

There you go! Wish there was a no-code approach to doing this, but thankfully the low-code approach is not that burdensome.

Leave a Reply

Your email address will not be published. Required fields are marked *