From 31675d0a28094654311c4eda9ac124b3b71c846d Mon Sep 17 00:00:00 2001 From: dongpd Date: Thu, 8 Oct 2020 17:25:53 +0700 Subject: [PATCH] refactor GridView helpers --- helpers/AreaGrid.php | 13 ++++++++++++ helpers/CommonGrid.php | 20 ++++++++++++++++++ helpers/DepartmentGrid.php | 23 --------------------- views/area/_form.php | 33 ------------------------------ views/area/_search.php | 42 -------------------------------------- views/area/create.php | 21 ------------------- views/area/index.php | 41 ------------------------------------- views/area/index.tpl | 12 +++++------ views/area/update.php | 21 ------------------- views/area/view.php | 41 ------------------------------------- views/department/index.tpl | 4 ++-- views/device/index.tpl | 4 ++-- 12 files changed, 43 insertions(+), 232 deletions(-) create mode 100644 helpers/AreaGrid.php delete mode 100644 views/area/_form.php delete mode 100644 views/area/_search.php delete mode 100644 views/area/create.php delete mode 100644 views/area/index.php delete mode 100644 views/area/update.php delete mode 100644 views/area/view.php diff --git a/helpers/AreaGrid.php b/helpers/AreaGrid.php new file mode 100644 index 00000000..639b53ae --- /dev/null +++ b/helpers/AreaGrid.php @@ -0,0 +1,13 @@ +pid]) ? $departmentArray[$model->pid] : ""; + }; + } + +} diff --git a/helpers/CommonGrid.php b/helpers/CommonGrid.php index 75688fc9..7c7ec76d 100644 --- a/helpers/CommonGrid.php +++ b/helpers/CommonGrid.php @@ -62,4 +62,24 @@ class CommonGrid { }; } + public static function checkbox($type) { + return function($model) use ($type) { + if ($model->id == 1) + return ""; + return ""; + }; + } + + public static function rows($type) { + return function($model, $index, $widget, $grid) use ($type) { + return [ + "onclick" => "common.form(this, '{$type}');", + "style" => "cursor: pointer;", + "data" => [ + "href" => Url::to(["update", "id" => $model->id]) + ] + ]; + }; + } + } diff --git a/helpers/DepartmentGrid.php b/helpers/DepartmentGrid.php index 19680f33..6bff3f08 100644 --- a/helpers/DepartmentGrid.php +++ b/helpers/DepartmentGrid.php @@ -2,9 +2,6 @@ namespace app\helpers; -use yii\helpers\Html; -use yii\helpers\Url; - class DepartmentGrid extends CommonGrid { public static function pid($departmentArray) { @@ -13,24 +10,4 @@ class DepartmentGrid extends CommonGrid { }; } - public static function checkbox() { - return function($model) { - if ($model->id == 1) - return ""; - return ""; - }; - } - - public static function rows() { - return function($model, $index, $widget, $grid) { - return [ - "onclick" => "common.form(this, 'department');", - "style" => "cursor: pointer;", - "data" => [ - "href" => Url::to(["update", "id" => $model->id]) - ] - ]; - }; - } - } diff --git a/views/area/_form.php b/views/area/_form.php deleted file mode 100644 index 688a772b..00000000 --- a/views/area/_form.php +++ /dev/null @@ -1,33 +0,0 @@ - - -
- - - - field($model, 'code')->textInput() ?> - - field($model, 'pid')->textInput() ?> - - field($model, 'name')->textInput(['maxlength' => true]) ?> - - field($model, 'description')->textarea(['rows' => 6]) ?> - - field($model, 'created_at')->textInput() ?> - - field($model, 'modified_at')->textInput() ?> - -
- 'btn btn-success']) ?> -
- - - -
diff --git a/views/area/_search.php b/views/area/_search.php deleted file mode 100644 index c086554e..00000000 --- a/views/area/_search.php +++ /dev/null @@ -1,42 +0,0 @@ - - - diff --git a/views/area/create.php b/views/area/create.php deleted file mode 100644 index b761aa06..00000000 --- a/views/area/create.php +++ /dev/null @@ -1,21 +0,0 @@ -title = 'Create Area'; -$this->params['breadcrumbs'][] = ['label' => 'Areas', 'url' => ['index']]; -$this->params['breadcrumbs'][] = $this->title; -?> -
- -

title) ?>

- - render('_form', [ - 'model' => $model, - ]) ?> - -
diff --git a/views/area/index.php b/views/area/index.php deleted file mode 100644 index eb3dda80..00000000 --- a/views/area/index.php +++ /dev/null @@ -1,41 +0,0 @@ -title = 'Areas'; -$this->params['breadcrumbs'][] = $this->title; -?> -
- -

title) ?>

- - render('_search', ['model' => $searchModel]); ?> - -

- 'btn btn-success']) ?> -

