m-comscience / yii2-data-column
Data Column for Yii2 framework
Installs: 63
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 1
Open Issues: 0
Type:yii2-extension
Requires
- yiisoft/yii2: ~2.0.14
This package is auto-updated.
Last update: 2025-03-20 03:34:08 UTC
README
Yii2 Extension
Requirements
The minimum requirement is that your Web server supports PHP 7.0 or >.
Installation
The preferred way to install this extension is through composer.
composer require m-comscience/yii2-data-column '@dev'
Usage
Controller
use mcomscience\data\DataColumn; use mcomscience\data\ActionColumn; use yii\data\ArrayDataProvider; public function ActionDataServiceGroup() { \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; $query = (new \yii\db\Query()) ->select([ 'tb_service_group.*', ]) ->from('tb_service_group') ->all(); $dataProvider = new ArrayDataProvider([ 'allModels' => $query, ]); // or /* use yii\data\ActiveDataProvider; $query = Post::find()->where(['status' => 1]); $dataProvider = new ActiveDataProvider([ 'query' => $query, 'sort' => [ 'defaultOrder' => [ 'created_at' => SORT_DESC, 'title' => SORT_ASC, ] ], ]); */ $columns = Yii::createObject([ 'class' => DataColumn::className(), 'dataProvider' => $dataProvider, 'formatter' => Yii::$app->formatter, 'columns' => [ [ 'attribute' => 'service_group_id', ], [ 'attribute' => 'service_group_name', ], [ 'attribute' => 'service_group_status', 'value' => function ($model, $key, $index) { return static::getBadgeStatus($model['service_group_status']); }, 'format' => 'raw', ], [ 'class' => ActionColumn::className(), 'template' => '{view} {update} {delete}', 'viewOptions' => [ 'title' => Yii::t('yii','View'), //'label' => 'View' ], 'updateOptions' => [ 'role' => 'modal-remote', 'title' => Yii::t('yii','Edit'), ], 'deleteOptions' => [ 'class' => 'text-danger on-delete', 'title' => Yii::t('yii','Delete'), ], 'urlCreator' => function ($action, $model, $key, $index) { if ($action == 'update') { return Url::to(['/app/settings/update-service-group', 'id' => $key]); } if ($action == 'delete') { return Url::to(['/app/settings/delete-service-group', 'id' => $key]); } }, ], /* DropdownButton [ 'class' => ActionColumn::className(), 'template' => '{view} {btn1}', 'dropdown' => true, 'dropdownButton' => [ 'label' => 'Actions', 'class' => 'btn btn-success' ], 'viewOptions' => [ 'role' => 'modal-remote', 'title' => 'Detail', 'label' => 'Detail', ], 'buttons' => [ 'btn1' => function ($url, $model, $key) { return Html::tag('li', Html::a('Add', $url, ['data-pjax' => 0])); }, ], 'urlCreator' => function ($action, $model, $key, $index) { if ($action == 'view') { return Url::to(['view', 'id' => $key]); } }, ], */ ], ]); return $columns->renderDataColumns(); }
Result
[
{
"service_group_id": 1,
"service_group_name": "Example1",
"service_group_status": "<span class=\"badge badge-success\">Active</span>",
"actions": "html format"
},
{
"service_group_id": 2,
"service_group_name": "Example2",
"service_group_status": "<span class=\"badge badge-success\">Active</span>",
"actions": "html format"
},
];