WebSite-PHP Framework PHP
Multi langues
Mode Ajax simple
Aucun HTML, aucun JavaScript
URL rewrinting
Envoi d'email
Sitemap - RSS - Web service
Télécharger le Framework WebSite-PHP maintenant
 


Loading
 


Tutoriel Treeview

Tutoriels

>

Tutoriel Treeview



Tutoriel 1 : Simple Treeview
Fichier: /pages/tutorials/treeview/treeview-01.php

<?php
class Treeview01 extends Page {
    public function 
InitializeComponent() {
        
parent::$PAGE_TITLE "Tutorial : Simple Treeview";
        
        
$tree = new TreeView("tree01");
        
$root = new TreeViewFolder("root");
        
        
$folder1 = new TreeViewFolder("folder1");
        
$file1 = new TreeViewFile("file1");
        
$file2 = new TreeViewFile("file2");
        
$folder1->setTreeViewItems(
                new 
TreeViewItems($file1$file2));
                
        
$folder2 = new TreeViewFolder("folder2");
        
$file3 = new TreeViewFile("file3");
        
$file4 = new TreeViewFile("file4");
        
$folder2->setTreeViewItems(
                new 
TreeViewItems($file3$file4));
        
$folder2->collapse();
                
        
$folder3 = new TreeViewFolder("folder3");
        
$file5 = new TreeViewFile("file5");
        
$file6 = new TreeViewFile("file6");
        
$folder3->setTreeViewItems(
                new 
TreeViewItems($file5$file6));
        
$folder3->collapse();
        
        
$root->setTreeViewItems(
                new 
TreeViewItems($folder1$folder2$folder3));
                
        
$tree->setTreeViewItems(
                new 
TreeViewItems($root));
        
        
$this->render $tree;
    }
}
?>


Tutoriel 2 : Treeview loaded from path
Fichier: /pages/tutorials/treeview/treeview-02.php

<?php
class Treeview02 extends Page {
    public function 
InitializeComponent() {
        
parent::$PAGE_TITLE "Tutorial : Treeview 
                    loaded from path"
;
        
        
$tree = new TreeView("tree02");
        
        
// To display a dialog box with file information
        // you can use {#file} to display all the path
        
$dialog = new DialogBox('File''You click on the file {#filename}');
        
        
// Create the tree from the current folder
        
$tree->loadFromPath(dirname(__FILE__), $dialog);
        
        
$this->render $tree;
    }
}
?>


Tutoriel 3 : Treeview with context menu (right click)
Fichier: /pages/tutorials/treeview/treeview-03.php

<?php
class Treeview03 extends Page {
    public function 
InitializeComponent() {
        
parent::$PAGE_TITLE "Tutorial : Treeview with 
                    context menu (right click)"
;
        
        
// Init treeview
        
$tree = new TreeView("tree03");
        
$root = new TreeViewFolder("root");
        
        
$folder1 = new TreeViewFolder("folder1");
        
$file1 = new TreeViewFile("file1");
        
$file2 = new TreeViewFile("file2");
        
$folder1->setTreeViewItems(
                new 
TreeViewItems($file1$file2));
                
        
$folder2 = new TreeViewFolder("folder2");
        
$file3 = new TreeViewFile("file3");
        
$file4 = new TreeViewFile("file4");
        
$folder2->setTreeViewItems(
                new 
TreeViewItems($file3$file4));
        
$folder2->collapse();
                
        
$folder3 = new TreeViewFolder("folder3");
        
$file5 = new TreeViewFile("file5");
        
$file6 = new TreeViewFile("file6");
        
$folder3->setTreeViewItems(
                new 
TreeViewItems($file5$file6));
        
$folder3->collapse();
        
        
$root->setTreeViewItems(
                new 
TreeViewItems($folder1$folder2$folder3));
                
        
$tree->setTreeViewItems(
                new 
TreeViewItems($root));
        
        
// Create context menu
        
$folder_menu = new ContextMenu("context_menu_folder_03");
        
$folder_menu->addItem("Folder"
                        
ContextMenu::CONTEXTMENU_ICON_FOLDER);
        
$file_menu = new ContextMenu("context_menu_file_03");
        
$file_menu->addItem("File"
                        
ContextMenu::CONTEXTMENU_ICON_PAGE);
                        
        
$tree->setContextMenuRoot($folder_menu);
        
$tree->setContextMenuFolder($folder_menu);
        
$tree->setContextMenuFile($file_menu);
        
        
$this->render $tree;
    }
}
?>


Tutoriel 4 : Add / delete dynamically folder / file (Ajax events + right click)
Fichier: /pages/tutorials/treeview/treeview-04.php

