Nov 08

Create Prefab using JQuery Plugin

This post walks you through the creation and usage of a Prefab using JQuery Plugin. This is to showcase the use of Events and Methods in a Prefab.

Let us create a simple Prefab named TreeView which is interactive. We are going to use a JQuery plugin named jsTree. jsTree is a JQuery plugin, that provides interactive trees. jsTree is easily extendable, theme-able and configurable, it supports HTML & JSON data sources and AJAX loading.

Prerequisites

Download the jsTree JQuery Plugin from https://www.jstree.com/
Unzip the file downloaded and use the files from the dist folder.

Creating the Prefab

  1. Click on Create from the Prefab tab of the Project Dashboard
  2. Enter a name (say TreeView2) and description for the Prefab.
  3. From File Explorer add the JS and CSS files from the above-downloaded dist folder. Here we have created a folder jsTree to hold the same structure as the dist folder.
  4. From Project Configurations, choose Config Prefab under Settings:
    1. In the Resources tab, choose the css and js files.
    2. In the Properties tab, add an inbound property as an object,array (type if not selectable) which should consist of parent and child node names:
    3. In the Events tab, add events to be triggered when a node is Selected, Deselected, Expanded or Collapsed. Note, by default two events are already given – Load and Destroy
    4. In the Methods tab, add the following methods:
      1. SelectNode: Method to select a node
        • Parameters: Node, Type: any
        • Return type: void
      2. Redraw: Method to redraw the whole tree
        • Return type: void
      3. selectAllNodes: Method to select all Nodes
        • Return type: void
      4. deselectAllNodes: Method to deselect all nodes
        • Return type: void
      5. deselectNode: Method to deselect a node
        • Parameters: Node, Type: any
        • Return type: void

  5. Once these properties, methods, and events are added to Prefab configuration in the Prefab Script the Method snippets will be auto-generated. You can fill with the code for the same.
  6. Here is the script for all methods :
    Declare a variable:

    Property Change event:

    Function to initialize the tree view:

    Function to redraw the tree view:

    Methods for the tree:

  7. Add a wm-container tag with the name treeViewContainer in the Markup, as follows:

  8. Publish the Prefab. Know more about publishing Prefabs from here.

Using the Prefab in Project

  1. Let us now use the above-created Prefab in an application.
  2. We are using the WaveMaker non-enterprise version and have Published the Prefab to Workspace.
  3. Create or Open an application.
  4. You can see the Prefab in the Prefab Listing.
  5. On the Main page drag and drop the TreeView2 Prefab.

Invoking Prefab Methods in Project

The prefab has exposed methods which can be triggered by the application as shown below.

  1. To call the exposed methods drag and drop four Button Widgets in the main page under the Prefab and give the captions for the buttons as SELECT ALL, SELECT NODE, DESELECT NODE and DESELECT ALL. We have named the buttons as selectAll, selectNode, deselectNode and deselectAll.
  2. Create On Click events for each of the buttons.
  3. This will create the snippet for the <button>Click event in the script tab of the main page in the application.
  4. The code for the <button>Click event will call the exposed methods respectively (check the name of the Button widget, it might be different if you have not named them as mentioned in step 1). Ensure that the Prefab name (TreeView21) matches what you have in your project:

Invoking Prefab Events in Project

The events are also exposed on the events tab and you can create actions for each of them.

Here we have created Notification Action to be displayed for each of the Event:

  • selectAction for On Select – Create a Notification Action and when any node is selected, it will give a callback as a notification with the text “Node Selected”
  • collapseAction for On collapse – Create a Notification Action and when nodes are collapsed, it will give a callback as a notification with the text “Nodes Collapsed”
  • expandAction for On expand – Create a Notification Action and when nodes are expanded it will give a callback as a notification with the text “Nodes expanded”
  • deselectAction for On deselect – Create a Notification Action and when any node is deselected, it will give a callback as a notification with the text “Node DeSelected”

The Prefab application can also add an Event listener for an event and take some appropriate action once the event happens.

Passing Data to Prefab in Project

Finally, this Prefab needs data to render in the Tree format.

  1. For this, we have created a Model Variable with the following JSON structure:

  2. Bind the above Variable to the Tree Data property of the Prefab:

  3. Run the app and see the Prefab in action
 
We use cookies to provide you with a better experience. By using our website you agree to the use of cookies as described in our Privacy Policy.