|
|
<?php
/* * The MIT License * * Copyright 2019 Blobt. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */
namespace blobt\widgets;
use yii\widgets\InputWidget; use Yii; use yii\base\InvalidConfigException; use yii\helpers\ArrayHelper; use yii\helpers\Html; use blobt\web\IcheckAsset;
/** * Description of Icheck * * @author Blobt * @email 380255922@qq.com * @created Sep 12, 2019 */ class Icheck extends InputWidget {
/** * @var array checkbox的选项 */ public $items = [];
/** * @var array select 的html选择 */ public $options = [];
/** * @var string 类型,支持checkbox或radio */ public $type = 'checkbox';
/** * @inheritdoc * @throws \ReflectionException * @throws \yii\base\InvalidConfigException */ public function run() { $this->registerAsset(); $this->registerJs(); return $this->renderInput(); }
/** * 初始化 */ public function init() { parent::init(); 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'; }
/** * 注册js和css */ protected function registerAsset() { $view = $this->getView(); IcheckAsset::register($view); }
/** * 注册js */ protected function registerJs() { $js = <<<SCRIPT $("input[name='{$this->name}']").iCheck({ checkboxClass: 'icheckbox_flat-blue', radioClass: 'iradio_flat-blue', }); SCRIPT; $this->getView()->registerJs($js); }
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); } return ""; }
}
|