WebSite-PHP Framework PHP
Mehrsprachig
Einfacher Ajax Modus
Kein HTML, kein JavaScript
URL Rewriting
Mails senden
Sitemap - RSS - Web service
Laden Sie WebSite-PHP FrameWork jetzt herunter
 


Loading
 


Tutorial : Add / delete dynamically 
folder / file (Ajax events + right click)

Tutorials

>

Tutorial Treeview

>

Tutorial : Add / delete dynamically folder / file (Ajax events + right click)


Treeview-04: Tutorial der Komponente Treeview vom PHP FrameWork WebSite-PHP.

Tutorial : Add / delete dynamically folder / file (Ajax events + right click):



Zurück zum Tutorial der Komponente Treeview



Quelltext

Quelltext


Datei: /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();
    }
}
?>


Zurück zum Tutorial der Komponente Treeview


Share

 


Copyright © 2009-2024 WebSite-PHP Framework PHP
Start Dokumentation Herunterladen Schnellstart Tutorials Wiki Issue Tracker