* @copyright (c), Thread */ class EditableColumn extends DataColumn { /** * @var string */ public $attribute; /** * @var string */ public $link = ['attribute-save']; /** * @var null */ public $displayValue = null; /** * @throws InvalidConfigException */ public function init() { parent::init(); if (empty($this->attribute)) { throw new InvalidConfigException('The "name" property must be set.'); } } /** * @param $model * @return array|mixed|string */ protected function getLink($model) { /** * @var $model ActiveRecord */ if (!empty($this->link)) { if ($this->link instanceof \Closure) { $f = $this->link; return $f($model); } else { $r = $this->link; return $r; } } else { return $this->link; } } /** * @return bool|mixed */ public function getDisplayValue($model) { if ($this->displayValue instanceof \Closure) { $m = $this->displayValue; return $m($model); } return $model[$this->displayValue]??$model[$this->attribute]; } /** * @param mixed $model * @param mixed $key * @param int $index * @return mixed */ protected function renderDataCellContent($model, $key, $index) { /** * @var $model ActiveRecord */ $attribute = $this->attribute; return Editable::widget([ 'name' => $attribute . '[' . $model['id'] . ']', 'id' => $attribute . uniqid(), 'value' => $this->getDisplayValue($model), 'displayValue' => $this->getDisplayValue($model), 'asPopover' => false, 'header' => $model->getAttributeLabel($attribute), 'size' => 'xs', 'formOptions' => [ 'action' => Url::toRoute($this->getLink($model)), ], 'options' => [ 'class' => 'form-control', ], 'buttonsTemplate' => '{submit}' ]); } }