<?php
class Treeview04 extends Page {
    public function 
InitializeComponent() {
        
parent::$PAGE_TITLE "Tutorial : Add / delete dynamically 
                    folder / file (Ajax events + right click)"
;
        
        
// Init treeview
        
$this->tree = new TreeView("tree04");
        
$root = new TreeViewFolder("Root");
        
$root->setTag(-1);
        
$items_array = array();
        
$items_array[-1] = $root;
        
$this->tree->setTreeViewItems(new TreeViewItems($root));
        
        
// Create nodes from database
        
$treeview_items null;
        
$last_parent_id = -9999;
        
$treeview_list = new TreeviewObjList();
        
$treeview_array $treeview_list->getTreeviewObjectArray(""
                                    
TreeviewDbTable::FIELD_PARENT);
        for (
$i=0$i sizeof($treeview_array); $i++) {
            
$node $treeview_array[$i];
            
            
// Create TreeViewItems to associate on parent node
            
if ($last_parent_id != $node->getParent()) {
                if (
$treeview_items != null && $parent_node != null) {
                    
$parent_node->setTreeViewItems($treeview_items);
                }
                
$treeview_items = new TreeViewItems();
                
$last_parent_id $node->getParent();
            }
            
            
// get parent node
            
$parent_node $items_array[$node->getParent()];
            
            
// Create node (file or folder)
            
if ($node->getType() == "D") { // Folder
                
$item = new TreeViewFolder("Folder ".$node->getId()); 
                
$item->setTag($node->getId()); // to store node id
            
} else {
                
$item = new TreeViewFile("File ".$node->getId()); 
                
$item->setTag($node->getId()); // to store node id
            
}
            
            
// associate item on parent node
            
$treeview_items->add($item);
            
            
// save item on array items_array
            
$items_array[$node->getId()] = $item;
        }
        if (
$treeview_items != null && $parent_node != null) {
            
$parent_node->setTreeViewItems($treeview_items);
        }
        
        
// Create context menu
        
$menu_form = new Form($this); // necessary in tab
        
$this->folder_menu = new ContextMenu("context_menu_folder_4");
        
        
$add_folder_event = new ContextMenuEvent($menu_form);
        
$add_folder_event->onClick("createFolder")->setAjaxEvent();
        
$this->folder_menu->addItem("Add Folder"
                        
ContextMenu::CONTEXTMENU_ICON_FOLDER_ADD,
                        
$add_folder_event);
        
        
$del_folder_event = new ContextMenuEvent($menu_form);
        
$del_folder_event->onClick("deleteFolder")->setAjaxEvent();
        
$this->folder_menu->addItem("Delete Folder"
                        
ContextMenu::CONTEXTMENU_ICON_FOLDER_DELETE,
                        
$del_folder_event);
        
                        
        
$add_file_event = new ContextMenuEvent($menu_form);
        
$add_file_event->onClick("createFile")->setAjaxEvent();
        
$this->folder_menu->addItem("Add File"
                        
ContextMenu::CONTEXTMENU_ICON_PAGE_ADD,
                        
$add_file_event);
        
        
$this->file_menu = new ContextMenu("context_menu_file_4");
        
$del_file_event = new ContextMenuEvent($menu_form);
        
$del_file_event->onClick("deleteFile")->setAjaxEvent();
        
$this->file_menu->addItem("Delete File"
                        
ContextMenu::CONTEXTMENU_ICON_PAGE_DELETE,
                        
$del_file_event);
        
        
// Associate context menu to treeview items
        
$this->tree->setContextMenuRoot($this->folder_menu);
        
$this->tree->setContextMenuFolder($this->folder_menu);
        
$this->tree->setContextMenuFile($this->file_menu);
        
        
// Don't forget to call this methos to be sure you have 
        // generated ids on the node of your tree (necessary to 
        // call the method createFolder, deleteFolder, ...)
        
$this->tree->generateTreeViewIds();
        
        
$this->render $menu_form->setContent($this->tree);
    }
    
    public function 
createFolder($sender$treeview_item) {
        
// Create new folder in database
        
$new_folder = new TreeviewObj(null"D"
                                
$treeview_item->getTag());
        
$new_folder->save();
        
        
// Create folder in the treeview (ajax refresh)
        
$treeview_item->addFolder("Folder ".$new_folder->getId(), 
                                    
$this->folder_menu);
    }
    
    public function 
deleteFolder($sender$treeview_item) {
        if (
$treeview_item->getTag() == -1) {
            
alert("You can't delete root folder");
        } else {
            
// Get chidren if exists
            
$treeview_list = new TreeviewObjList();
            
$treeview_array $treeview_list->getTreeviewObjectArray(
                                
TreeviewDbTable::FIELD_PARENT."=".
                                
$treeview_item->getTag());
            for (
$i=0$i sizeof($treeview_array); $i++) {
                
// Generate child id (path)
                
$child_id $treeview_item->getId()."_";
                if (
$treeview_array[$i]->getType() == "D") {
                    
$child_id .= "folder_";
                } else {
                    
$child_id .= "file_";
                }
                
$child_id .= $treeview_array[$i]->getId();
                
                
// Search chold node in the tree
                
$item $this->tree->searchTreeViewItemId($child_id);
                if (
$item != null) {
                    
// Delete the child node
                    
$this->deleteFolder($sender$item);
                }
            }
            
            
// Delete folder in database
            
$del_folder = new TreeviewObj($treeview_item->getTag());
            
$del_folder->delete();
            
            
// Delete folder in the treeview (ajax refresh)
            
$treeview_item->removeItem();
        }
    }
    
    public function 
createFile($sender$treeview_item) {
        
// Create new file in database
        
$new_folder = new TreeviewObj(null"F"
                                
$treeview_item->getTag());
        
$new_folder->save();
        
        
// Create file in the treeview (ajax refresh)
        
$treeview_item->addFile("File ".$new_folder->getId(), 
                                    
$this->file_menu);
    }
    
    public function 
deleteFile($sender$treeview_item) {
        
// Delete file in database
        
$del_folder = new TreeviewObj($treeview_item->getTag());
        
$del_folder->delete();
        
        
// Delete file in the treeview (ajax refresh)
        
$treeview_item->removeItem();
    }
}
?>



Share

 


Copyright © 2009-2016 WebSite-PHP Framework PHP
Accueil Documentation Télécharger Quick start Tutoriels Wiki Issue Tracker