nestedSortable2.0的实现

安装数: 7,486

依赖者: 0

建议者: 0

安全性: 0

星标: 1

关注者: 2

分支: 2

公开问题: 0

类型:yii2-extension

v1.0.2 2017-11-29 16:00 UTC

This package is auto-updated.

Last update: 2024-09-11 19:01:13 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License

nestedSortable2.0的实现

安装

通过composer安装此扩展是首选方法。

运行以下命令

php composer.phar require --prefer-dist claudejanz/yii2-nested-sortable "*"

"claudejanz/yii2-nested-sortable": "*"

将以下内容添加到您的composer.json文件的require部分。

准备模型

在表迁移中

$this->createTable('page', [
    'id'               => $this->primaryKey(),
    'title'            => $this->string(255)->notNull(),
    'parent_id'        => $this->integer()->null(),
    'weight'           => $this->integer(11)->notNull()->defaultValue(1),
]);

$this->createIndex('idx-page-parent_id', 'page', 'parent_id');
$this->addForeignKey('fk-page-parent_id-page-id', 'page', 'parent_id', 'page', 'id', 'SET NULL', 'CASCADE');

在ActiveRecord中:关于自定义查询类的更多详细信息

/**
* @inheridoc
*/
public static function find()
{
    return (new PageQuery(get_called_class()))->orderBy('weight');
}

/**
* @return ActiveQuery
*/
public function getParent()
{
   return $this->hasOne(Page::className(), ['id' => 'parent_id']);
}

/**
* @return ActiveQuery
*/
public function getPages()
{
   return $this->hasMany(Page::className(), ['parent_id' => 'id'])->inverseOf('parent');
}

使用方法

安装扩展后,只需在代码中使用它即可

在视图中

use claudejanz\yii2nestedSortable\NestedSortable;
echo NestedSortable::widget([
    'items'         => Page::find()->andWhere(['parent_id'=>null])->all(),
    'url'           => ['pages/save-sortable'],
    'contentAttribute' => 'title';
    'itemsAttribute' => 'pages';
]);

在控制器中

public function actions()
{
    return [
        'save-sortable' => [
            'class' => 'claudejanz\yii2nestedSortable\NestedSortableAction',
            //'scenario'=>'editable',  //optional
            'modelclass' => Page::className(),
        ],
    ];
}