gtcesar/recursive-db-tree

Adianti 框架的通用高效递归数据库树组件。

v0.0.2 2024-05-28 12:47 UTC

This package is auto-updated.

Last update: 2024-09-29 00:43:06 UTC


README

Static Badge Static Badge Adianti Framework Static Badge Donate

RecursiveDBTree 插件是一个为 Adianti 框架量身定制的通用高效解决方案。它为开发者提供了一套强大的工具集,以便无缝管理层次化数据结构。该组件便于在您的 Web 应用程序中直观地进行树导航和处理。

主要特性

  • 通用:处理各种层次化数据结构。
  • 高效:针对性能和资源利用进行了优化。
  • 直观导航:提供直观的界面来浏览树结构。
  • 易操作:允许轻松操作树元素,包括添加、编辑和删除节点。
  • 上下文菜单选项:提供上下文菜单选项,以增强用户交互和管理树节点。

基于 VanillaTree 库

本插件基于 VanillaTree JavaScript 库。借助 VanillaTree 的功能,RecursiveDBTree 组件增强了 Adianti 框架,使开发者能够轻松实现动态和交互式树视图。

Adianti 框架依赖

本插件专门为 Adianti 框架设计。请确保您的项目正在使用 Adianti 以实现无缝集成和最佳性能。

安装

运行以下命令: composer require gtcesar/recursive-db-tree

用法示例

SQL

段表的定义,包括 id、parent_segment_id 和 description 列。插入语句向段表中添加数据,用一些段及其关系示例填充。

CREATE TABLE IF NOT EXISTS `segment` (
  `id` int(11) NOT NULL,
  `parent_segment_id` int(11) DEFAULT NULL,
  `description` varchar(200) COLLATE utf8_unicode_ci NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

INSERT INTO `segment` (`id`, `parent_segment_id`, `description`) VALUES
(1, NULL, 'Transportation'),
(2, 1, 'Executive'),
(3, 1, 'Fractional'),
(4, NULL, 'Information Technology'),
(5, 4, 'Software'),
(6, 4, 'Support and Maintenance'),
(7, NULL, 'Retail'),
(8, 7, 'Cleaning Supplies'),
(16, 8, 'Chemicals'),
(17, 16, 'Controlled');

模型

在 app/model/Segment.php 中创建模型

/**
 * Segment
 *
 * Represents a segment entity in the database.
 * Extends TRecord, the base class for database records in Adianti Framework.
 */
class Segment extends TRecord
{
    const TABLENAME = 'segment'; // Name of the database table
    const PRIMARYKEY = 'id'; // Primary key field name
    const IDPOLICY = 'serial'; // ID generation policy (max, serial)

    /**
     * Constructor method
     *
     * @param int|null $id The ID of the segment (optional)
     * @param bool $callObjectLoad Whether to call the parent's load method (default: TRUE)
     */
    public function __construct($id = NULL, $callObjectLoad = TRUE)
    {
        // Call the parent constructor to initialize the object
        parent::__construct($id, $callObjectLoad);
        
        // Add attributes to the record
        parent::addAttribute('parent_segment_id'); // Parent segment ID
        parent::addAttribute('description'); // Segment description
    }
}

控制器

在 app/controller/SegmentTree.php 中创建控制器

use Gtcesar\RecursiveDBTree\RecursiveDBTree;

/**
 * SegmentTree
 *
 * This class represents a segment tree page.
 * It displays a hierarchical structure of segments using RecursiveDBTree component.
 * Users can interact with the segments by selecting, editing, deleting, or viewing them.
 */
class SegmentTree extends TPage
{
    /**
     * Class constructor
     * Creates the page and initializes its components
     */
    function __construct()
    {
        parent::__construct();
        
        // Create a panel
        $panel = new TPanelGroup('Segment');
       
        // Create a RecursiveDBTree instance to display segments
        $segment = new RecursiveDBTree('segment', 'app', 'Segment', 'id', 'parent_segment_id', 'description', 'id asc');
        $segment->collapse();
        
        // Set an action when selecting an item
        $segment->setItemAction(new TAction(array($this, 'onSelect')));
        
        // Add options to the context menu
        $segment->addContextMenuOption('Edit', new TAction([$this, 'onEdit']));
        $segment->addContextMenuOption('Delete', new TAction([$this, 'onDelete']));
        $segment->addContextMenuOption('View', new TAction([$this, 'onView']));
        
        $panel->add($segment);
        
        // Wrap the page content using a vertical box
        $vbox = new TVBox;
        $vbox->style = 'width: 100%';
        $vbox->add($panel);

        parent::add($vbox);
    }
    
    /**
     * Handle item selection
     * Displays information about the selected item
     */
    public function onSelect($param)
    {
        new TMessage('info', str_replace(',', '<br>', json_encode($param)));
    }    
    
    /**
     * Handle item editing
     * Displays information about the item being edited
     */
    public function onEdit($param)
    {
        new TMessage('info', str_replace(',', '<br>', json_encode($param)));
    }    

    /**
     * Handle item deletion
     * Displays information about the item being deleted
     */
    public function onDelete($param)
    {
        new TMessage('info', str_replace(',', '<br>', json_encode($param)));
    }    

    /**
     * Handle item viewing
     * Displays information about the item being viewed
     */
    public function onView($param)
    {
        new TMessage('info', str_replace(',', '<br>', json_encode($param)));
    }  
}

结果

示例的结果

许可证

MIT

免费软件,太棒了!

给我来一杯咖啡

啊,咖啡!这是我超越数字世界的激情之一。那种难以忘怀的香气,浓郁的滋味...每喝一口都仿佛带来一股新的能量。对我来说,咖啡不仅仅是饮料,它是不可或缺的工作伙伴。从雾蒙蒙的早晨到深夜的灵感迸发,它总是陪伴在我身边,准备让我恢复活力,保持头脑清醒。

一杯简单的咖啡对我的表现产生了惊人的影响。它帮助我保持专注、集中和创造力,使我能够以更大的热情和决心应对一天的挑战。没有它,我承认我就不是现在的我了!

所以,是的,一杯咖啡在这里非常受欢迎。它是解锁我全部潜能和确保我能够在每一次互动中提供最好的自己的关键。

Donate