refactor GridView helpers

This commit is contained in:
dongpd 2020-10-08 17:25:53 +07:00
parent 0273aa2179
commit 31675d0a28
12 changed files with 43 additions and 232 deletions

13
helpers/AreaGrid.php Normal file
View File

@ -0,0 +1,13 @@
<?php
namespace app\helpers;
class AreaGrid extends CommonGrid {
public static function pid($departmentArray) {
return function($model) use ($departmentArray) {
return isset($departmentArray[$model->pid]) ? $departmentArray[$model->pid] : "";
};
}
}

View File

@ -62,4 +62,24 @@ class CommonGrid {
};
}
public static function checkbox($type) {
return function($model) use ($type) {
if ($model->id == 1)
return "";
return "<input type='checkbox' value='{$model->id}' name='checkbox-{$type}' class='checkbox-{$type}'>";
};
}
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])
]
];
};
}
}

View File

@ -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 "<input type='checkbox' value='{$model->id}' name='checkbox-department' class='checkbox-department'>";
};
}
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])
]
];
};
}
}

View File

@ -1,33 +0,0 @@
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $model app\models\Area */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="area-form">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'code')->textInput() ?>
<?= $form->field($model, 'pid')->textInput() ?>
<?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'description')->textarea(['rows' => 6]) ?>
<?= $form->field($model, 'created_at')->textInput() ?>
<?= $form->field($model, 'modified_at')->textInput() ?>
<div class="form-group">
<?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>

View File

@ -1,42 +0,0 @@
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $model app\models\AreaSearch */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="area-search">
<?php $form = ActiveForm::begin([
'action' => ['index'],
'method' => 'get',
'options' => [
'data-pjax' => 1
],
]); ?>
<?= $form->field($model, 'id') ?>
<?= $form->field($model, 'code') ?>
<?= $form->field($model, 'pid') ?>
<?= $form->field($model, 'name') ?>
<?= $form->field($model, 'description') ?>
<?php // echo $form->field($model, 'created_at') ?>
<?php // echo $form->field($model, 'modified_at') ?>
<div class="form-group">
<?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
<?= Html::resetButton('Reset', ['class' => 'btn btn-default']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>

View File

@ -1,21 +0,0 @@
<?php
use yii\helpers\Html;
/* @var $this yii\web\View */
/* @var $model app\models\Area */
$this->title = 'Create Area';
$this->params['breadcrumbs'][] = ['label' => 'Areas', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="area-create">
<h1><?= Html::encode($this->title) ?></h1>
<?= $this->render('_form', [
'model' => $model,
]) ?>
</div>

View File

@ -1,41 +0,0 @@
<?php
use yii\helpers\Html;
use yii\grid\GridView;
use yii\widgets\Pjax;
/* @var $this yii\web\View */
/* @var $searchModel app\models\AreaSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */
$this->title = 'Areas';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="area-index">
<h1><?= Html::encode($this->title) ?></h1>
<?php Pjax::begin(); ?>
<?php // echo $this->render('_search', ['model' => $searchModel]); ?>
<p>
<?= Html::a('Create Area', ['create'], ['class' => 'btn btn-success']) ?>
</p>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'code',
'pid',
'name',
'description:ntext',
//'created_at',
//'modified_at',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
<?php Pjax::end(); ?>
</div>

View File

@ -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' => "<input type='checkbox' value='0' class='checkbox-department' id='checkall-department'>",
'header' => "<input type='checkbox' value='0' class='checkbox-area' id='checkall-area'>",
'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()
]
]
])}

View File

@ -1,21 +0,0 @@
<?php
use yii\helpers\Html;
/* @var $this yii\web\View */
/* @var $model app\models\Area */
$this->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';
?>
<div class="area-update">
<h1><?= Html::encode($this->title) ?></h1>
<?= $this->render('_form', [
'model' => $model,
]) ?>
</div>

View File

@ -1,41 +0,0 @@
<?php
use yii\helpers\Html;
use yii\widgets\DetailView;
/* @var $this yii\web\View */
/* @var $model app\models\Area */
$this->title = $model->name;
$this->params['breadcrumbs'][] = ['label' => 'Areas', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="area-view">
<h1><?= Html::encode($this->title) ?></h1>
<p>
<?= Html::a('Update', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
<?= Html::a('Delete', ['delete', 'id' => $model->id], [
'class' => 'btn btn-danger',
'data' => [
'confirm' => 'Are you sure you want to delete this item?',
'method' => 'post',
],
]) ?>
</p>
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'id',
'code',
'pid',
'name',
'description:ntext',
'created_at',
'modified_at',
],
]) ?>
</div>

View File

@ -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',

View File

@ -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',