diff --git a/kcadmin/views/category/_form.php b/kcadmin/views/category/_form.php index 422a8ea..680b4a3 100644 --- a/kcadmin/views/category/_form.php +++ b/kcadmin/views/category/_form.php @@ -19,9 +19,9 @@ use blobt\widgets\Icheck; field($model, 'icon')->textInput(['maxlength' => true]) ?> - field($model, 'icon_type')->widget(Select2::className(), ["items" => $model::$iconType]) ?> + field($model, 'icon_type')->widget(Select2::className(), ["items" => $model::$iconType, "promptText" => false]) ?> - field($model, 'icon_type')->widget(Icheck::className(), ["items" => ["a" => "Option A", "b" => "Option B", "c" => "Option C"]]) ?> + field($model, 'icon_type')->widget(Icheck::className(), ["items" => $model::$iconType]) ?> field($model, 'description')->textarea(['rows' => 6]) ?> diff --git a/kcadmin/web/css/site.css b/kcadmin/web/css/site.css index 966d765..f9c5b41 100644 --- a/kcadmin/web/css/site.css +++ b/kcadmin/web/css/site.css @@ -41,4 +41,8 @@ .form-group.has-success .select2-selection, .form-group.has-success .select2-selection { border-color: #00a65a; box-shadow: none; +} + +.icheck-label-group label{ + margin-right: 20px; } \ No newline at end of file diff --git a/vendor/blobt/widgets/Icheck.php b/vendor/blobt/widgets/Icheck.php index 0bad81f..c69a703 100644 --- a/vendor/blobt/widgets/Icheck.php +++ b/vendor/blobt/widgets/Icheck.php @@ -43,10 +43,20 @@ use blobt\web\IcheckAsset; class Icheck extends InputWidget { /** - * @var var checkbox的选项 + * @var array checkbox的选项 */ public $items = []; + /** + * @var array select 的html选择 + */ + public $options = []; + + /** + * @var string 类型,支持checkbox或radio + */ + public $type = 'checkbox'; + /** * @inheritdoc * @throws \ReflectionException @@ -55,7 +65,7 @@ class Icheck extends InputWidget { public function run() { $this->registerAsset(); $this->registerJs(); - return $this->renderCheckBox(); + return $this->renderInput(); } /** @@ -63,9 +73,16 @@ class Icheck extends InputWidget { */ public function init() { parent::init(); - if ($this->hasModel() && !isset($options['name'])) { + if ($this->hasModel() && !isset($this->options['name'])) { $this->name = $this->options['name'] = Html::getInputName($this->model, $this->attribute); + if ($this->type == 'checkbox') { + $this->name .= "[]"; + } } + + //表单默认会把css类form-control带过来,用来控制边框,这里不需边框所以去除 + $this->options["class"] = str_replace("form-control", '', $this->options["class"]); + $this->options["class"] = $this->options["class"] . ' icheck-label-group'; } /** @@ -89,14 +106,14 @@ SCRIPT; $this->getView()->registerJs($js); } - protected function renderCheckBox() { - $line = []; - foreach ($this->items as $key => $val) { - $input = Html::tag('input', $key, ["type" => "radio", "name" => $this->name]); - $line[] = Html::tag('label', $input ." ". $val); + protected function renderInput() { + if ($this->type == 'checkbox') { + return Html::activeCheckboxList($this->model, $this->attribute, $this->items, $this->options); + } + if ($this->type == 'radio') { + return Html::activeRadioList($this->model, $this->attribute, $this->items, $this->options); } - $content = implode("\n", $line); - return Html::tag('div', $content); + return ""; } } diff --git a/vendor/blobt/widgets/Select2.php b/vendor/blobt/widgets/Select2.php index fb51b44..cd4d33d 100644 --- a/vendor/blobt/widgets/Select2.php +++ b/vendor/blobt/widgets/Select2.php @@ -53,7 +53,7 @@ class Select2 extends InputWidget { public $options; /** - * @var string 默认显示的提示 + * @var string|bool 默认显示的提示内容,如果不想显示可以设置成false */ public $promptText = '请选择'; @@ -73,7 +73,7 @@ class Select2 extends InputWidget { */ public function init() { parent::init(); - if ($this->hasModel() && !isset($options['name'])) { + if ($this->hasModel() && !isset($this->options['name'])) { $this->name = $this->options['name'] = Html::getInputName($this->model, $this->attribute); }