kr0lik/yii2-tree-manager

使用 fancytree 管理树的 Yii2 小部件

安装次数: 1,103

依赖项: 0

建议者: 0

安全性: 0

星星: 1

关注者: 1

分支: 1

开放问题: 0

类型:yii2-extension

3.2.0 2020-05-08 17:05 UTC

This package is auto-updated.

Last update: 2024-09-06 03:05:52 UTC


README

使用 fancytree 库的 Yii2 树形管理器。

此扩展可以添加/删除/移动树分支,并快速编辑分支字段。

Tree manager example

Tree select example

安装

安装此扩展的首选方式是通过 composer

运行以下命令到您的 composer.json 文件的 require 部分:

composer require --prefer-dist kr0lik/yii2-tree-manager

描述

此扩展将安装 fancytree 库、yii2-jquery、yii2-jqueryui 和 yii2-bootstrap。

您可以使用任何树形扩展,只需实现 kr0lik\tree\contracts\TreeModelInterface 接口。

模型中必需的字段:id

用法

首先在 Model 中实现 kr0lik\tree\contracts\TreeModelInterface

树形管理器

kr0lik\tree\TreeManagerAction 添加到控制器中。

必需选项

  • treeModelClass - 树形模型类。

可选

  • bsVersion: int - Bootstrap 版本。默认 3。
  • formViewPath - 表单视图路径。
  • formNameField - 节点名字段。默认: name
  • formFields - 额外编辑字段数组(例如:body 或 description)。可以是字符串或可调用。
  • formLinks - 链接数组(例如:查看页面或编辑页面的链接)。可以是字符串或可调用。

示例

<?php
use app\path\to\YourActiveRecord;
use kr0lik\tree\TreeManagerAction;
use yii\web\Controller;
use yii\widgets\ActiveForm;

class YourController extends Controller
{
    public function actions()
    {
        return [
            'tree' => [
                'class' => TreeManagerAction::class,
                'treeModelClass' => YourActiveRecord::class,
                'formNameField' => 'title',
                'formFields' => [
                    'description',
                    function (ActiveForm $form, YourActiveRecord $model) {
                        return $form->field($model, 'body')->textarea();
                    },
                ],
                'formLinks' => [
                    function (YourActiveRecord $model) {
                        return Html::a('View', ['/url/to/view', 'id' => $model->id], ['class' => 'btn btn-sm btn-info']);
                    },
                ]
            ]
        ];
    }
}

kr0lik\tree\TreeManagerWidget 添加到视图中。

必需选项

  • pathAction - 树形模型后端操作的 URL。

可选

  • bsVersion: int - Bootstrap 版本。默认 3。
  • treeOptions: array - 容器标签选项。
  • multipleRoots: bool - 允许多个根。默认: false。
  • activeId: int - 默认激活节点的 ID。
  • dndEnable: bool - 禁用拖放则设置为 null 或 false。默认: true。
  • viewPath - 管理器视图路径。
  • firstRootActivateDefault - 如果没有 activeId,则加载时激活第一个根。默认: true。
  • buttons - 额外按钮数组(作为 Html)。
<?php
use kr0lik\tree\TreeManagerWidget;
?>

<?= TreeManagerWidget::widget([
    'pathAction' => 'url/to/YourController/tree/action',
]) ?>

事件

$(document).on('treeFormAfterLoade', function (event, form, node) {

});
$(document).on('treeFormAfterSubmit', function (event, form, node) {

});
$(document).on('treeElementAfterMove', function (event, targetNode, hitNode) {

});
$(document).on('treeElementAfterRemove', function (event, node) {

});
$(document).on('treeFormAfterReset', function (event, node) {

});

树形输入

kr0lik\tree\TreeAction 添加到控制器中。

必需选项

  • treeModelClass - 树形模型类。

示例

<?php
use yii\web\Controller;
use kr0lik\tree\TreeAction;
use app\path\to\YourActiveRecord;

class YourController extends Controller
{
    public function actions()
    {
        return [
            'tree' => [
                'class' => TreeAction::class,
                'treeModelClass' => YourActiveRecord::class,
            ]
        ];
    }
}

kr0lik\tree\TreeInput 添加到视图中。

必需选项

  • pathAction - 树形模型后端操作的 URL。

可选

  • bsVersion: int - Bootstrap 版本。默认 3。
  • treeOptions: array - 容器标签选项。
  • leavesOnly: bool - 仅选择端点节点。默认: true。
  • multiple: bool - 选择多个节点。默认: false。
  • options: array - 输入选项。
  • viewPath: string - 输入视图路径。
  • collapse: bool - 折叠树。默认: true。

示例

<?php
use kr0lik\tree\TreeInput;
?>

<?= $form->field($model, 'field')->widget(TreeInput::class, [
    'pathAction' => 'url/to/YourController/tree/action',
]) ?>

事件

$(document).on('treeInputChange', function (event, selections) {

});

树形关系输入

kr0lik\tree\TreeRelationAction 添加到控制器中。

必需选项

  • treeModelClass - 树形模型类。
  • relationModelClass - 树形关系模型类。

示例

<?php
use yii\web\Controller;
use kr0lik\tree\TreeRelationAction;
use app\path\to\YourTreeActiveRecord;
use app\path\to\YourRelationActiveRecord;

class YourController extends Controller
{
    public function actions()
    {
        return [
            'tree' => [
                'class' => TreeRelationAction::class,
                'treeModelClass' => YourTreeActiveRecord::class,
                'relationModelClass' => YourRelationActiveRecord::class,
            ]
        ];
    }
}

kr0lik\tree\TreeRelationInput 添加到视图中。

必需选项

  • pathAction - 树形模型后端操作的 URL。

可选

  • bsVersion: int - Bootstrap 版本。默认 3。
  • treeOptions: array - 容器标签选项。
  • leavesOnly: bool - 仅选择端点节点。默认: true。
  • multiple: bool - 选择多个节点。默认: false。
  • options: array - 输入选项。
  • viewPath: string - 输入视图路径。
  • collapse: bool - 折叠树。默认: true。

示例

<?php
use kr0lik\tree\TreeInput;
?>

<?= $form->field($model, 'field')->widget(TreeRelationInput::class, [
    'pathAction' => 'url/to/YourController/tree/action',
]) ?>

事件

$(document).on('treeInputChange', function (event, selections) {

});

国际化

此扩展中引入的所有文本和信息都可在 'kr0lik.tree' 类别下进行翻译。您可以使用此扩展中提供的翻译,使用以下应用程序配置:

return [
    'components' => [
        'i18n' => [
            'translations' => [
                'kr0lik.tree' => [
                    'class' => 'yii\i18n\PhpMessageSource',
                    'basePath' => '@kr0lik/tree/messages',
                ],
                // ...
            ],
        ],
        // ...
    ],
    // ...
];

全局 Bootstrap 版本

您可以通过在 yii params 中添加 bsVersion 选项全局指定树形管理器的 Bootstrap 版本(例如,在 config/params.php 中)。

'params' => [
    'bsVersion' => 4,
    // other settings
],
// ...