yii\helpers\BaseHtml::listBox PHP Method

listBox() public static method

Generates a list box.
public static listBox ( string $name, string | array | null $selection = null, array $items = [], array $options = [] ) : string
$name string the input name
$selection string | array | null the selected value(s). String for single or array for multiple selection(s).
$items array the option data items. The array keys are option values, and the array values are the corresponding option labels. The array can also be nested (i.e. some array values are arrays too). For each sub-array, an option group will be generated whose label is the key associated with the sub-array. If you have a list of data models, you may convert them into the format described above using [[\yii\helpers\ArrayHelper::map()]]. Note, the values and labels will be automatically HTML-encoded by this method, and the blank spaces in the labels will also be HTML-encoded.
$options array the tag options in terms of name-value pairs. The following options are specially handled: - prompt: string, a prompt text to be displayed as the first option. Since version 2.0.11 you can use an array to override the value and to set other tag attributes: ```php ['text' => 'Please select', 'options' => ['value' => 'none', 'class' => 'prompt', 'label' => 'Select']], ``` - options: array, the attributes for the select option tags. The array keys must be valid option values, and the array values are the extra attributes for the corresponding option tags. For example, ```php [ 'value1' => ['disabled' => true], 'value2' => ['label' => 'value 2'], ]; ``` - groups: array, the attributes for the optgroup tags. The structure of this is similar to that of 'options', except that the array keys represent the optgroup labels specified in $items. - unselect: string, the value that will be submitted when no option is selected. When this attribute is set, a hidden field will be generated so that if no option is selected in multiple mode, we can still obtain the posted unselect value. - encodeSpaces: bool, whether to encode spaces in option prompt and option value with ` ` character. Defaults to false. - encode: bool, whether to encode option prompt and option value characters. Defaults to `true`. This option is available since 2.0.3. The rest of the options will be rendered as the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. If a value is null, the corresponding attribute will not be rendered. See [[renderTagAttributes()]] for details on how attributes are being rendered.
return string the generated list box tag
    public static function listBox($name, $selection = null, $items = [], $options = [])
    {
        if (!array_key_exists('size', $options)) {
            $options['size'] = 4;
        }
        if (!empty($options['multiple']) && !empty($name) && substr_compare($name, '[]', -2, 2)) {
            $name .= '[]';
        }
        $options['name'] = $name;
        if (isset($options['unselect'])) {
            // add a hidden field so that if the list box has no option being selected, it still submits a value
            if (!empty($name) && substr_compare($name, '[]', -2, 2) === 0) {
                $name = substr($name, 0, -2);
            }
            $hidden = static::hiddenInput($name, $options['unselect']);
            unset($options['unselect']);
        } else {
            $hidden = '';
        }
        $selectOptions = static::renderSelectOptions($selection, $items, $options);
        return $hidden . static::tag('select', "\n" . $selectOptions . "\n", $options);
    }

Usage Example

Beispiel #1
0
 public static function multiSelect($attribute, $needle, $haystack, $key, $value, $labels, $options = [])
 {
     $newOptions = ['class' => 'form-listbox', 'multiple' => true];
     if (!empty($options)) {
         $newOptions = array_merge($newOptions, $options);
     }
     $sorted = array();
     if (!empty($haystack)) {
         foreach ($haystack as $object) {
             if (is_object($object)) {
                 if (isset($object->{$key}) && $object->{$value}) {
                     $sorted[$object->{$key}] = $object->{$value};
                 }
             } elseif (is_array($object)) {
                 if (isset($object[$key]) && $object[$value]) {
                     $sorted[$object[$key]] = $object[$value];
                 }
             }
         }
     }
     $html = '<div class="form-group">';
     $html .= '<label class="control-label">' . (isset($labels[$attribute]) ? $labels[$attribute] : '') . '</label><br>';
     $html .= BaseHtml::listBox($attribute, $needle, $sorted, $newOptions);
     $html .= '</div>';
     return $html;
 }