Browse Source

icheck基本完成

wechat_public_accounts
blobt 5 years ago
parent
commit
6e36d11581
  1. 4
      kcadmin/views/category/_form.php
  2. 4
      kcadmin/web/css/site.css
  3. 37
      vendor/blobt/widgets/Icheck.php
  4. 4
      vendor/blobt/widgets/Select2.php

4
kcadmin/views/category/_form.php

@ -19,9 +19,9 @@ use blobt\widgets\Icheck;
<?= $form->field($model, 'icon')->textInput(['maxlength' => true]) ?> <?= $form->field($model, 'icon')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'icon_type')->widget(Select2::className(), ["items" => $model::$iconType]) ?>
<?php //$form->field($model, 'icon_type')->widget(Select2::className(), ["items" => $model::$iconType, "promptText" => false]) ?>
<?php // $form->field($model, 'icon_type')->widget(Icheck::className(), ["items" => ["a" => "Option A", "b" => "Option B", "c" => "Option C"]]) ?>
<?= $form->field($model, 'icon_type')->widget(Icheck::className(), ["items" => $model::$iconType]) ?>
<?= $form->field($model, 'description')->textarea(['rows' => 6]) ?> <?= $form->field($model, 'description')->textarea(['rows' => 6]) ?>

4
kcadmin/web/css/site.css

@ -42,3 +42,7 @@
border-color: #00a65a; border-color: #00a65a;
box-shadow: none; box-shadow: none;
} }
.icheck-label-group label{
margin-right: 20px;
}

37
vendor/blobt/widgets/Icheck.php

@ -43,10 +43,20 @@ use blobt\web\IcheckAsset;
class Icheck extends InputWidget { class Icheck extends InputWidget {
/** /**
* @var var checkbox的选项
* @var array checkbox的选项
*/ */
public $items = []; public $items = [];
/**
* @var array select 的html选择
*/
public $options = [];
/**
* @var string 类型,支持checkbox或radio
*/
public $type = 'checkbox';
/** /**
* @inheritdoc * @inheritdoc
* @throws \ReflectionException * @throws \ReflectionException
@ -55,7 +65,7 @@ class Icheck extends InputWidget {
public function run() { public function run() {
$this->registerAsset(); $this->registerAsset();
$this->registerJs(); $this->registerJs();
return $this->renderCheckBox();
return $this->renderInput();
} }
/** /**
@ -63,9 +73,16 @@ class Icheck extends InputWidget {
*/ */
public function init() { public function init() {
parent::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); $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); $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 "";
} }
} }

4
vendor/blobt/widgets/Select2.php

@ -53,7 +53,7 @@ class Select2 extends InputWidget {
public $options; public $options;
/** /**
* @var string 默认显示的提示
* @var string|bool 默认显示的提示内容,如果不想显示可以设置成false
*/ */
public $promptText = '请选择'; public $promptText = '请选择';
@ -73,7 +73,7 @@ class Select2 extends InputWidget {
*/ */
public function init() { public function init() {
parent::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); $this->name = $this->options['name'] = Html::getInputName($this->model, $this->attribute);
} }

Loading…
Cancel
Save