Created
August 9, 2017 16:09
-
-
Save sanchezzzhak/3cc9353d5bb5d786658485f43c257520 to your computer and use it in GitHub Desktop.
GridView closure options test example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
interface IStat { | |
const GROUP_HOUR = 'hour'; | |
} | |
class StatGrid extends Object | |
{ | |
/** @var Stat */ | |
public $model; | |
/** @var SqlDataProvider */ | |
public $provider; | |
/** @var string */ | |
public $group; | |
public $userDashboard = false; | |
public $countryName = false; | |
public $countryImage = true; | |
// ============================================ | |
// render columns | |
// ============================================ | |
public function renderLinkFilter($action, $text, $dateFrom, $dateTo) | |
{ | |
$formName = $this->model->formName(); | |
return Html::a($text, [ | |
$action, | |
$formName . '[dateFrom]' => $dateFrom, | |
$formName . '[dateTo]' => $dateTo, | |
$formName . '[user_id]' => $this->model->user_id, | |
// .. old attributes | |
]); | |
} | |
public function getDataContentCompare($data, $key, $index, $column) | |
{ | |
if ($this->group != IStat::GROUP_HOUR) { | |
return []; | |
} | |
$dataKey = $key; | |
$dataCurrent = $data[$dataKey]; | |
return [ | |
'data-date' => $data['event_date'] . ' ' . $data['hour'], | |
'data-type' => $dataKey, | |
'data-current' => $dataCurrent, | |
'data-filters' => json_encode($this->model->getAttributes()), | |
]; | |
} | |
public function renderTextArea($content) | |
{ | |
return \yii\helpers\Html::textarea('', $content, [ | |
'style' => 'overflow:hidden; width:100px; border:0; background: none; resize: none;', | |
'onclick' => 'this.focus();this.select()', | |
]); | |
} | |
public function getDataCellDateValue($data, $key, $index) | |
{ | |
if ($this->group != IStat::GROUP_DATE ) { | |
return $data['event_date']; | |
} | |
return $this->renderLinkFilter('hour', $data['event_date'], $data['event_date'] . ' 00:00:00' , $data['event_date'] . ' 23:59:59'); | |
} | |
public function getDataCellHourValue($data, $key, $index) | |
{ | |
if ($this->group != IStat::GROUP_HOUR) { | |
return $data['hour'] . ':00'; | |
} | |
$hour = $data['hour']. ':00'; | |
$dateFrom = $data['event_date'] . ' '. str_pad($data['hour'], 2, '0', STR_PAD_LEFT) . ':00:00'; | |
$dateTo = $data['event_date'] . ' '. str_pad($data['hour'], 2, '0', STR_PAD_LEFT) .':59:59'; | |
return $this->renderLinkFilter('minute', $hour , $dateFrom , $dateTo ); | |
} | |
// ============================================ | |
/** | |
* get columns from GridView | |
* @return DataColumn[] | |
*/ | |
public function getGridViewColumns() | |
{ | |
$columns = [ | |
'date' => [ | |
'label' => Yii::t('app', 'Date'), | |
'attribute' => 'event_date', | |
'format' => 'raw', | |
'value' => [$this, 'getDataCellDateValue'], /** @see getDataCellDateValue */ | |
], | |
'hour' => [ | |
'label' => Yii::t('app', 'Hour'), | |
'attribute' => 'hour', | |
'format' => 'raw', | |
'value' => [$this, 'getDataCellHourValue'], /** @see getDataCellHourValue */ | |
], | |
'minute' => [ | |
'label' => Yii::t('app', 'Hour'), | |
'attribute' => 'minute', | |
'format' => 'raw', | |
'value' => [$this, 'getDataCellMinuteValue'], /** @see getDataCellMinuteValue */ | |
], | |
'click_sum' => [ | |
'label' => Yii::t('app', 'Hits'), | |
'format' => 'raw', | |
'attribute' => 'click_sum', | |
'contentOptions' => [$this, 'getDataContentCompare'], /** @see getDataContentCompare */ | |
], | |
// ... hide more columns (26) | |
] | |
return $this->removeColumnsByGroup($columns) | |
} | |
public function removeColumnsByGroup($columns) | |
{ | |
$removeColumns = [ | |
'hour', 'minute' | |
]; | |
if ($group == IStat::GROUP_HOUR) { | |
$removeColumns = array_diff($removeColumns, ['hour', 'date']); | |
} | |
foreach ($removeColumns as $column) { | |
ArrayHelper::remove($columns, $column); | |
} | |
return $removeColumns; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$statHelper = new \app\models\stat\StatGrid([ | |
'provider' => $provider, | |
'group' => $group, | |
'model' => $model, | |
]); | |
$columns = $statHelper->getGridViewColumns(); | |
echo GridView::widget([ | |
'id' => 'stat-' . $this->context->action->id, | |
'showFooter' => true, | |
'options' => [ | |
'class' => 'table-responsive stat-grid' | |
], | |
'dataProvider' => $provider, | |
'columns' => $columns | |
]); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment