From 28bad57ca8a1d87ea10e11f9507527589fd723e4 Mon Sep 17 00:00:00 2001 From: mdmunir Date: Tue, 20 Oct 2020 12:05:25 +0700 Subject: [PATCH] Add checkbox field to GridInput --- DataColumn.php | 61 ++++++++++++++++++++++++++++++++++++----------- README.md | 4 ++++ TabularWidget.php | 1 + 3 files changed, 52 insertions(+), 14 deletions(-) diff --git a/DataColumn.php b/DataColumn.php index ce4c9c0..98c9439 100644 --- a/DataColumn.php +++ b/DataColumn.php @@ -2,9 +2,9 @@ namespace mdm\widgets; -use Yii; -use yii\helpers\Html; +use Closure; use yii\base\Model; +use yii\helpers\Html; use yii\helpers\Inflector; use yii\widgets\ActiveForm; @@ -27,7 +27,7 @@ class DataColumn extends Column public $inputOptions = ['class' => 'form-control']; /** - * @var array|\Closure + * @var array|Closure */ public $items; @@ -44,6 +44,12 @@ class DataColumn extends Column */ public $widget; + /** + * + * @var string + */ + public $type = 'text'; + /** * @inheritdoc */ @@ -92,7 +98,7 @@ public function renderInputCell($model, $key, $index) if ($this->widget !== null) { if (is_array($this->widget)) { list($widget, $options) = $this->widget; - if ($options instanceof \Closure) { + if ($options instanceof Closure) { $options = call_user_func($options, $model, $key, $index); } } else { @@ -110,21 +116,48 @@ public function renderInputCell($model, $key, $index) return $widget::widget($options); } } elseif ($items !== null) { - if ($items instanceof \Closure) { + if ($items instanceof Closure) { $items = call_user_func($items, $model, $key, $index); } - if ($form instanceof ActiveForm) { - return $form->field($model, "[$key]{$this->attribute}", ['template' => $this->template]) - ->dropDownList($items, $this->inputOptions); - } else { - return Html::activeDropDownList($model, "[$key]{$this->attribute}", $items, $this->inputOptions); + switch ($this->type) { + case 'checkbox': + if ($form instanceof ActiveForm) { + return $form->field($model, "[$key]{$this->attribute}", ['template' => $this->template]) + ->checkboxList($items, $this->inputOptions); + } else { + return Html::activeCheckboxList($model, "[$key]{$this->attribute}", $items, $this->inputOptions); + } + break; + + default: + if ($form instanceof ActiveForm) { + return $form->field($model, "[$key]{$this->attribute}", ['template' => $this->template]) + ->dropDownList($items, $this->inputOptions); + } else { + return Html::activeDropDownList($model, "[$key]{$this->attribute}", $items, $this->inputOptions); + } + break; } } else { - if ($form instanceof ActiveForm) { - return $form->field($model, "[$key]{$this->attribute}", ['template' => $this->template]) - ->textInput($this->inputOptions); + switch ($this->type) { + case 'checkbox': + if ($form instanceof ActiveForm) { + return $form->field($model, "[$key]{$this->attribute}", ['template' => $this->template]) + ->checkbox($this->inputOptions, false); + } else { + return Html::activeCheckbox($model, "[$key]{$this->attribute}", $this->inputOptions); + } + break; + + default: + if ($form instanceof ActiveForm) { + return $form->field($model, "[$key]{$this->attribute}", ['template' => $this->template]) + ->textInput($this->inputOptions); + } else { + return Html::activeTextInput($model, "[$key]{$this->attribute}", $this->inputOptions); + } + break; } - return Html::activeTextInput($model, "[$key]{$this->attribute}", $this->inputOptions); } } diff --git a/README.md b/README.md index 9b656d0..128328a 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,10 @@ Usage 2 => 'Dozen' ] ], + [ + 'attribute' => 'tax', + 'type' => 'checkbox', + ], ['class' => 'mdm\widgets\ButtonColumn'] ], ]); diff --git a/TabularWidget.php b/TabularWidget.php index b9ac93c..45ac42e 100644 --- a/TabularWidget.php +++ b/TabularWidget.php @@ -209,6 +209,7 @@ protected function getClientOptions() $this->form->attributes = $oldAttrs; } $js = []; + ksort($view->js); foreach ($view->js as $pieces) { $js[] = implode("\n", $pieces); }