How to use templates to style the different nodes of the Ignite UI Tree control

Marina Stoyanova / Tuesday, June 17, 2014

 Recently I published a blog about the changes around the template and how to use different template engines to customize the look and style of some of the Ignite UI controls. The Tree control goes under the same rules. If you want the structure of your jQuery Tree to be unique you can create a template and apply it to all of the nodes in it. In our UserVoice site we received a request for a way to style each tree node based upon a number of criteria. In the blog I will try to explain you how to achieve that.

Node’s templates

You can easily style any node by creating a template and providing it to each level of binding of the igTree. By setting the nodeContentTemplate option of the binding object, you can define custom HTML which will be shown for each node in the control. If you have read my blog about how to use different template engines with the Ignite UI controls you probably already know that you can create the template by using your favorite Template Engine and apply it to the tree. For the purpose of the sample I used the Infragistics template engine.

  1. $("#tree").igTree({
  2.     dataSource: files,
  3.     dataSourceType: "json",
  4.     checkboxMode: "triState",
  5.     bindings: {
  6.         textKey: "Text",
  7.         imageUrlKey: "ImageUrl",
  8.         nodeContentTemplate: "${Text}  ",
  9.         childDataProperty: "Folder",
  10.         bindings: {
  11.             textKey: "Text",
  12.             imageUrlKey: "ImageUrl",
  13.             nodeContentTemplate: "${Text}  ",
  14.             childDataProperty: "Folder",
  15.             bindings: {
  16.                 textKey: "Text",
  17.                 imageUrlKey: "ImageUrl",
  18.                 nodeContentTemplate: "${Text}  "
  19.             }
  20.         }
  21.     }
  22. });

This is how you can create a hierarchical  tree structure and use different template for the separate levels of binding. When you add new child nodes they will be styled according to the template on the  relevant level of the tree.

  1. //remove a node
  2. $(document).on('click', 'img[data-role=delete-node]', function () {
  3.     var node = $(this).closest('li');
  4.     $('#tree').igTree('removeAt', node.attr('data-path'));
  5. });
  6. //add new node
  7. $(document).on('click', 'img[data-role=add-node]', function () {
  8.     var node = $(this).closest('li');
  9.     $('#tree').igTree('addNode', { Text: 'New Node' }, node);
  10. });

Image:

igTree

Check out the live demo in jsFiddle.

Using conditional template

In the online documentation you can find a detailed step by step guidance how to add and remove nodes from the igTree control. We are going to use a context menu that offers choices such as adding a new node and deleting a node. For more information on how to create a context menu you can read the “The Infragistics Tree with a Context Menu” blog. Let’s see how to apply a predefined template when we add new sibling nodes or new child nodes.

We are going to create a condition template and initialize the igTree first.

Template:

  1. <script id="template" type="text/template">
  2.     {{if ${Type} == "MusicFolder"}}
  3.     <img src="http://igniteui.com/images/samples/tree/book.png">
  4.     {{elseif ${Type} == "PictureFolder"}}
  5.     <img src="http://igniteui.com/images/samples/tree/coins.png">
  6.     {{elseif ${Type} == "DocumentFolder"}}
  7.     <img src="http://igniteui.com/images/samples/tree/documents-folder.png">
  8.     {{elseif ${Type} == "Computer"}}
  9.     <img src="http://igniteui.com/images/samples/tree/computer.png">
  10.     {{elseif ${Type} == "MusicFile"}}
  11.     <img src="http://igniteui.com/images/samples/tree/music.png">
  12.     {{elseif ${Type} == "DocumentFile"}}
  13.     <img src="http://igniteui.com/images/samples/tree/documents.png">
  14.     {{elseif ${Type} == "PictureFile"}}
  15.     <img src="http://igniteui.com/images/samples/tree/coins_add.png">
  16.     {{else}}
  17.     <img src="http://igniteui.com/images/samples/tree/documents-folder.png"/>
  18.     {{/if}}
  19.     ${Text}
  20. script>

Applying it for the template:

  1. $("#tree").igTree({
  2.     dataSource: files,
  3.     dataSourceType: "json",
  4.     checkboxMode: "triState",
  5.     bindings: {
  6.         nodeContentTemplate: $("#template").html(),
  7.         childDataProperty: "Folder"
  8.     }
  9. });

Image:

igTree with template

As you can see from the above code snippet we have created a simple template with conditions which determine which image to be used for the particular node. This way you can easily create different appearance for the nodes based upon a number of criteria.

Now let’s start with adding a sibling node. First we are going to create a menu with a dialog with by using the igDialog, igEditor, igCombo and igButton controls. Because the purpose of the blog is to show you how to implement and use template for the different nodes, I’m going to focus on the event that is triggered when the save button is selected.  What happens there is: The User selects “Add new sibling” and a dialog window appears asking for a name and type for the new node(it is important to set the type for the node because the image for the node will be determined based upon it) and to save it. When you select the save button we take the value that you have filled in the editor’s input and assign it to the “Text” variable in the predefined array that contains the data we want to add in the tree structure.

  1. var newFolders = [{ Text: "Smth", Value: "File",Type:"",  Folder: "" }];

Then we use the addNode method. This method uses three parameters –node, parent and node index . To the node parameter (which specifies the data used to create the new nodes) we assign the new data with the changed text option. The other two parameters are optional. One is used to specify the jQuery object of the parent node the nodes are to be appended to and the other specifies the index at which the node to be inserted. We are going to use the first one and by using the parentNode method we will extract the element on the required level.

By using this addNode method the new node will go through the template that we use for the original rendering of the igTree.

Initialization of the Save button:

  1. $("#btnSave").igButton({
  2.     labelText: "Save",
  3.     click: function () {
  4.         if ($("#newValue").igEditor("validate")) {
  5.             var newItem = newFolders.clone();
  6.             newItem[0].Text = $("#newValue").val();
  7.             var type = $("#combo").igCombo("value");
  8.             type = type.replace(/\s/g, '');
  9.             
  10.             newItem[0].Type = type;
  11.             $("#tree").igTree("addNode", newItem,
  12.                 $("#tree").igTree("parentNode", node.element)
  13.             );
  14.             $("#dialog").igDialog("destroy");
  15.             $("#dialog").remove();
  16.         }
  17.     }
  18. });

Image:

Adding sibling node to igTree

The situation with adding a child node is similar the only difference occurs on the third parameter of the addNode method. You don’t have to use the parentNode method here, you just need the node element.

Adding a child node element:

  1. $("#btnSave").igButton({
  2.     labelText: "Save",
  3.     click: function () {
  4.         if ($("#newValue").igEditor("validate")) {
  5.             var newItem = newFolders.clone();                        
  6.             newItem[0].Text = $("#newValue").val();
  7.             var type = $("#combo").igCombo("value");
  8.             type = type.replace(/\s+/g, '');
  9.             newItem[0].Type = type;
  10.             $("#tree").igTree("addNode", newItem, node.element);
  11.             $("#dialog").igDialog("destroy");
  12.             $("#dialog").remove();
  13.         }
  14.     }
  15. });

Image:

Adding a child node to igTree

Summary

The Ignite UI Tree control simplifies the presentation of your hierarchical data into a web-based tree structure. By applying a template to the control you can easily style the nodes and even more you can create the template by using any available template engine such as jsRender, Handlebars, Mustache and others. 

You can see a working demo on jsFiddle.

 

You can follow us on Twitter @Infragistics and stay in touch on Facebook, Google+ and LinkedIn!