|
@ -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 ""; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |