Adding and Removing Panels Dynamically

Description

You can add and remove panels using Javascript, without having to place [Panel Card] containers in a [Panel Navigator]. This guide demonstrates how to build a UX component that can dynamically add panels that have a designated panel number defined by the user.

A dynamically added Panel.
A dynamically added Panel.

To see how to dynamically add and remove panels follow the guide below or watch the following video:

Create a UX that Dynamically Adds and Removes Panels

  1. In the UX Builder open the UX Controls page and check the Mobile checkbox.

    Dyn
  2. Open the Panels Menu and click on [Panel Navigator] to add a Panel Navigator to the component.

    Dyn2
  3. Highlight the Panel Navigator. In the Panels menu click the [Panel Card] option and click 'Insert After'.

    Dyn3
  4. Highlight [Panel Card End:PANELCARD_1]. Click on the [Panel Card] option and 'Insert After again to add a second panel card to the Panel Navigator.

    Dyn4
  5. Highlight [Panel Card End: PANELCARD_2]. Click on the [Panel Card] option in the Panels menu and click 'Insert After. The component should now look like this:

    Dyn5
  6. Highlight PANELCARD_1. Click on [Static Text] in the 'Other Controls' menu to add a Static text control to PANELCARD_1.

    Dyn6
  7. Highlight the Static Text control. In Static Text Properties section on the right set the 'Static text property' to read 'Panel 1'

    Dyn7
  8. Highlight PANELCARD_2. Add a [Static Text] control to PANELCARD_2 and set the 'Static text' property to read 'Panel 2'.

    Dyn8
  9. Highlight PANELCARD_3. Add a third [Static Text] control and se 'Static text' to read 'Panel 3'.

    Dyn9
  10. Highlight the Panel Navigator

    Dyn10
  11. In the Panel Navigator's properties list locate the Navigator Position Indicator section, check the 'Has indicator' checkbox.

    Dyn11
  12. Open the Containers menu and click on the [Container] option.

    Dyn12
  13. From the Container Type list select 'PanelHeader' and click 'Insert After'.

    Dyn13
  14. Highlight the newly added Panel Header. Open the 'Other Controls' menu and click on [Button] to add a button control to the panel header.

    Dyn14
  15. In the button control's properties list change the 'Button text' property to read 'Add Panel'.

    Dyn15
  16. Scroll down the properties list for the 'Add Panel' button to the 'Javascript' section. Click the [...] button next to the onClick property.

    Dyn16
  17. In the Edit onClick Event dialog select 'Text mode' from the radio buttons in the Toolbar, then add the following javascript and click 'Save'.

    addPanel();
    Dyn17
  18. Open the Other Controls menu again and click [Button] to add a second button control to the Panel Header.

    Dyn18
  19. Highlight this button and in the properties list set it's 'Button text' property to read 'Remove Panel'.

    Dyn19
  20. Scroll down the properties list for the Remove Panel button. Again in the Javascript section click on the onClick property.

    Dyn20
  21. In the Edit onClick Event select the 'Text mode' radio button again and add the following javascript and click 'Save'.

    removePanel();
    Dyn21
  22. Open the Data Controls menu and click on [TextBox] to add a textbox control to the panel header. Give this textbox control the name 'panelNum'

    Dyn22
  23. Open the Javascript functions page in the Code menu.

    Dyn23
  24. Add the following Javascript to define the addPanel() and removePanel() functions:

    function addPanel() {
         var id = {dialog.object}.getValue('panelNum');
         if(id=='') {
         alert('Please type a number in the textbox before clicking the button.');
         return false;
         }
         
         //specify a unique name for the new Panel.
         var panelName = 'NEWPANEL_' + id;
         var panelTitle = 'Pane' + id;
         
         //define a new Panel Card
         var myNewPanelCard = new A5.PanelCard({
              theme : '(dialog.style)',
              body: {
                   content: {
                        type: 'html',
                        src: 'this is my new panel text for:' + panelName
                        }
                    }
                })
          
          //get a pointer to the Panel Navigator where the new Panel will be added
          
          var pNav = {dialog.object}.panelGet('PANELNAVIGATOR_1');
          
          //add the new Panel Card to the Panel Navigator
          
          pNav.addPanel({
              name: panelName,
              title: panelTitle,
              src: myNewPanelCard
          });
          
          //optionally set focus to the Panel just added.
          pNav.setActivePanel(panelName);
          }
    
    function removePanel() {
         var id = {dialog.object}.getValue('panelNum');
         if(id == '') {
             alert('Please type a number in the textbox before clicking the button');
             return false;
         }
         var panelName = 'NEWPANEL_' + id;
         var pNav = {dialog.object}.panelGet('PANELNAVIGATOR_1');
         pNav.removePanel(panelName);
         }
    Dyn24
  25. Run the component in Live Preview. When you try to add a page without entering any information into the textbox you should get an alert.

    Dyn25
  26. When a number is entered into the textbox and 'Add Panel' is clicked, then a new panel should appear.

    Dyn26
  27. Now try removing the added panel.

    Dyn27