orlac / yii2-sortable
为 Yii 2 框架提供的可排序 ActiveRecord
0.0.2
2016-08-29 08:52 UTC
Requires
- php: ~5.6|~7.0
- yiisoft/yii2: *
This package is not auto-updated.
Last update: 2024-09-16 21:36:27 UTC
README
此扩展用于排序 ActiveRecord 模型。
安装
通过 Composer
$ composer require orlac/yii2-sortable
用法
模型
use orlac\sortable\SortableBehavior; public function behaviors(){ return [ [ 'class' => SortableBehavior::className(), ], ]; } public function behaviors(){ return [ [ 'class' => SortableBehavior::className(), 'scope' => function($model){ return Model::find()->addWere('...'); } ], ]; } public function behaviors(){ return [ [ 'class' => SortableBehavior::className(), 'scope' => function($model){ return Model::find()->addWere('...'); }, 'sortColumn' => 'sort' ], ]; }
控制器
use orlac\sortable\UpAction; use orlac\sortable\DownAction; .... public function behaviors() { return [ 'verbs' => [ 'class' => VerbFilter::className(), 'actions' => [ 'delete' => ['POST'], 'up' => ['POST'], 'down' => ['POST'], ], ], ]; } public function actions(){ return [ 'up' => [ 'class' => UpAction::className(), 'modelClass' => Model::className() ], 'down' => [ 'class' => DownAction::className(), 'modelClass' => Model::className() ], ]; }
视图
use orlac\sortable\ActionColumn; <?php Pjax::begin(); ?> <?= GridView::widget([ ... 'columns' => [ ... [ 'class' => ActionColumn::className(), ], [ 'class' => ActionColumn::className(), 'template' => ($dataProvider->sort->getAttributeOrder('priority')) ? '{up} {down} {update} {delete}' : '{update} {delete}', 'order' => ($dataProvider->sort->getAttributeOrder('priority') === SORT_DESC) ? SORT_DESC : SORT_ASC, ], ], ]); ?> <?php Pjax::end(); ?>