Tree menu

The GUI library features a component for building tree structures, which is useful for creating folder trees and menus with a hieratic structure. The example below demonstrates how to use the component.

/TreeMenu.png

public function Render()
{
    // First we create all items

    $filesItem = new SMTreeMenuItem("UniqueId1", "files", "files");
    $filesItem->AddChild(new SMTreeMenuItem("UniqueId2", "downloads", "files/downloads"));
    $filesItem->AddChild(new SMTreeMenuItem("UniqueId3", "editor", "files/editor"));

    $galleryItem = new SMTreeMenuItem("UniqueId4", "gallery", "files/gallery");
    $galleryItem->AddChild(new SMTreeMenuItem("UniqueId5", "Cars", "files/gallery/Cars"));

    $filesItem->AddChild($galleryItem);
    $filesItem->AddChild(new SMTreeMenuItem("UniqueId6", "images", "files/images"));

    // Now we create the actual tree control and assign the root node (files)

    $tree = new SMtreeMenu("UniqueId");
    $tree->AddChild($filesItem);

    // We create a button that can be used to post back the selected value

    $button = new SMLinkButton("UniqueButtonId");
    $button->SetTitle("Click me to post back");

    // Output button, tree, and selection (if made)

    $output = $tree->Render() . "<br><br>" . $button->Render();

    if ($tree->GetSelectedValue() !== null)
        $output .= "<br><br> Selected item: " . $tree->GetSelectedValue();

    return $output;
}
The code above will create a tree structure identical to the example image in the top of this page. Selecting an item by clicking it, will result in the given item being marked as selected. Click the submit button, to see the selected value being read server side.

By default the control remembers its state on the computer on which it is being used, so items expanded/collapsed will have the same state, next time the control is used. The tree can also be set to automatically collapse all nodes, or expand all nodes. See the class descriptions for more information.


SMTreeMenu class

Function Return type Description
__construct($id:string)

Create instance of tree menu with unique ID.
GetId()
String
Get unique ID specified in constructor.
AddChild($child:SMTreeMenuItem)

Add root item to tree. See class description for SMTreeMenuItem for further information.
RemoveChild($id:string)
Boolean
Remove root item by unique ID. Returns True on success, otherwise False (item not found).
RemoveChild($id:string, $searchDeep:boolean) Boolean Remove child by unique ID from anywhere in the hierarchy, if $searchDeep is True. Returns True on success, otherwise False (item not found).
SetChildren($children:SMTreeMenuItem[])

Set internal child collection.
GetChildren()
SMTreeMenuItem[]
Get internal child collection.
GetChild($id:string)
SMTreeMenuItem
Get root item by unique ID. Returns NULL if not found.
GetChild($id:string, $searchDeep:boolean) SMTreeMenuItem Get item by unique ID. Search all children and their children if $searchDeep is True. Returns NULL if not found.
SetAutoPostBack($value:boolean)

Set True to perform auto post back when an item is selected, False not to (default).
GetAutoPostBack()
Boolean
Get value indicating whether auto post back has been enabled or not.
SetCollapsed($value:boolean)

Set True to have all items automatically collapse, False not to (default)
GetCollapsed()
Boolean
Get value indicating whether all items by default are collepsed or not.
SetRestoreState($value:boolean)

Set True to remember which nodes are collepsed and which are expanded, False not to. A cookie is used to remember the state. This feature is enabled by default.
GetRestoreState()
Boolean
Get value indicating whether the state of the menu will be stored locally on the computer or not.
PerformedPostBack()
Boolean
Returns True if control performed a post back, otherwise False.
GetSelectedId()
String
Returns the ID of the selected node. Returns NULL if post back has not been performed, or if no selection has been made.
GetSelectedValue()
String
Returns the value of the selected node. Returns NULL if post back has not been performed, or if node is not found (ie if it has been removed in another session).
SetSelected($id:string)

Set selected node by its unique ID.
SetRender($value:boolean)

Set True to have control rendered when invoking Render(), false not to.
GetRender() Boolean Get value indicating whether control is going to be rendered.
Render() String Get code representing the control client side.


SMTreeMenuItem class

Function Return type Description
__construct($itemId:string, $title:string, $value:string)

Create instance of tree item with a unique ID, a display title, and an internal value.
GetId()
String
Get unique ID specified in constructor.
GetParentId() String Get unique parent ID.
SetTitle($value:string)

Change title to specified value.
GetTitle()
String
Get title.
SetValue($value:string)

Change internal value to specified value.
GetValue()
String
Get internal value.
AddChild($child:SMMenuTreeItem)

Add child node.
RemoveChild($id:string)
Boolean
Remove child by its unique ID. Returns True on success, otherwise False (item not found).
GetChildren()
SMTreeMenuItem[]
Get internal collection of children.
GetChild($id:string)
SMTreeMenuItem
Get child by its unique ID. Use SMTreeMenu->GetChild($id:string, $searchDeep:boolean), to get a child by its unique ID anywhere in the hierarchy.
SetSelectable($value:boolean)

Set True (default) to allow element to be selected, False to make it in-selectable.
GetSelectable()
Boolean
Get value indicating whether item is selectable or not.
Render()
String
Get code representing the item client side.