quanzo/yii2-tree

Ajax tree mapping. Adjacency list tree type. The ability to change the structure.

1.0.0 2019-09-05 18:36 UTC

This package is auto-updated.

Last update: 2024-04-06 04:49:36 UTC


README

Install

Install via Composer:

composer require quanzo/yii2-tree

or add

"quanzo/yii2-tree" : "*"

to the require section of your composer.json file.

Config

$config = [
...
    'modules' => [
...
        'tree' => [
            'class' => 'x51\yii2\modules\tree\Module',
            'tree' => [ // tree configs
                'category' => [
                    'tableName' => '{{%category}}',
                    'keyField' => 'id',
                    'parentField' => 'post_parent',
                    'nameField' => 'post_name',
                ],
                'articles' => [
                    'module' => 'articles',
                    //'where' => ['post_type'=>'category'],
                    'name' => function ($arItem) {
                        return '[' . $arItem['post_type'] . '] ' . $arItem['post_title'];
                    },
                ],
                'category' => [
                    'module' => 'articles',
                    'where' => ['post_type' => 'category'],
                ],
            ],
        ],
        // another definition
        'rubricator' => [
            'class' => '\x51\yii2\modules\tree\Module',
            'itemTemplate' => '<li><span class="expand" data-id="{id}" data-tree="{tree}">⊕</span><a href="{url}" class="item" data-id="{id}" data-tree="{tree}">{name}</a><span class="subitems">{subitems}</span></li>',
            'params' => [ // Extra options. see itemTemplate
                'url' => function ($cfg, $item) {
                    if (!empty($cfg['module'])) {
                        return yii\helpers\Url::to(['/articles/post/view', 'id' => $item['id']]);
                    } else {
                        return '##';
                    }
                },
            ],
            'tree' => [
                'articles' => [
                    'module' => 'articles',
                    'where' => ['post_type' => 'category'],
                    'urlDelete' => false,
                    'urlMove' => false,
                    'allowSelect' => false,
                    'activeID' => function ($cfg) {
                        if (!empty($_GET['id'])) {
                            return intval($_GET['id']);
                        }
                        return false;
                    },
                ],
            ],
        ],
...
    ],
...
];

Using

echo \Yii::$app->getModule('tree')->widget('category');
// or
echo \Yii::$app->getModule('tree')->widget('category', $arAdvParams);
/* reassign parameters
$arAdvParams = [
    ...
    'urlDelete' => false,
    'urlMove' => false,
    'urlBranch' => false,
    ...
];
*/