onText === null) { $this->onText = Yii::t('main', 'On'); } if ($this->offText === null) { $this->offText = Yii::t('main', 'Off'); } if ($this->onValueText === null) { $this->onValueText = Yii::t('main', 'Active'); } if ($this->offValueText === null) { $this->offValueText = Yii::t('main', 'Inactive'); } if ($this->enableAjax) { $this->registerJs(); } } /** * Registers the ajax JS */ public function registerJs() { if (Yii::$app->request->isAjax) { return; } $js = <<<'JS' $(document.body).on("click", "a.toggle-column", function(e) { e.preventDefault(); $.post($(this).attr("href"), function(data) { var pjaxId = $(e.target).closest("[data-pjax-container]").attr("id"); $.pjax.reload({container:"#" + pjaxId}); }); return false; }); JS; $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( '', $url, [ 'title' => $title, 'class' => 'toggle-column', 'style' => 'color:' . $color, 'data-method' => 'post', 'data-pjax' => '0', ] ) . ($this->displayValueText ? " {$valueText}" : ''); } }