zlatov/yii-components

为 yii2 的一些扩展组件

1.1.2 2017-06-09 05:59 UTC

This package is auto-updated.

Last update: 2024-09-28 04:43:48 UTC


README

  1. 行为(behaviors)
  2. 特性(traits)
  3. 小部件(widgets)
  4. 辅助器(helpers)
  5. 资源(assets)

行为(behaviors)

Sid

在实例类验证前,根据指定属性生成字符串标识符,从而允许在模型中根据特定场景将字符串标识符作为非必填字段。

使用方法

use \Zlatov\yiiComponents\behaviors\Sid;

...

    public function behaviors()
    {
        return [

            ...

            'sid' => [
                'class' => Sid::className(),
                'in_attribute' => 'header',
            ],

            ...

        ];
    }
    
    ...

    public function rules()
    {
        return Sid::calcRules(parent::rules());
    }

特性(traits)

Sid

为模型提供从数据库获取数据并将其转换为树状结构输出的方法。

使用方法

模型

class ...
{
    use \Zlatov\yiiComponents\traits\Tree;

    private static $treeOptions = [
        'fnId' => 'id',
        'fnPid' => 'pid',
        'fnChildrens' => 'childrens',
        'fnHeader' => 'header',
        'fnLevel' => 'level',
        'idOfTheRoot' => null,
        'addRoot' => false,
        'returnOnly' => null,
        'clearFromNonRoot' => true,
        'rootName' => 'Нет родителя (этот элемент корневой)',
        'forSelect' => false,
        'order' => [
            'level' => SORT_ASC,
            'order' => SORT_ASC,
        ],
    ];

控制器

    public function actionIndex()
    {
        $sections = Section::treeMulti();

        return $this->render('index', [
            'sections' => $sections,
        ]);
    }

视图

    <?= Zlatov\yiiComponents\widgets\ViewTree::widget([
        'viewTree' => $sections,
        'options' => [
            'admin' => true,
        ],
        'model' => null
    ]) ?>

小部件(widgets)

ViewTree

辅助器(helpers)

文本

资源(assets)

ViewTreeAsset

<?= ViewTree::widget([
	'ztree' => $current_menu,
	'current_id' => $model->id,
]) ?>

ViewTreeSelectAsset

FormAsset

使用方法

Zlatov\yiiComponents\assets\FormAsset::register($this);