rootlocal / yii2-sortable-grid-view
Sortable Grid View
Installs: 11
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 3
Forks: 0
Open Issues: 0
Type:yii2-extension
Requires
- php: >=7.4||^8.0||^8.1
- yiisoft/yii2: ^2.0
- yiisoft/yii2-jui: ~2.0.0
Requires (Dev)
- codeception/codeception: ^4.1
- codeception/module-asserts: ^2.0
- codeception/module-filesystem: ^1.0
- codeception/module-phpbrowser: ^1.0
- codeception/module-yii2: ^1.1
- codeception/verify: ^2.2
- pdepend/pdepend: ^2.10
- phploc/phploc: ^7.0
- phpmd/phpmd: ^2.12
- sebastian/phpcpd: ^6.0
- squizlabs/php_codesniffer: 3.*
- symfony/browser-kit: >=2.7 <=4.2.4
- yiisoft/yii2-bootstrap: ~2.0.0
- yiisoft/yii2-faker: ~2.0.0
README
install
composer require rootlocal/yii2-sortable-grid-view
or added composer.json:
"rootlocal/yii2-sortable-grid-view": "~2.0"
Model Behavior
<?php namespace common\models; use rootlocal\widgets\sortable\SortableGridBehavior; class Book extends ActiveRecord { // ... /** * {@inheritDoc} */ public function behaviors(): array { return [ 'sort' => ['class' => SortableGridBehavior::class], // ... ]; } }
Query
<?php namespace common\models; use rootlocal\widgets\sortable\SortableGridQueryTrait; use yii\db\ActiveQuery; /** * This is the ActiveQuery class for [[Book]]. * * @see Book * @mixin SortableGridQueryTrait */ class BookQuery extends ActiveQuery { use SortableGridQueryTrait; // ... }
Controller
/** * {@inheritDoc} */ public function actions(): array { return [ 'sort' => [ 'class' => SortableGridAction::class, 'model' => Book::class, ] ]; }
Sorting
<?php $query = Book->find()->sortByOrder();
Example
<?php use common\models\BookSearch; use rootlocal\widgets\sortable\SortableGridColumnWidget; use yii\data\ActiveDataProvider; use yii\grid\GridView; use yii\web\View; /** * @var View $this * @var BookSearch $searchModel * @var ActiveDataProvider $dataProvider */ <?= GridView::widget([ 'dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [ [ 'class' => SortableGridColumnWidget::class, 'sortableWidgetOptions' => ['sortValueSelector' => '.sort_order'], ], // ... [ 'attribute' => 'sort_order', 'format' => 'raw', 'value' => fn(BookSearch $model) => Html::tag('span', $model->sort_order, [ 'class' => 'sort_order', ]), ], ]]) ?>
Extended config
<?php use common\models\BookSearch; use rootlocal\widgets\sortable\SortableGridColumnWidget; use yii\data\ActiveDataProvider; use yii\grid\GridView; use yii\web\View; use yii\helpers\Url; /** * @var View $this * @var BookSearch $searchModel * @var ActiveDataProvider $dataProvider */ <?= GridView::widget([ 'dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [ [ 'class' => SortableGridColumnWidget::class, 'template' => '{sort} {test}', 'sortableWidgetOptions' => [ // Sort action 'sortableAction' => Url::to(['/book/sort']) ], 'buttons' => [ 'test' => fn(BookSearch $model, $key) => '<i class="fa fa-address-book"></i>', ], 'visibleButtons' => [ 'test' => fn(BookSearch $model) => $model->status === $model::STATUS_TEST10, ], ], // ... ]]) ?>