|
|
@ -13,122 +13,88 @@ use Yii; |
|
|
|
|
|
|
|
|
|
|
|
class ToggleColumn extends DataColumn |
|
|
|
class ToggleColumn extends DataColumn |
|
|
|
{ |
|
|
|
{ |
|
|
|
/** |
|
|
|
/** |
|
|
|
* Toggle action that will be used as the toggle action in your controller |
|
|
|
* Toggle action that will be used as the toggle action in your controller |
|
|
|
* @var string |
|
|
|
* @var string |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public $action = 'toggle'; |
|
|
|
public $action = 'toggle'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* @var string pk field name |
|
|
|
* @var string pk field name |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public $primaryKey = 'primaryKey'; |
|
|
|
public $primaryKey = 'primaryKey'; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Whether to use ajax or not |
|
|
|
* Whether to use ajax or not |
|
|
|
* @var bool |
|
|
|
* @var bool |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public $enableAjax = true; |
|
|
|
public $enableAjax = true; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* @var string glyphicon for 'on' value |
|
|
|
* @var string glyphicon for 'on' value |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public $iconOn = 'ok'; |
|
|
|
public $iconOn = 'ok'; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* @var string glyphicon for 'off' value |
|
|
|
* @var string glyphicon for 'off' value |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public $iconOff = 'remove'; |
|
|
|
public $iconOff = 'remove'; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* @var string text to display on the 'on' link |
|
|
|
* @var string text to display on the 'on' link |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public $onText; |
|
|
|
public $onText; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* @var string text to display on the 'off' link |
|
|
|
* @var string text to display on the 'off' link |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public $offText; |
|
|
|
public $offText; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* @var string text to display next to the 'on' link |
|
|
|
* @var string text to display next to the 'on' link |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public $displayValueText = false; |
|
|
|
public $displayValueText = false; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* @var string text to display next to the 'on' link |
|
|
|
* @var string text to display next to the 'on' link |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public $onValueText; |
|
|
|
public $onValueText; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* @var string text to display next to the 'off' link |
|
|
|
* @var string text to display next to the 'off' link |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public $offValueText; |
|
|
|
public $offValueText; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function init() |
|
|
|
public function init() |
|
|
|
{ |
|
|
|
{ |
|
|
|
if ($this->onText === null) { |
|
|
|
if ($this->onText === null) { |
|
|
|
$this->onText = Yii::t('main', 'On'); |
|
|
|
$this->onText = Yii::t('main', 'On'); |
|
|
|
} |
|
|
|
} |
|
|
|
if ($this->offText === null) { |
|
|
|
if ($this->offText === null) { |
|
|
|
$this->offText = Yii::t('main', 'Off'); |
|
|
|
$this->offText = Yii::t('main', 'Off'); |
|
|
|
} |
|
|
|
} |
|
|
|
if ($this->onValueText === null) { |
|
|
|
if ($this->onValueText === null) { |
|
|
|
$this->onValueText = Yii::t('main', 'Active'); |
|
|
|
$this->onValueText = Yii::t('main', 'Active'); |
|
|
|
} |
|
|
|
} |
|
|
|
if ($this->offValueText === null) { |
|
|
|
if ($this->offValueText === null) { |
|
|
|
$this->offValueText = Yii::t('main', 'Inactive'); |
|
|
|
$this->offValueText = Yii::t('main', 'Inactive'); |
|
|
|
} |
|
|
|
} |
|
|
|
if ($this->enableAjax) { |
|
|
|
if ($this->enableAjax) { |
|
|
|
$this->registerJs(); |
|
|
|
$this->registerJs(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* @inheritdoc |
|
|
|
* Registers the ajax JS |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
protected function renderDataCellContent($model, $key, $index) |
|
|
|
public function registerJs() |
|
|
|
{ |
|
|
|
{ |
|
|
|
$url = [$this->action, 'id' => $model->{$this->primaryKey}]; |
|
|
|
if (Yii::$app->request->isAjax) { |
|
|
|
|
|
|
|
return; |
|
|
|
$attribute = $this->attribute; |
|
|
|
} |
|
|
|
$value = $model->$attribute; |
|
|
|
$js = <<<'JS' |
|
|
|
|
|
|
|
|
|
|
|
if ($value === null || $value == true) { |
|
|
|
|
|
|
|
$icon = $this->iconOn; |
|
|
|
|
|
|
|
$title = $this->offText; |
|
|
|
|
|
|
|
$valueText = $this->onValueText; |
|
|
|
|
|
|
|
$color = 'green'; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
$icon = $this->iconOff; |
|
|
|
|
|
|
|
$title = $this->onText; |
|
|
|
|
|
|
|
$valueText = $this->offValueText; |
|
|
|
|
|
|
|
$color = 'red'; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return Html::a( |
|
|
|
|
|
|
|
'<span class="glyphicon glyphicon-' . $icon . '"></span>', |
|
|
|
|
|
|
|
$url, |
|
|
|
|
|
|
|
[ |
|
|
|
|
|
|
|
'title' => $title, |
|
|
|
|
|
|
|
'class' => 'toggle-column', |
|
|
|
|
|
|
|
'style' => 'color:' . $color, |
|
|
|
|
|
|
|
'data-method' => 'post', |
|
|
|
|
|
|
|
'data-pjax' => '0', |
|
|
|
|
|
|
|
] |
|
|
|
|
|
|
|
) . ( $this->displayValueText ? " {$valueText}" : "" ); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Registers the ajax JS |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public function registerJs() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if(Yii::$app->request->isAjax) { |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
$js = <<<'JS' |
|
|
|
|
|
|
|
$(document.body).on("click", "a.toggle-column", function(e) { |
|
|
|
$(document.body).on("click", "a.toggle-column", function(e) { |
|
|
|
e.preventDefault(); |
|
|
|
e.preventDefault(); |
|
|
|
$.post($(this).attr("href"), function(data) { |
|
|
|
$.post($(this).attr("href"), function(data) { |
|
|
@ -138,6 +104,41 @@ $(document.body).on("click", "a.toggle-column", function(e) { |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
}); |
|
|
|
}); |
|
|
|
JS; |
|
|
|
JS; |
|
|
|
$this->grid->view->registerJs($js, View::POS_READY, 'zx-toggle-column'); |
|
|
|
$this->grid->view->registerJs($js, View::POS_READY, 'zx-toggle-column'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* @inheritdoc |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
protected function renderDataCellContent($model, $key, $index) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
$url = [$this->action, 'id' => $model->{$this->primaryKey}]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$attribute = $this->attribute; |
|
|
|
|
|
|
|
$value = $model->$attribute; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ($value === null || $value == true) { |
|
|
|
|
|
|
|
$icon = $this->iconOn; |
|
|
|
|
|
|
|
$title = $this->offText; |
|
|
|
|
|
|
|
$valueText = $this->onValueText; |
|
|
|
|
|
|
|
$color = 'green'; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
$icon = $this->iconOff; |
|
|
|
|
|
|
|
$title = $this->onText; |
|
|
|
|
|
|
|
$valueText = $this->offValueText; |
|
|
|
|
|
|
|
$color = 'red'; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return Html::a( |
|
|
|
|
|
|
|
'<span class="glyphicon glyphicon-' . $icon . '"></span>', |
|
|
|
|
|
|
|
$url, |
|
|
|
|
|
|
|
[ |
|
|
|
|
|
|
|
'title' => $title, |
|
|
|
|
|
|
|
'class' => 'toggle-column', |
|
|
|
|
|
|
|
'style' => 'color:' . $color, |
|
|
|
|
|
|
|
'data-method' => 'post', |
|
|
|
|
|
|
|
'data-pjax' => '0', |
|
|
|
|
|
|
|
] |
|
|
|
|
|
|
|
) . ($this->displayValueText ? " {$valueText}" : ''); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |