hbhe / yii2-toggle-column
Provides a toggle data column and action
Installs: 30
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 1
Forks: 0
Open Issues: 0
Type:yii2-extension
Requires
- yiisoft/yii2: >=2.0.7
This package is not auto-updated.
Last update: 2024-06-19 08:10:56 UTC
README
Toggle data column for Yii2
Provides a toggle data column and action
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist hbhe/yii2-toggle-column "*"
or add
"hbhe/yii2-toggle-column": "*"
to the require section of your composer.json
file.
Usage
Once the extension is installed, simply use it in your code by :
// Controller文件中 use hbhe\grid\actions\ToggleAction; public function actions() { return [ 'toggle-status' => [ 'class' => ToggleAction::className(), 'onValue' => User::STATUS_ACTIVE, 'offValue' => User::STATUS_NOT_ACTIVE, 'modelClass' => 'common\models\User', 'attribute' => 'status', // Uncomment to enable flash messages 'setFlash' => true, ] ]; } // View文件中 // Pjax::begin(); GridView::widget([ 'dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [ 'id', [ 'class' => '\hbhe\grid\ToggleColumn', 'attribute' => 'status', 'action' => 'toggle-status', 'onText' => '禁用', 'offText' => '启用', 'displayValueText' => true, 'onValueText' => '已禁用', 'offValueText' => '已启用', 'iconOn' => 'stop', 'iconOff' => 'stop', 'enableAjax' => false, // 使用pjax时要设为true 'confirm' => function($model, $toggle) { if ($model->status == Member::STATUS_NOT_ACTIVE) { return "确认启用: {$model->username}({$model->id})?"; } else { return "确认禁用: {$model->username}({$model->id})?"; } }, 'headerOptions' => array('style' => 'width:80px;'), ], ], ]); // Pjax::end();