Skip to content

Commit

Permalink
Add checkbox field to GridInput
Browse files Browse the repository at this point in the history
  • Loading branch information
mdmunir committed Oct 20, 2020
1 parent 1ef3c4c commit 28bad57
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 14 deletions.
61 changes: 47 additions & 14 deletions DataColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -27,7 +27,7 @@ class DataColumn extends Column
public $inputOptions = ['class' => 'form-control'];

/**
* @var array|\Closure
* @var array|Closure
*/
public $items;

Expand All @@ -44,6 +44,12 @@ class DataColumn extends Column
*/
public $widget;

/**
*
* @var string
*/
public $type = 'text';

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -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 {
Expand All @@ -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);
}
}

Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ Usage
2 => 'Dozen'
]
],
[
'attribute' => 'tax',
'type' => 'checkbox',
],
['class' => 'mdm\widgets\ButtonColumn']
],
]);
Expand Down
1 change: 1 addition & 0 deletions TabularWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ protected function getClientOptions()
$this->form->attributes = $oldAttrs;
}
$js = [];
ksort($view->js);
foreach ($view->js as $pieces) {
$js[] = implode("\n", $pieces);
}
Expand Down

0 comments on commit 28bad57

Please sign in to comment.