- - $dataProvider, - 'filterModel' => $searchModel, - 'columns' => [ - ['class' => 'yii\grid\SerialColumn'], - - 'id', - 'code', - 'pid', - 'name', - 'description:ntext', - //'created_at', - //'modified_at', - - ['class' => 'yii\grid\ActionColumn'], - ], - ]); ?> - -
diff --git a/views/area/index.tpl b/views/area/index.tpl index 4efb02a7..d24f2988 100644 --- a/views/area/index.tpl +++ b/views/area/index.tpl @@ -30,12 +30,12 @@ {GridView::widget([ 'dataProvider' => $dataProvider, 'filterModel' => $searchModel, - 'layout'=> \app\helpers\DepartmentGrid::getLayout(), + 'layout'=> \app\helpers\AreaGrid::getLayout(), 'tableOptions' => [ 'class' => 'table table-striped table-bordered table-hover', 'style' => 'background:#fff;min-width:700px;' ], - 'rowOptions' => \app\helpers\DepartmentGrid::rows(), + 'rowOptions' => \app\helpers\AreaGrid::rows("area"), 'columns' => [ [ 'class' => 'yii\grid\SerialColumn', @@ -43,11 +43,11 @@ 'headerOptions' => ['class' => 'text-center', 'style' => 'width:3%'] ], [ - 'header' => "", + 'header' => "", 'format' => 'raw', 'contentOptions' => ['class' => 'text-center'], 'headerOptions' => ['class' => 'text-center', 'style' => 'width:3%'], - 'value' => \app\helpers\DepartmentGrid::checkbox() + 'value' => \app\helpers\AreaGrid::checkbox("area") ], 'code', 'pid', @@ -55,11 +55,11 @@ 'description:ntext', [ 'attribute' => 'created_at', - 'value' => \app\helpers\DepartmentGrid::createdAt() + 'value' => \app\helpers\AreaGrid::createdAt() ], [ 'attribute' => 'modified_at', - 'value' => \app\helpers\DepartmentGrid::modifiedAt() + 'value' => \app\helpers\AreaGrid::modifiedAt() ] ] ])} diff --git a/views/area/update.php b/views/area/update.php deleted file mode 100644 index aecd227c..00000000 --- a/views/area/update.php +++ /dev/null @@ -1,21 +0,0 @@ -title = 'Update Area: ' . $model->name; -$this->params['breadcrumbs'][] = ['label' => 'Areas', 'url' => ['index']]; -$this->params['breadcrumbs'][] = ['label' => $model->name, 'url' => ['view', 'id' => $model->id]]; -$this->params['breadcrumbs'][] = 'Update'; -?> -
- -

title) ?>

- - render('_form', [ - 'model' => $model, - ]) ?> - -
diff --git a/views/area/view.php b/views/area/view.php deleted file mode 100644 index eb5cea84..00000000 --- a/views/area/view.php +++ /dev/null @@ -1,41 +0,0 @@ -title = $model->name; -$this->params['breadcrumbs'][] = ['label' => 'Areas', 'url' => ['index']]; -$this->params['breadcrumbs'][] = $this->title; -?> -
- -

title) ?>

- -

- $model->id], ['class' => 'btn btn-primary']) ?> - $model->id], [ - 'class' => 'btn btn-danger', - 'data' => [ - 'confirm' => 'Are you sure you want to delete this item?', - 'method' => 'post', - ], - ]) ?> -

- - $model, - 'attributes' => [ - 'id', - 'code', - 'pid', - 'name', - 'description:ntext', - 'created_at', - 'modified_at', - ], - ]) ?> - -
diff --git a/views/department/index.tpl b/views/department/index.tpl index 5c0953a4..18b6e6c4 100644 --- a/views/department/index.tpl +++ b/views/department/index.tpl @@ -42,7 +42,7 @@ 'class' => 'table table-striped table-bordered table-hover', 'style' => 'background:#fff;min-width:700px;' ], - 'rowOptions' => \app\helpers\DepartmentGrid::rows(), + 'rowOptions' => \app\helpers\DepartmentGrid::rows("department"), 'columns' => [ [ 'class' => 'yii\grid\SerialColumn', @@ -54,7 +54,7 @@ 'format' => 'raw', 'contentOptions' => ['class' => 'text-center'], 'headerOptions' => ['class' => 'text-center', 'style' => 'width:3%'], - 'value' => \app\helpers\DepartmentGrid::checkbox() + 'value' => \app\helpers\DepartmentGrid::checkbox("department") ], 'code', 'name', diff --git a/views/device/index.tpl b/views/device/index.tpl index c588b8aa..d5eee435 100644 --- a/views/device/index.tpl +++ b/views/device/index.tpl @@ -42,7 +42,7 @@ 'class' => 'table table-striped table-bordered table-hover', 'style' => 'background:#fff;min-width:700px;' ], - 'rowOptions' => \app\helpers\DepartmentGrid::rows(), + 'rowOptions' => \app\helpers\DepartmentGrid::rows("device"), 'columns' => [ [ 'class' => 'yii\grid\SerialColumn', @@ -54,7 +54,7 @@ 'format' => 'raw', 'contentOptions' => ['class' => 'text-center'], 'headerOptions' => ['class' => 'text-center', 'style' => 'width:3%'], - 'value' => \app\helpers\DepartmentGrid::checkbox() + 'value' => \app\helpers\DepartmentGrid::checkbox("device") ], 'name', 'ip_address',