Exemplo n.º 1
0
    /**
     * @param mixed $model
     * @param mixed $key
     * @param int $index
     *
     * @return string
     *
     * @throws Exception
     */
    protected function renderDataCellContent($model, $key, $index)
    {
        if ($this->checkboxOptions instanceof Closure) {
            $options = call_user_func($this->checkboxOptions, $model, $key, $index, $this);
        } else {
            $options = $this->checkboxOptions;
            if (!isset($options['value'])) {
                $options['value'] = is_array($key) ? Json::encode($key) : $key;
            }
        }
        $options['id'] = ArrayHelper::getValue($options, 'id', rtrim($this->name, '[]') . '-' . $key);
        return Checkbox::widget(['name' => $this->name, 'checked' => !empty($options['checked']), 'inputOptions' => $options, 'clientOptions' => ['fireOnInit' => true, 'onChange' => new JsExpression('function() {
                    var $parentCheckbox = $(".select-on-check-all").closest(".checkbox"),
                    $checkbox = $("#' . $this->grid->options['id'] . '").find("input[name=\'' . $this->name . '\']").closest(".checkbox"),
                    allChecked = true,
                    allUnchecked = true;

                    $checkbox.each(function() {
                        if ($(this).checkbox("is checked")) {
                            allUnchecked = false;
                        } else {
                            allChecked = false;
                        }
                    });

                    if(allChecked) {
                        $parentCheckbox.checkbox("set checked");
                    } else if(allUnchecked) {
                        $parentCheckbox.checkbox("set unchecked");
                    } else {
                        $parentCheckbox.checkbox("set indeterminate");
                    }
                }')]]);
    }
Exemplo n.º 2
0
 /**
  * @return callable
  */
 public function getDefaultItem()
 {
     return function ($index, $label, $name, $checked, $value) {
         $inputOptions = $this->inputOptions;
         $inputOptions['value'] = ArrayHelper::getValue($inputOptions, 'value', $value);
         return Html::tag('div', Checkbox::widget(['name' => $name, 'type' => $this->type, 'label' => $this->encodeLabel ? Html::encode($label) : $label, 'readOnly' => $this->readOnly, 'disabled' => $this->disabled, 'fitted' => $this->fitted, 'checked' => $checked, 'inputOptions' => $inputOptions, 'labelOptions' => $this->labelOptions]), ['class' => 'field']);
     };
 }
Exemplo n.º 3
0
 public function init()
 {
     $this->type = self::TYPE_RADIO;
     parent::init();
 }