sharkom/ yii2-nested-sortable-widget
nestedSortable2.0的实现
v1.0.5
2023-09-29 16:48 UTC
Requires
- bower-asset/jquery-nested-sortable: *
- yiisoft/yii2: *
- yiisoft/yii2-jui: *
README
安装
安装此扩展的首选方式是通过composer。
运行以下命令之一
php composer.phar require --prefer-dist sharkom/yii2-nested-sortable "*"
或在您的composer.json文件的require部分中添加
"sharkom/yii2-nested-sortable": "*"
准备模型
在表迁移中
$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 sharkom\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' => 'sharkom\yii2nestedSortable\NestedSortableAction', //'scenario'=>'editable', //optional 'modelclass' => Page::className(), ], ]; }
.