# Generic tree directive

The`sfTree` directive enables you to display items hierarchically and provides you with the functionality to retrieve the selected item. You can use the directive to display hierarchical tree in the frontend, as well as in the backend. For example, on a page, as well as in a widget designer.

The `sfTree` is a directive with an isolated scope that is defined in a module with the same name: `sfTree`. \
For more information, see [Isolating the Scope of a Directive](https://docs.angularjs.org/guide/directive#isolating-the-scope-of-a-directive).

## Attributes

The `sfTree` directive exposes the following attributes:

 <table>
<colgroup><col><col></colgroup>
<tbody>
<tr>
<td>

<strong>Attribute</strong>

</td>
<td>

<strong>Description</strong>

</td>
</tr>
<tr>
<td>

<code>sf-request-children</code><br>

</td>
<td>

Accepts a callback function that must provide a promise with the array of items that are going to be displayed. The callback must return the root items or the child items of the currently selected node. The callback function is invoked during the initial load of the <code>sfTree</code> directive. The callback function is also invoked for each subsequent expansion, if the child nodes of the selected item have not been retrieved yet.

</td>
</tr>
<tr>
<td>

<code>sf-model</code><br>

</td>
<td>

Accepts a scope variable that references the identifier field of the selected item. If there is no selected item, the value of this variable is <code>null</code>.

</td>
</tr>
<tr>
<td>

<code>sf-identifier</code><br>

</td>
<td>

Specifies which property value is to be exposed in the <code>sf-model</code> collection. If you do not specify this attribute, the <code>sfTree</code> directive exposes the item itself in the <code>sf-model</code> collection.

</td>
</tr>
<tr>
<td>

<code>sf-expand-on-select</code><br>

</td>
<td>

Specifies whether the tree will be expanded when an item is selected.

</td>
</tr>
<tr>
<td>

<code>sf-deselectable</code><br>

</td>
<td>

Specifies whether the tree allows selected items to be deselected when they are clicked again. If an item is deselected, the value of the <code>sf-model</code> is set to <code>null</code>.

</td>
</tr>
<tr>
<td>

<code>sf-template-url</code><br>

</td>
<td>

Allows you to override the template of the tree.

</td>
</tr>
<tr>
<td>

<code>sf-template-assembly</code><br>

</td>
<td>

Specifies the assembly where the template of the tree is located.

</td>
</tr>
<tr>
<td>

<code>sf-item-template-url</code><br>

</td>
<td>

Allows you to override the item template of the tree.

</td>
</tr>
<tr>
<td>

<code>sf-item-template-assembly</code><br>

</td>
<td>

Specifies the assembly where the item template of the tree is located.

</td>
</tr>
<tr>
<td>

<code>sf-has-children-field</code><br>

</td>
<td>

Accepts the name of the scope variable that determines whether a node has children. If this attribute value is not explicitly specified, the <code>sfTree</code> assumes that a node has children. When requesting the children of a node that has no children, an empty array is returned.

</td>
</tr>
</tbody>
</table>

## Add the generic tree directive

The following example demonstrates how to add a generic tree directive in a widget designer's view.

To enable AngularJs to link the `sfTree` directive in your custom designer view, you must load the script of the directive and add a dependency to the module.\
Perform the following:

1. Sitefinity CMS automatically registers the scripts you need and, if no other designer view with explicitly set priority exists, Sitefinity CMS sets your designer view priority 1. In case you need to have full control over the scripts that are loaded or you want to set custom priority, you can alternatively create your own `DesignerView.YourView.json` file. If you have a `JSON` file that matches the convention (even if empty), this automatic scripts registration will not occur. In your `DesignerView.YourView.json` file, add a scripts array.\
   Use the following code:

   ```JSON
   {
     "priority": 1,
     "components" : ["sf-tree"]
   }
   ```
2. In your `designerview-yourview.js` file, place the following code snippet:

   ```JavaScript
   (function () {
       var designerModule = angular.module('designer');

       angular.module('designer').requires.push('sfTree');

       designerModule.controller('SettingsCtrl', ['$scope', '$q', function ($scope, $q) {

           $scope.requestChildrenCallback = function (parentObj) {
               parentObj = parentObj || {};
               var parent = parentObj.IsParent;
               var result = $q.defer();

               if (!parent) {
                   result.resolve([{ Key: '1', Title: 'Title Root 1', IsParent: true },
                                   { Key: '2', Title: 'Root 2', IsParent: false }]);
               }

               else {
                   result.resolve([{ Key: parentObj.Key + '#1', Title: '1st child of ' + parentObj.Key, IsParent: true },
                                   { Key: parentObj.Key + '#2', Title: '2nd child of ' + parentObj.Key, IsParent: false }]);
               }

               return result.promise;
           };
       }]);
       
       // Subscribe to selection event
       $scope.$on('sf-tree-item-selected', function (event, item) { });

   })();
   ```

   The code above is invoked during the initial load of the `sfTree` directive and two root elements are added to the tree. If you select an item which is a parent, the tree expands and invokes the function. As a result, two more elements are added under the currently selected node. \
   If the `sf-has-children-field` attribute is present, its scope variable is evaluated. If the attribute is not present, all elements of the tree visually appear as if they have children. If you try to expand such an element, the response is an empty array and the item no longer appears as a parent.
3. In your `DesignerView.YourView.cshtml` file, place the following tag where you want to render the `sfTree` directive:
   ```HTML
   <sf-tree sf-model="selectedItemId" sf-request-children="requestChildrenCallback(parent)" sf-identifier="Key" sf-has-children-field="IsParent" sf-expand-on-select/>
   ```

## Subscribe to selection event

To provide notification when an item is selected the `sfTree` directive emits a custom selection event. To subscribe to the selection event, use the code in *Step 2* of the above procedure.
