TbHtml::checkBoxList PHP Method

checkBoxList() public static method

Generates a check box list.
public static checkBoxList ( string $name, mixed $select, array $data, array $htmlOptions = [] ) : string
$name string name of the check box list.
$select mixed selection of the check boxes.
$data array $data value-label pairs used to generate the check box list.
$htmlOptions array additional HTML attributes.
return string the generated list.
    public static function checkBoxList($name, $select, $data, $htmlOptions = array())
    {
        $inline = TbArray::popValue('inline', $htmlOptions, false);
        $separator = TbArray::popValue('separator', $htmlOptions, ' ');
        $container = TbArray::popValue('container', $htmlOptions, 'div');
        $containerOptions = TbArray::popValue('containerOptions', $htmlOptions, array());
        $labelOptions = TbArray::popValue('labelOptions', $htmlOptions, array());
        if (substr($name, -2) !== '[]') {
            $name .= '[]';
        }
        $checkAll = TbArray::popValue('checkAll', $htmlOptions);
        $checkAllLast = TbArray::popValue('checkAllLast', $htmlOptions);
        if ($checkAll !== null) {
            $checkAllLabel = $checkAll;
            $checkAllLast = $checkAllLast !== null;
        }
        $items = array();
        $baseID = $containerOptions['id'] = TbArray::popValue('baseID', $htmlOptions, parent::getIdByName($name));
        $id = 0;
        $checkAll = true;
        foreach ($data as $value => $label) {
            $checked = !is_array($select) && !strcmp($value, $select) || is_array($select) && in_array($value, $select);
            $checkAll = $checkAll && $checked;
            $htmlOptions['value'] = $value;
            $htmlOptions['id'] = $baseID . '_' . $id++;
            if ($inline) {
                $htmlOptions['label'] = $label;
                self::addCssClass('checkbox-inline', $labelOptions);
                $htmlOptions['labelOptions'] = $labelOptions;
                $items[] = self::checkBox($name, $checked, $htmlOptions);
            } else {
                $option = self::checkBox($name, $checked, $htmlOptions);
                $items[] = self::tag('div', array('class' => 'checkbox'), self::label($option . ' ' . $label, false, $labelOptions));
            }
        }
        if (isset($checkAllLabel)) {
            $htmlOptions['value'] = 1;
            $htmlOptions['id'] = $id = $baseID . '_all';
            $htmlOptions['label'] = $checkAllLabel;
            $htmlOptions['labelOptions'] = $labelOptions;
            $item = self::checkBox($id, $checkAll, $htmlOptions);
            if ($inline) {
                self::addCssClass('checkbox-inline', $labelOptions);
            } else {
                $item = self::checkBox($id, $checkAll, $htmlOptions);
                $item = self::tag('div', array('class' => 'checkbox'), $item);
            }
            if ($checkAllLast) {
                $items[] = $item;
            } else {
                array_unshift($items, $item);
            }
            $name = strtr($name, array('[' => '\\[', ']' => '\\]'));
            $js = <<<EOD
jQuery('#{$id}').on('click', function() {
\tjQuery("input[name='{$name}']").prop('checked', this.checked);
});
jQuery("input[name='{$name}']").on('click', function() {
\tjQuery('#{$id}').prop('checked', !jQuery("input[name='{$name}']:not(:checked)").length);
});
jQuery('#{$id}').prop('checked', !jQuery("input[name='{$name}']:not(:checked)").length);
EOD;
            $cs = Yii::app()->getClientScript();
            $cs->registerCoreScript('jquery');
            $cs->registerScript($id, $js);
        }
        $inputs = implode($separator, $items);
        return !empty($container) ? self::tag($container, $containerOptions, $inputs) : $inputs;
    }

Usage Example

			<?php 
echo TbHtml::checkBoxList('checkGames', '', array('6.1, 6.2, 6.3, 6.4' => CHtml::encode('Does your app is a game?')));
?>
		<p><?php 
echo TbHtml::b('Contracts and Extensions');
?>
</p>
                        <?php 
echo TbHtml::checkBoxList('checkContracts', '', array('7.1, 7.2' => CHtml::encode('Does your app is a share source?')));
?>
		<p><?php 
echo TbHtml::b('Audio and Video');
?>
</p>
			<?php 
echo TbHtml::checkBoxList('checkAudioAndVideo', '', array('9.1, 9.2' => CHtml::encode('Does your app is a music app?'), '9.3' => CHtml::encode('Does your app use voice call?'), '9.4' => CHtml::encode('Does your app have sounds?')));
?>
                
	</div>

 <?php 
echo TbHtml::button('Back', array('onclick' => 'js:document.location.href="/mtcontrool/runs/create"', 'color' => TbHtml::BUTTON_COLOR_DEFAULT, 'size' => TbHtml::BUTTON_SIZE_LARGE));
?>
	 <?php 
echo TbHtml::link('Submit', array('runs/saveWPQuest', 'id' => $idRuns), array('class' => 'btn btn-success btn-large'));
?>
 

 <?php 
echo TbHtml::button('Cancel', array('onclick' => 'js:document.location.href="/mtcontrool"', 'color' => TbHtml::BUTTON_COLOR_DANGER, 'size' => TbHtml::BUTTON_SIZE_LARGE));
?>
All Usage Examples Of TbHtml::checkBoxList
TbHtml