nestedSortable2.0的实现
v1.0.2
2017-11-29 16:00 UTC
Requires
- bower-asset/jquery-nested-sortable: *
- yiisoft/yii2: *
- yiisoft/yii2-jui: *
This package is auto-updated.
Last update: 2024-09-11 19:01:13 UTC
README
安装
通过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(), ], ]; }